From d96a7933446f5673734399714eaae9ed7c5a47ae Mon Sep 17 00:00:00 2001 From: maxluli Date: Thu, 13 Oct 2022 10:08:13 +0200 Subject: [PATCH] Added partial file import and Hints calculation --- NonoGramme/Form1.cs | 8 +++++-- NonoGramme/Grid.cs | 51 ++++++++++++++++++++++++++++++++++++++------- NonoGramme/Line.cs | 46 ++++++++++++++++++++++++++++++++-------- 3 files changed, 87 insertions(+), 18 deletions(-) diff --git a/NonoGramme/Form1.cs b/NonoGramme/Form1.cs index 2da3e1c..2cabe13 100644 --- a/NonoGramme/Form1.cs +++ b/NonoGramme/Form1.cs @@ -25,7 +25,7 @@ namespace NonoGramme private void NewGame() { nonogramme1.NewRandomGame(new Size(5, 5)); - nonogramme1.Refresh(); + RefreshUi(); } private void Form1_Load(object sender, EventArgs e) @@ -47,9 +47,12 @@ namespace NonoGramme { nonogramme1.Size = new Size(ClientSize.Width - UI_BORDER, ClientSize.Width); } + RefreshUi(); + } + private void RefreshUi() + { nonogramme1.Refresh(); } - private void button1_Click(object sender, EventArgs e) { FolderBrowserDialog dialog = new FolderBrowserDialog(); @@ -80,6 +83,7 @@ namespace NonoGramme { selectedNonoGram = selectedFile; nonogramme1.LoadNewGame(selectedNonoGram); + RefreshUi(); } } } diff --git a/NonoGramme/Grid.cs b/NonoGramme/Grid.cs index aee24ce..108e897 100644 --- a/NonoGramme/Grid.cs +++ b/NonoGramme/Grid.cs @@ -14,6 +14,7 @@ namespace NonoGramme { private readonly Color DEFAULT_FULL_COLOR = Color.Black; private readonly Color DEFAULT_EMPTY_COLOR = Color.LightGray; + private readonly Color DEBUG_UNDISCOVERED_COLOR = Color.DarkOliveGreen; private readonly Color DEFAULT_BORDER_COLOR = Color.Pink; private readonly Color DEFAULT_ERROR_COLOR = Color.Purple; @@ -126,15 +127,38 @@ namespace NonoGramme rawData = matches[0].Groups[1].Value; } - - MessageBox.Show(rawData); - /* - for () + //MessageBox.Show(rawData); + if (rawData.Length != size.Width * size.Height) + MessageBox.Show("The file contains conflicting datas"); + + //Filling the class with infos + GridSize = size; + HorizontalLines = new Line[GridSize.Height]; + VerticalLines = new Line[GridSize.Width]; + + for (int y = 0; y < GridSize.Height; y++) { + HorizontalLines[y] = new Line(GridSize.Width); + for (int x = 0; x < GridSize.Width; x++) + { + if (y == 0) + { + VerticalLines[x] = new Line(GridSize.Height); + } + Tile.State tileState = Tile.State.Empty; + int flatIndex = y * GridSize.Width + x; + if (rawData[flatIndex] == 1) + { + tileState = Tile.State.Undiscovered; + } + HorizontalLines[y].Edit(x,tileState); + VerticalLines[x].Edit(y,tileState); + } } - */ + + MessageBox.Show("File imported Successfully"); } private void Populate() { @@ -157,7 +181,7 @@ namespace NonoGramme state = Tile.State.Empty; break; case 3: - state = Tile.State.Full; + state = Tile.State.Undiscovered; break; default: state = Tile.State.Empty; @@ -169,8 +193,21 @@ namespace NonoGramme } Console.WriteLine("Coucou"); } + public void RefreshGrid() + { + foreach (Line line in HorizontalLines) + { + line.RecalculateHints(); + } + foreach (Line line in VerticalLines) + { + line.RecalculateHints(); + } + } public Bitmap Draw(Bitmap UI,Point offset) { + RefreshGrid(); + Size actualSize = new Size(UI.Width - offset.X, UI.Height - offset.Y); Size blocSize = new Size(actualSize.Width/GridSize.Width,actualSize.Height/GridSize.Height); @@ -216,7 +253,7 @@ namespace NonoGramme g.FillRectangle(new SolidBrush(DEFAULT_EMPTY_COLOR), new Rectangle(position, blocSize)); break; case Tile.State.Undiscovered: - g.FillRectangle(new SolidBrush(DEFAULT_FULL_COLOR), new Rectangle(position, blocSize)); + g.FillRectangle(new SolidBrush(DEBUG_UNDISCOVERED_COLOR), new Rectangle(position, blocSize)); break; default: g.FillRectangle(new SolidBrush(DEFAULT_ERROR_COLOR), new Rectangle(position, blocSize)); diff --git a/NonoGramme/Line.cs b/NonoGramme/Line.cs index db844cc..6ac21fc 100644 --- a/NonoGramme/Line.cs +++ b/NonoGramme/Line.cs @@ -16,7 +16,8 @@ namespace NonoGramme public int Length { get => _length; set => _length = value; } public List Hints { get => _hints; set => _hints = value; } - public Line(Tile[] states) { + public Line(Tile[] states) + { //This method will create a new Line wich can then be edited Data = states; RecalculateHints(); @@ -26,9 +27,9 @@ namespace NonoGramme //This method will just create the Line but totally empty Length = length; Data = new Tile[Length]; - for (int i = 0; i< Length; i++) + for (int i = 0; i < Length; i++) { - Data[i] = new Tile(); + Data[i] = new Tile(); } RecalculateHints(); @@ -52,7 +53,7 @@ namespace NonoGramme return result; } public string GetVerticalHints() - { + { string result = ""; foreach (int hint in Hints) { @@ -67,13 +68,40 @@ namespace NonoGramme } public void RecalculateHints() { - //This will have to be done, for now its just a placeholder - //TO REMOVE !! Hints = new List(); - int count = rnd.Next(1, Data.Count()-1); - for (int i = 0;i < count;i++) + bool previousTile = false; + bool currentTile = false; + int counter = 0; + for (int i = 0; i < Data.Length; i++) { - Hints.Add(rnd.Next(1, Data.Count()-1)); + switch (Data[i].CurrentState) + { + case Tile.State.Discovered: + case Tile.State.Empty: + case Tile.State.Full: + currentTile = false; + break; + case Tile.State.Undiscovered: + currentTile = true; + break; + } + if (currentTile) + { + counter++; + } + if (!currentTile && previousTile) + { + //We had a full segment that just finished + Hints.Add(counter); + counter = 0; + } + + previousTile = currentTile; + } + if (currentTile) + { + //if the last tilke is full then we need to add it at the end + Hints.Add(counter); } } }