From 95f523a24fbc64e75a8e3d5c9117e52a1f96ad3f Mon Sep 17 00:00:00 2001
From: Floris <f.k.h.vandezande@students.uu.nl>
Date: Wed, 27 Nov 2024 15:33:41 +0100
Subject: [PATCH] board visual update

- Fixed board not updating/drawing on start
- Fixed pixel offsets and nonsquare tiles with FitBoard method
---
 PuzzlePlayer/Binary.cs     | 29 ++++++++++++++-------------
 PuzzlePlayer/Board.cs      |  2 +-
 PuzzlePlayer/PuzzleForm.cs | 41 +++++++++++++++++++++++++++-----------
 3 files changed, 45 insertions(+), 27 deletions(-)

diff --git a/PuzzlePlayer/Binary.cs b/PuzzlePlayer/Binary.cs
index 049e309..3458fd9 100644
--- a/PuzzlePlayer/Binary.cs
+++ b/PuzzlePlayer/Binary.cs
@@ -27,33 +27,34 @@ namespace PuzzlePlayer_Namespace
             Clear();
         }
 
-        public override void Draw (Graphics gr, Point p, Size s)
+        public override void Draw (Graphics gr, Rectangle r) //draws board in rectangle R. warning: will stretch image unless FitBoard() is used for rectangle size
         {
-            gr.FillRectangle(Brushes.Beige, p.X, p.Y, s.Width, s.Height);
+            Size tilesize = new Size(r.Width / boardState.GetLength(0), r.Height / boardState.GetLength(1));
+            gr.FillRectangle(Brushes.Gainsboro, r.X, r.Y, tilesize.Width*boardState.GetLength(0), tilesize.Height*boardState.GetLength(1));
             for (int i = 0; i < boardState.GetLength(0); i++)
             {
                 for(int j = 0; j < boardState.GetLength(1); j++)
                 {
                     gr.DrawRectangle(Pens.Black,
-                        p.X+i*s.Width/boardState.GetLength(0),
-                        p.Y+j*s.Height/boardState.GetLength(1),
-                        s.Width / boardState.GetLength(0),
-                        s.Height / boardState.GetLength(1));
+                        r.X+i* tilesize.Width,
+                        r.Y+j* tilesize.Height,
+                        tilesize.Width,
+                        tilesize.Height);
                     if (boardState[i,j] == 0)
                     {
                         gr.FillEllipse(Brushes.White,
-                            (int)(p.X + ((double)i + 0.125) * s.Width / boardState.GetLength(0)),
-                            (int)(p.Y + ((double)j + 0.125) * s.Height / boardState.GetLength(1)),
-                            s.Width / boardState.GetLength(0) * 3 / 4,
-                            s.Height / boardState.GetLength(1) * 3 / 4);
+                            (int)(r.X + ((double)i + 0.125) * tilesize.Width),
+                            (int)(r.Y + ((double)j + 0.125) * tilesize.Height),
+                            tilesize.Width * 3 / 4,
+                            tilesize.Height * 3 / 4);
                     }
                     if (boardState[i, j] == 1)
                     {
                         gr.FillEllipse(Brushes.Black,
-                            (int)(p.X + ((double)i + 0.125) * s.Width / boardState.GetLength(0)),
-                            (int)(p.Y + ((double)j + 0.125) * s.Height / boardState.GetLength(1)),
-                            s.Width / boardState.GetLength(0) * 3 / 4,
-                            s.Height / boardState.GetLength(1) * 3 / 4);
+                            (int)(r.X + ((double)i + 0.125) * tilesize.Width),
+                            (int)(r.Y + ((double)j + 0.125) * tilesize.Height),
+                            tilesize.Width * 3 / 4,
+                            tilesize.Height * 3 / 4);
                     }
                 }
             }
diff --git a/PuzzlePlayer/Board.cs b/PuzzlePlayer/Board.cs
index 836b5bf..d373062 100644
--- a/PuzzlePlayer/Board.cs
+++ b/PuzzlePlayer/Board.cs
@@ -43,7 +43,7 @@ namespace PuzzlePlayer_Namespace
                 return false;
             }
         }
-        public abstract void Draw(Graphics gr, Point p, Size s);
+        public abstract void Draw(Graphics gr, Rectangle r);
         // a methode for solving the whole board. It uses the private SolveStep methode untill the whole board is solved
         public SOLUTIONS Solve()
         {
diff --git a/PuzzlePlayer/PuzzleForm.cs b/PuzzlePlayer/PuzzleForm.cs
index 7c70587..f9d6f7b 100644
--- a/PuzzlePlayer/PuzzleForm.cs
+++ b/PuzzlePlayer/PuzzleForm.cs
@@ -14,9 +14,9 @@ namespace PuzzlePlayer_Namespace
         private Button solvebutton;
         private Button hintbutton;
         private Button generatebutton;
-        private Label boardlabel;
         private TextBox informationbox;
-        public Graphics graphics;
+        private Graphics graphics;
+        private Rectangle boardspace;
 
         private Board board;
         public Board Board //Updating the Board member will immediately call board.Draw method so that the board is updated visually
@@ -25,7 +25,7 @@ namespace PuzzlePlayer_Namespace
             set
             {
                 board = value;
-                board.Draw(graphics, new Point(220, 30), new Size(400, 400));
+                this.Invalidate();
             }
         }
 
@@ -33,18 +33,26 @@ namespace PuzzlePlayer_Namespace
         {
             if (s == default(Size)) s = new Size(800, 500);
             this.Size = s;
+            this.WindowState = FormWindowState.Maximized;
+            this.BackColor = Color.Beige;
             graphics = this.CreateGraphics();
+            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
             generatebutton = new Button();
             hintbutton = new Button();
             solvebutton = new Button();
             informationbox = new TextBox();
+            boardspace = new Rectangle(220, 30, 400, 400);
+            this.Paint += (object o, PaintEventArgs pea) =>
+            {
+                graphics.Clear(this.BackColor);
+                board.Draw(graphics, boardspace);
+                //graphics.FillRectangle(Brushes.LightCoral, 220, 30, this.Width - 400, this.Height - 100);
+                //graphics.FillRectangle(Brushes.DarkRed, 220, 30, boardspace.Width*Board.boardState.GetLength(0), boardspace.Height * Board.boardState.GetLength(1));
+            };
+            this.Resize += (object o, EventArgs ea) => UpdateUI();
             Board = b;
             CreateUI();
             UpdateUI();
-            this.Resize += (object sender, EventArgs ea) =>
-            {
-                UpdateUI();
-            };
             Board.boardState[1, 1] = 1;
             Board.boardState[2, 2] = 0;
         }
@@ -74,7 +82,7 @@ namespace PuzzlePlayer_Namespace
             solvebutton.MouseClick += (object sender, MouseEventArgs mea) =>
             {
                 //board = board.Solve();
-                board.Draw(graphics, new Point(220, 30), new Size(400, 400));
+                Board = Board;
             };
 
             Controls.Add(informationbox);
@@ -83,13 +91,22 @@ namespace PuzzlePlayer_Namespace
             informationbox.BackColor = Color.LightGray;
             informationbox.Text = board.description;
         }
-
         private void UpdateUI()
         {
-            generatebutton.Location = new Point(this.Width - 160, 30);
-            hintbutton.Location = new Point(this.Width - 160, 130);
-            solvebutton.Location = new Point(this.Width - 160, 230);
+            boardspace.Size = FitBoard(new Size(Board.boardState.GetLength(0),Board.boardState.GetLength(1)),new Size(this.Width - 400, this.Height - 100),8);
+            generatebutton.Location = new Point(boardspace.Right + 30, 30);
+            hintbutton.Location = new Point(boardspace.Right + 30, 130);
+            solvebutton.Location = new Point(boardspace.Right + 30, 230);
             informationbox.Location = new Point(40, 30);
+            this.Invalidate();
+        }
+        public static Size FitBoard(Size gamesize, Size maxboardsize, int drawfactor)
+        {
+            int TileLength = Math.Min(maxboardsize.Width / gamesize.Width, maxboardsize.Height / gamesize.Height)/drawfactor*drawfactor;
+            return new Size(gamesize.Width*TileLength, gamesize.Height*TileLength);
+            //After each of the divisions in this method, the value should be rounded down,
+            //as the end value cannot exceed the size given by maxboardsize.
+            //however, C# rounds divisions of Int values down by default, so this does not have to be explicitly done.
         }
     }
 }
-- 
GitLab