Added drawing of the hints

This commit is contained in:
2022-10-13 07:39:44 +02:00
parent 76daa4782d
commit 9b255c8ed9
5 changed files with 247 additions and 23 deletions
+29 -4
View File
@@ -29,24 +29,47 @@
private void InitializeComponent() private void InitializeComponent()
{ {
this.nonogramme1 = new NonoGramme.Nonogramme(); this.nonogramme1 = new NonoGramme.Nonogramme();
this.lsbFiles = new System.Windows.Forms.ListBox();
this.btnImportFiles = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.nonogramme1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.nonogramme1)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
// nonogramme1 // nonogramme1
// //
this.nonogramme1.Dock = System.Windows.Forms.DockStyle.Fill;
this.nonogramme1.GridSize = new System.Drawing.Size(0, 0); this.nonogramme1.GridSize = new System.Drawing.Size(0, 0);
this.nonogramme1.Location = new System.Drawing.Point(0, 0); this.nonogramme1.Location = new System.Drawing.Point(12, 12);
this.nonogramme1.Name = "nonogramme1"; this.nonogramme1.Name = "nonogramme1";
this.nonogramme1.Size = new System.Drawing.Size(632, 603); this.nonogramme1.Size = new System.Drawing.Size(400, 400);
this.nonogramme1.TabIndex = 0; this.nonogramme1.TabIndex = 0;
this.nonogramme1.TabStop = false; this.nonogramme1.TabStop = false;
// //
// lsbFiles
//
this.lsbFiles.FormattingEnabled = true;
this.lsbFiles.ItemHeight = 16;
this.lsbFiles.Location = new System.Drawing.Point(418, 12);
this.lsbFiles.Name = "lsbFiles";
this.lsbFiles.Size = new System.Drawing.Size(204, 356);
this.lsbFiles.TabIndex = 1;
this.lsbFiles.SelectedIndexChanged += new System.EventHandler(this.lsbFiles_SelectedIndexChanged);
//
// btnImportFiles
//
this.btnImportFiles.Location = new System.Drawing.Point(418, 374);
this.btnImportFiles.Name = "btnImportFiles";
this.btnImportFiles.Size = new System.Drawing.Size(204, 36);
this.btnImportFiles.TabIndex = 2;
this.btnImportFiles.Text = "Import";
this.btnImportFiles.UseVisualStyleBackColor = true;
this.btnImportFiles.Click += new System.EventHandler(this.button1_Click);
//
// Form1 // Form1
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(632, 603); this.ClientSize = new System.Drawing.Size(634, 422);
this.Controls.Add(this.btnImportFiles);
this.Controls.Add(this.lsbFiles);
this.Controls.Add(this.nonogramme1); this.Controls.Add(this.nonogramme1);
this.Name = "Form1"; this.Name = "Form1";
this.Text = "Form1"; this.Text = "Form1";
@@ -60,6 +83,8 @@
#endregion #endregion
private Nonogramme nonogramme1; private Nonogramme nonogramme1;
private System.Windows.Forms.ListBox lsbFiles;
private System.Windows.Forms.Button btnImportFiles;
} }
} }
+45 -4
View File
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
using System.Drawing; using System.Drawing;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -13,20 +14,28 @@ namespace NonoGramme
public partial class Form1 : Form public partial class Form1 : Form
{ {
const int UI_BORDER = 30; const int UI_BORDER = 30;
string selectedFolder;
string selectedNonoGram;
public Form1() public Form1()
{ {
InitializeComponent(); InitializeComponent();
nonogramme1.NewGame(new Size(5,5)); NewGame();
}
private void NewGame()
{
nonogramme1.NewRandomGame(new Size(5, 5));
nonogramme1.Refresh();
} }
private void Form1_Load(object sender, EventArgs e) private void Form1_Load(object sender, EventArgs e)
{ {
ScaleUi(); //ScaleUi();
} }
private void Form1_Resize(object sender, EventArgs e) private void Form1_Resize(object sender, EventArgs e)
{ {
ScaleUi(); //ScaleUi();
} }
private void ScaleUi() private void ScaleUi()
{ {
@@ -38,8 +47,40 @@ namespace NonoGramme
{ {
nonogramme1.Size = new Size(ClientSize.Width - UI_BORDER, ClientSize.Width); nonogramme1.Size = new Size(ClientSize.Width - UI_BORDER, ClientSize.Width);
} }
nonogramme1.Refresh(); nonogramme1.Refresh();
} }
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
selectedFolder = dialog.SelectedPath;
}
else
{
MessageBox.Show("Ooops... Could not retrieve folder informations :(");
}
if (Directory.Exists(selectedFolder))
{
string[] nonFiles = Directory.GetFiles(selectedFolder, "*.non");
foreach (string file in nonFiles)
{
lsbFiles.Items.Add(Path.GetFileName(file));
}
}
}
private void lsbFiles_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedFile = selectedFolder + "\\" + lsbFiles.Items[lsbFiles.SelectedIndex];
if (Directory.Exists(selectedFolder) && File.Exists(selectedFile))
{
selectedNonoGram = selectedFile;
nonogramme1.LoadNewGame(selectedNonoGram);
}
}
} }
} }
+129 -9
View File
@@ -4,6 +4,10 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Drawing; using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace NonoGramme namespace NonoGramme
{ {
internal class Grid internal class Grid
@@ -28,6 +32,110 @@ namespace NonoGramme
VerticalLines = new Line[size.Width]; VerticalLines = new Line[size.Width];
Populate(); Populate();
} }
public Grid(string filePath)
{
ImportFromFile(filePath);
}
public void ImportFromFile(string filePath)
{
//GridSize = size;
//HorizontalLines = new Line[size.Height];
//VerticalLines = new Line[size.Width];
string fileContent = File.ReadAllText(filePath);
string catalogue = "";
string title = "";
string author = "";
string copyright = "";
string license = "";
Size size = new Size(0,0);
List<int>[] rows;
List<int>[] columns;
string rawData = "";
//It would be smart to have a Header Struct to store all those metadatas
Regex rx = new Regex(@"^catalogue ""(?<content>.+)""",RegexOptions.Multiline| RegexOptions.IgnoreCase);
MatchCollection matches = rx.Matches(fileContent);
if (matches.Count > 0 && matches[0].Groups.Count > 0)
{
catalogue = matches[0].Groups[1].Value;
}
rx = new Regex(@"^title ""(?<content>.+)""", RegexOptions.Multiline | RegexOptions.IgnoreCase);
matches = rx.Matches(fileContent);
if (matches.Count > 0 && matches[0].Groups.Count > 0)
{
title = matches[0].Groups[1].Value;
}
rx = new Regex(@"^by ""(?<content>.+)""", RegexOptions.Multiline | RegexOptions.IgnoreCase);
matches = rx.Matches(fileContent);
if (matches.Count > 0 && matches[0].Groups.Count > 0)
{
author = matches[0].Groups[1].Value;
}
rx = new Regex(@"^copyright ""(?<content>.+)""", RegexOptions.Multiline | RegexOptions.IgnoreCase);
matches = rx.Matches(fileContent);
if (matches.Count > 0 && matches[0].Groups.Count > 0)
{
copyright = matches[0].Groups[1].Value;
}
rx = new Regex(@"^license (?<content>.+)$", RegexOptions.Multiline | RegexOptions.IgnoreCase);
matches = rx.Matches(fileContent);
if (matches.Count > 0 && matches[0].Groups.Count > 0)
{
license = matches[0].Groups[1].Value;
}
rx = new Regex(@"^height (?<height>[0-9]+)\nwidth (?<width>[0-9]+)", RegexOptions.Multiline | RegexOptions.IgnoreCase);
matches = rx.Matches(fileContent);
if (matches.Count > 0 && matches[0].Groups.Count > 0)
{
size =new Size(Convert.ToInt32(matches[0].Groups["width"].Value), Convert.ToInt32(matches[0].Groups["height"].Value));
}
string TEST = "";
rows = new List<int>[size.Height];
rx = new Regex(@"^rows\n(?<row>[0-9,]+\r?\n)+", RegexOptions.Multiline);
matches = rx.Matches(fileContent);
if (matches.Count > 0 && matches[0].Groups.Count > 0)
{
/*
foreach (GroupCollection group in matches[0].Groups)
{
TEST += @"\n"+group[0].Value;
}
*/
}
columns = new List<int>[size.Height];
rx = new Regex(@"^columns\n([0-9,]+\r?\n)+", RegexOptions.Multiline);
matches = rx.Matches(fileContent);
if (matches.Count > 0 && matches[0].Groups.Count > 0)
{
//TODO
}
rx = new Regex(@"^goal ""([0-1]+)""", RegexOptions.Multiline | RegexOptions.IgnoreCase);
matches = rx.Matches(fileContent);
if (matches.Count > 0 && matches[0].Groups.Count > 0)
{
rawData = matches[0].Groups[1].Value;
}
MessageBox.Show(rawData);
/*
for ()
{
}
*/
}
private void Populate() private void Populate()
{ {
Random rnd = new Random(); Random rnd = new Random();
@@ -66,17 +174,29 @@ namespace NonoGramme
Size actualSize = new Size(UI.Width - offset.X, UI.Height - offset.Y); Size actualSize = new Size(UI.Width - offset.X, UI.Height - offset.Y);
Size blocSize = new Size(actualSize.Width/GridSize.Width,actualSize.Height/GridSize.Height); Size blocSize = new Size(actualSize.Width/GridSize.Width,actualSize.Height/GridSize.Height);
//Drawing the rows Text
//TODO
//Drawing the columns Text
//TODO
//Drawing the grid
using (Graphics g = Graphics.FromImage(UI)) using (Graphics g = Graphics.FromImage(UI))
{ {
//Drawing the rows Text
int rowCount = 0;
string hints = "";
for (int i = 0; i < HorizontalLines.Count(); i++)
{
hints = HorizontalLines[i].GetHorizontalHints();
g.DrawString(hints,new Font("Arial",16),Brushes.Black,new Point(0,offset.Y + rowCount*blocSize.Height));
rowCount++;
}
//Drawing the columns Text
int columnCount = 0;
hints = "";
for (int i = 0; i < VerticalLines.Count(); i++)
{
hints = VerticalLines[i].GetVerticalHints();
g.DrawString(hints, new Font("Arial", 16), Brushes.Black, new Point(offset.Y + columnCount * blocSize.Width, 0));
columnCount++;
}
//Drawing the grid
int row = 0; int row = 0;
foreach (Line line in HorizontalLines) foreach (Line line in HorizontalLines)
{ {
+38 -4
View File
@@ -8,6 +8,7 @@ namespace NonoGramme
{ {
internal class Line internal class Line
{ {
private static Random rnd = new Random();
private Tile[] _data; private Tile[] _data;
private int _length; private int _length;
private List<int> _hints; private List<int> _hints;
@@ -18,17 +19,47 @@ namespace NonoGramme
public Line(Tile[] states) { public Line(Tile[] states) {
//This method will create a new Line wich can then be edited //This method will create a new Line wich can then be edited
Data = states; Data = states;
RecalculateHints();
} }
public Line(int length) public Line(int length)
{ {
//This method will just create the Line but totally empty //This method will just create the Line but totally empty
Data = new Tile[length]; Length = length;
for (int i = 0; i< length; i++) Data = new Tile[Length];
for (int i = 0; i< Length; i++)
{ {
Data[i] = new Tile(); Data[i] = new Tile();
} }
RecalculateHints(); RecalculateHints();
} }
public string GetHorizontalHints()
{
string result = "";
int counter = 0;
foreach (int hint in Hints)
{
counter++;
if (counter != Hints.Count)
{
result += hint + ",";
}
else
{
result += hint;
}
}
return result;
}
public string GetVerticalHints()
{
string result = "";
foreach (int hint in Hints)
{
result += hint + Environment.NewLine;
}
return result;
}
public void Edit(int index, Tile.State state) public void Edit(int index, Tile.State state)
{ {
Data[index].CurrentState = state; Data[index].CurrentState = state;
@@ -39,8 +70,11 @@ namespace NonoGramme
//This will have to be done, for now its just a placeholder //This will have to be done, for now its just a placeholder
//TO REMOVE !! //TO REMOVE !!
Hints = new List<int>(); Hints = new List<int>();
Hints.Add(1); int count = rnd.Next(1, Data.Count()-1);
Hints.Add(3); for (int i = 0;i < count;i++)
{
Hints.Add(rnd.Next(1, Data.Count()-1));
}
} }
} }
} }
+6 -2
View File
@@ -11,7 +11,7 @@ namespace NonoGramme
internal class Nonogramme : PictureBox internal class Nonogramme : PictureBox
{ {
const int DEFAULT_GRID_SIZE = 5; const int DEFAULT_GRID_SIZE = 5;
const int DEFAULT_UI_RATIO = 20; const int DEFAULT_UI_RATIO = 30;
private Size _gridSize; private Size _gridSize;
private Grid _gameGrid; private Grid _gameGrid;
public Size GridSize { get => _gridSize; set => _gridSize = value; } public Size GridSize { get => _gridSize; set => _gridSize = value; }
@@ -20,10 +20,14 @@ namespace NonoGramme
{ {
GameGrid = new Grid(new Size(DEFAULT_GRID_SIZE,DEFAULT_GRID_SIZE)); GameGrid = new Grid(new Size(DEFAULT_GRID_SIZE,DEFAULT_GRID_SIZE));
} }
public void NewGame(Size gridSize) public void NewRandomGame(Size gridSize)
{ {
GameGrid = new Grid(gridSize); GameGrid = new Grid(gridSize);
} }
public void LoadNewGame(string filePath)
{
GameGrid.ImportFromFile(filePath);
}
public override void Refresh() public override void Refresh()
{ {
base.Refresh(); base.Refresh();