diff --git a/PuzzlePlayer/Binary.cs b/PuzzlePlayer/Binary.cs index 049e30979f636fb1d1add99b6ee2569111ce3f78..3458fd9a6f00b3bef7bee2d1f90bd4dee3331217 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 836b5bf24d149ff4fdc2d570f9af0c1fc6fe6237..d373062399cd4f3402f346b36694c4858cdbb473 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 7c70587197b641bc6870def576c7c04a6025d72c..f9d6f7b0ec6c34f0c4324c822b92f4ffbeef6851 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. } } }