From 9b255c8ed903aec5d37ffb5e3d91a9b1de861397 Mon Sep 17 00:00:00 2001 From: maxluli Date: Thu, 13 Oct 2022 07:39:44 +0200 Subject: [PATCH] Added drawing of the hints --- NonoGramme/Form1.Designer.cs | 33 ++++++++- NonoGramme/Form1.cs | 49 ++++++++++++- NonoGramme/Grid.cs | 138 ++++++++++++++++++++++++++++++++--- NonoGramme/Line.cs | 42 ++++++++++- NonoGramme/Nonogramme.cs | 8 +- 5 files changed, 247 insertions(+), 23 deletions(-) diff --git a/NonoGramme/Form1.Designer.cs b/NonoGramme/Form1.Designer.cs index 63535ca..652a697 100644 --- a/NonoGramme/Form1.Designer.cs +++ b/NonoGramme/Form1.Designer.cs @@ -29,24 +29,47 @@ private void InitializeComponent() { 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(); this.SuspendLayout(); // // nonogramme1 // - this.nonogramme1.Dock = System.Windows.Forms.DockStyle.Fill; 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.Size = new System.Drawing.Size(632, 603); + this.nonogramme1.Size = new System.Drawing.Size(400, 400); this.nonogramme1.TabIndex = 0; 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 // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); 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.Name = "Form1"; this.Text = "Form1"; @@ -60,6 +83,8 @@ #endregion private Nonogramme nonogramme1; + private System.Windows.Forms.ListBox lsbFiles; + private System.Windows.Forms.Button btnImportFiles; } } diff --git a/NonoGramme/Form1.cs b/NonoGramme/Form1.cs index a0fdf8b..2da3e1c 100644 --- a/NonoGramme/Form1.cs +++ b/NonoGramme/Form1.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -13,20 +14,28 @@ namespace NonoGramme public partial class Form1 : Form { const int UI_BORDER = 30; + + string selectedFolder; + string selectedNonoGram; public Form1() { 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) { - ScaleUi(); + //ScaleUi(); } private void Form1_Resize(object sender, EventArgs e) { - ScaleUi(); + //ScaleUi(); } private void ScaleUi() { @@ -38,8 +47,40 @@ namespace NonoGramme { nonogramme1.Size = new Size(ClientSize.Width - UI_BORDER, ClientSize.Width); } - 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); + } + } } } diff --git a/NonoGramme/Grid.cs b/NonoGramme/Grid.cs index 7b21832..aee24ce 100644 --- a/NonoGramme/Grid.cs +++ b/NonoGramme/Grid.cs @@ -4,6 +4,10 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; +using System.IO; +using System.Windows.Forms; +using System.Text.RegularExpressions; + namespace NonoGramme { internal class Grid @@ -28,6 +32,110 @@ namespace NonoGramme VerticalLines = new Line[size.Width]; 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[] rows; + List[] columns; + string rawData = ""; + + //It would be smart to have a Header Struct to store all those metadatas + Regex rx = new Regex(@"^catalogue ""(?.+)""",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 ""(?.+)""", 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 ""(?.+)""", 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 ""(?.+)""", 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 (?.+)$", 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 (?[0-9]+)\nwidth (?[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[size.Height]; + rx = new Regex(@"^rows\n(?[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[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() { Random rnd = new Random(); @@ -66,17 +174,29 @@ namespace NonoGramme Size actualSize = new Size(UI.Width - offset.X, UI.Height - offset.Y); 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)) { + //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; foreach (Line line in HorizontalLines) { diff --git a/NonoGramme/Line.cs b/NonoGramme/Line.cs index cfc6cab..db844cc 100644 --- a/NonoGramme/Line.cs +++ b/NonoGramme/Line.cs @@ -8,6 +8,7 @@ namespace NonoGramme { internal class Line { + private static Random rnd = new Random(); private Tile[] _data; private int _length; private List _hints; @@ -18,17 +19,47 @@ namespace NonoGramme public Line(Tile[] states) { //This method will create a new Line wich can then be edited Data = states; + RecalculateHints(); } public Line(int length) { //This method will just create the Line but totally empty - Data = new Tile[length]; - for (int i = 0; i< length; i++) + Length = length; + Data = new Tile[Length]; + for (int i = 0; i< Length; i++) { Data[i] = new Tile(); } + 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) { Data[index].CurrentState = state; @@ -39,8 +70,11 @@ namespace NonoGramme //This will have to be done, for now its just a placeholder //TO REMOVE !! Hints = new List(); - Hints.Add(1); - Hints.Add(3); + int count = rnd.Next(1, Data.Count()-1); + for (int i = 0;i < count;i++) + { + Hints.Add(rnd.Next(1, Data.Count()-1)); + } } } } diff --git a/NonoGramme/Nonogramme.cs b/NonoGramme/Nonogramme.cs index 31532d7..9a4472c 100644 --- a/NonoGramme/Nonogramme.cs +++ b/NonoGramme/Nonogramme.cs @@ -11,7 +11,7 @@ namespace NonoGramme internal class Nonogramme : PictureBox { const int DEFAULT_GRID_SIZE = 5; - const int DEFAULT_UI_RATIO = 20; + const int DEFAULT_UI_RATIO = 30; private Size _gridSize; private Grid _gameGrid; 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)); } - public void NewGame(Size gridSize) + public void NewRandomGame(Size gridSize) { GameGrid = new Grid(gridSize); } + public void LoadNewGame(string filePath) + { + GameGrid.ImportFromFile(filePath); + } public override void Refresh() { base.Refresh();