From 0c344504fd7feaf68852b0be7cbc9a57362ea9f6 Mon Sep 17 00:00:00 2001
From: Floris <f.k.h.vandezande@students.uu.nl>
Date: Wed, 4 Dec 2024 15:22:11 +0100
Subject: [PATCH] general fixes deel 3!!

---
 PuzzlePlayer/Binary.cs     |  15 ++++-
 PuzzlePlayer/Board.cs      |   8 ++-
 PuzzlePlayer/PuzzleForm.cs | 122 +++++++++++++++++++++----------------
 3 files changed, 90 insertions(+), 55 deletions(-)

diff --git a/PuzzlePlayer/Binary.cs b/PuzzlePlayer/Binary.cs
index d3ec27f..87efad4 100644
--- a/PuzzlePlayer/Binary.cs
+++ b/PuzzlePlayer/Binary.cs
@@ -21,6 +21,7 @@ namespace PuzzlePlayer_Namespace
         public Binary(int boardSize = 6) // should be even
         {
             boardState = GetClearBoard(boardSize);
+            lastGeneratedBoard = GetClearBoard(boardSize);
 
 
             description = "Binary puzzle is played on any even-numbered square grid, with some cells initially containing black or white circles. The goal of the puzzle is to fill all cells such that:\r\n● More than two circles of the same color cannot be adjacent\r\n● Each row and column must contain an equal number of black and white circles";
@@ -65,6 +66,14 @@ namespace PuzzlePlayer_Namespace
                             tilesize.Width * 3 / 4,
                             tilesize.Height * 3 / 4);
                     }
+                    if (lastGeneratedBoard[i,j] != Board.emptySpace)
+                    {
+                        gr.FillRectangle(Brushes.LightGray,
+                            (int)(r.X + ((double)i + 0.4375) * tilesize.Width),
+                            (int)(r.Y + ((double)j + 0.4375) * tilesize.Height),
+                            tilesize.Width / 8,
+                            tilesize.Height / 8);
+                    }
                 }
             }
         }
@@ -457,9 +466,11 @@ namespace PuzzlePlayer_Namespace
 
             return false;
         }
-        public override void TileInput(Point p, int x)
+        public override void TileInput(Point? p, Keys k)
         {
-            if (x==0 || x==1 || x==2) boardState[p.X, p.Y] = x%2;
+            if (p == null) return;
+            int num = (int)k - 48;
+            if (num==0 || num==1 || num==2) boardState[((Point)p).X, ((Point)p).Y] = num%2;
         }
         public override void TileClick(Point p, int x)
         {
diff --git a/PuzzlePlayer/Board.cs b/PuzzlePlayer/Board.cs
index 341c3d3..456b590 100644
--- a/PuzzlePlayer/Board.cs
+++ b/PuzzlePlayer/Board.cs
@@ -119,9 +119,15 @@ namespace PuzzlePlayer_Namespace
         public virtual bool IsBoardValid(int[,] boardToCheck) { return true; }
 
         // changes tile P to value X
-        public abstract void TileInput(Point p, int x);
+        public abstract void TileInput(Point? p, Keys k);
 
         // performs a left/right (X) click on tile P, changing its value
         public virtual void TileClick(Point p, int x) { }
+
+        // is called by restartbutton on PuzzleForm
+        public virtual void Restart()
+        {
+            boardState = (int[,])lastGeneratedBoard.Clone();
+        }
     }
 }
diff --git a/PuzzlePlayer/PuzzleForm.cs b/PuzzlePlayer/PuzzleForm.cs
index 8bda503..2bb1e6a 100644
--- a/PuzzlePlayer/PuzzleForm.cs
+++ b/PuzzlePlayer/PuzzleForm.cs
@@ -16,6 +16,7 @@ namespace PuzzlePlayer_Namespace
         private readonly Button solvebutton;
         private readonly Button hintbutton;
         private readonly Button generatebutton;
+        private readonly Button restartbutton;
         private readonly Label titlebox;
         private readonly Label informationbox;
         private Rectangle boardspace;
@@ -50,27 +51,11 @@ namespace PuzzlePlayer_Namespace
             generatebutton = new Button();
             hintbutton = new Button();
             solvebutton = new Button();
+            restartbutton = new Button();
             UPDATEBUTTON = new Button();
             titlebox = new Label();
             informationbox = new Label();
             boardspace = new Rectangle(220, 30, 400, 400);
-            MenuStrip menuStrip = new MenuStrip
-            {
-                BackColor = SettingForm.secondaryColor,
-                ForeColor = SettingForm.secondaryColor,
-                Name = "Main menu",
-                Text = "Main Menu",
-                Dock = DockStyle.Top,
-                Font = SettingForm.mainFont,
-            };
-            ToolStripMenuItem menuSettings = new ToolStripMenuItem
-            {
-                BackColor = Color.FromArgb(54, 57, 62),
-                ForeColor = Color.Black,
-                Text = "Settings",
-                TextAlign = ContentAlignment.BottomRight,
-                Font = SettingForm.mainFont,
-            };
             this.Paint += (object o, PaintEventArgs pea) =>
             {
                 board.Draw(bufferedGraphics.Graphics, boardspace);
@@ -80,19 +65,15 @@ namespace PuzzlePlayer_Namespace
             };
             this.Resize += (object o, EventArgs ea) => UpdateUI();
             this.Move += (object o, EventArgs ea) => this.Focus();
-            this.KeyPress += (object o, KeyPressEventArgs kea) => Input(kea.KeyChar);
+            this.KeyDown += (object o, KeyEventArgs kea) => Input(kea.KeyCode);
             this.MouseClick += (object o, MouseEventArgs mea) =>
             {
                 if (mea.Button == MouseButtons.Left)
                 {
-                    Input('[');
-                    return;
-                }
-                if (mea.Button == MouseButtons.Right)
-                {
-                    Input(']');
+                    Input(Keys.LButton);
                     return;
                 }
+                if (mea.Button == MouseButtons.Right) Input(Keys.RButton);
             };
 
             Board = b;
@@ -103,13 +84,32 @@ namespace PuzzlePlayer_Namespace
         }
         private void CreateUI() //sets up ui elements
         {
+            MenuStrip menuStrip = new MenuStrip
+            {
+                BackColor = SettingForm.secondaryColor,
+                ForeColor = SettingForm.secondaryColor,
+                Name = "Main menu",
+                Text = "Main Menu",
+                Dock = DockStyle.Top,
+                Font = SettingForm.mainFont,
+            };
+            ToolStripMenuItem menuSettings = new ToolStripMenuItem
+            {
+                BackColor = Color.FromArgb(54, 57, 62),
+                ForeColor = Color.Black,
+                Text = "Settings",
+                TextAlign = ContentAlignment.BottomRight,
+                Font = SettingForm.mainFont,
+            };
+            menuStrip.Items.Add(menuSettings);
+            this.Controls.Add(menuStrip);
             void CreateButton(Button b)
             {
                 Controls.Add(b);
                 b.Size = new Size(80, 50);
                 b.FlatAppearance.BorderColor = b.BackColor = Color.Gainsboro;
                 b.Text = "DEFAULT0";
-                b.Font = new Font("Verdana", 12, FontStyle.Bold | FontStyle.Italic);
+                b.Font = new Font("Verdana", 11, FontStyle.Bold | FontStyle.Italic);
                 b.FlatStyle = FlatStyle.Flat;
                 b.GotFocus += (object o, EventArgs ea) => this.Focus();
             }
@@ -134,6 +134,13 @@ namespace PuzzlePlayer_Namespace
                 Board.Solve(false);
                 this.Invalidate();
             };
+            CreateButton(restartbutton);
+            restartbutton.Text = "Restart";
+            restartbutton.MouseClick += (object sender, MouseEventArgs mea) =>
+            {
+                Board.Restart();
+                this.Invalidate();
+            };
 
             CreateButton(UPDATEBUTTON);
             UPDATEBUTTON.Text = "Update";
@@ -141,7 +148,7 @@ namespace PuzzlePlayer_Namespace
             UPDATEBUTTON.FlatAppearance.BorderColor = UPDATEBUTTON.BackColor = Color.Pink;
             UPDATEBUTTON.MouseClick += (object sender, MouseEventArgs mea) =>
             {
-                Board = Board;
+                this.Invalidate();
             };
 
             Controls.Add(titlebox);
@@ -169,7 +176,9 @@ namespace PuzzlePlayer_Namespace
             generatebutton.Location = new Point(boardspace.Right + 30, 30);
             hintbutton.Location = new Point(boardspace.Right + 30, 110);
             solvebutton.Location = new Point(boardspace.Right + 30, 190);
-            UPDATEBUTTON.Location = new Point(boardspace.Right + 30, 270);
+            restartbutton.Location = new Point(boardspace.Right + 30, 270);
+            UPDATEBUTTON.Location = new Point(boardspace.Right + 30, 350);
+            informationbox.Text = Convert.ToString(boardspace.Size);
             this.Invalidate();
         }
         public static Size FitBoard(Size gamesize, Size maxboardsize, int drawfactor) //returns the largest rectangle smaller than MaxBoardSize that fits the given GameSize well
@@ -187,44 +196,53 @@ namespace PuzzlePlayer_Namespace
             res.Y /= (r.Height / gamesize.Height);
             return res;
         }
-        public void Input(char c) //checks if a command binded to the keyboard key and runs it, affects tile that is hovered on if applicable
+        public void Input(Keys k) //checks if a command binded to the keyboard key and runs it, affects tile that is hovered on if applicable
         {
-            if (!$"nhs[]{(char)8}1234567890".Contains(c)) return;
-            switch (c)
+            switch (k)
             {
-                case 'n':
+                case Keys.N:
+                    Board.Generate();
                     this.Invalidate();
                     return;
-                case 'h':
+                case Keys.H:
                     MessageBox.Show("Hint: geef op");
                     this.Invalidate();
                     return;
-                case 's':
+                case Keys.S:
+                    Board.Solve(false);
                     this.Invalidate();
                     return;
-
+                case Keys.Up:
+                case Keys.Left:
+                case Keys.Right:
+                case Keys.Down:
+                    Board.TileInput(null, k);
+                    return;
             }
             Point tile = GetTile(new Size(Board.boardState.GetLength(0), Board.boardState.GetLength(1)), boardspace, Control.MousePosition);
-            if (!(tile.X >= 0 && tile.X < Board.boardState.GetLength(0) && tile.Y >= 0 && tile.Y < Board.boardState.GetLength(1))) return;
-            if((int)c == 8)
+            if (!(tile.X >= 0 && tile.X < Board.boardState.GetLength(0) && tile.Y >= 0 && tile.Y < Board.boardState.GetLength(1))) return; //if tile is in bounds
+            if (!(Board.lastGeneratedBoard[tile.X, tile.Y] == Board.emptySpace)) return; //if tile is a given
+            switch (k)
             {
-                Board.boardState[tile.X, tile.Y] = Board.emptySpace;
-                this.Invalidate();
-                return;
-            }
-            if (c == '[' || c == ']') // '[' = 91, ']' = 93
-            {
-                Board.TileClick(tile, (c - 91) / 2);
-                this.Invalidate();
-                return;
-            }
-            if (char.GetNumericValue(c) != -1)
-            {
-                Board.TileInput(tile, (int)char.GetNumericValue(c));
-                this.Invalidate();
-                return;
+                case Keys.Back:
+                    Board.boardState[tile.X, tile.Y] = Board.emptySpace;
+                    this.Invalidate();
+                    return;
+                case Keys.LButton:
+                case Keys.RButton:
+                    Board.TileClick(tile, (int)k - 1);
+                    this.Invalidate();
+                    return;
+                case Keys.OemOpenBrackets:
+                case Keys.OemCloseBrackets:
+                    Board.TileClick(tile, ((int)k - 219)/2);
+                    this.Invalidate();
+                    return;
+                default:
+                    Board.TileInput(tile, k);
+                    this.Invalidate();
+                    return;
             }
-            MessageBox.Show("uhoh");
         }
     }
 }
-- 
GitLab