diff --git a/PuzzlePlayer/Binary.cs b/PuzzlePlayer/Binary.cs index f3c453a8e1fd9585b00329ba3dd09384f008986e..5389e42cb94bacdbbaaea08f0dc5ac61f04cfcac 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); + } } } } @@ -432,9 +441,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 766aae9d457971a2e6cf85c67ec86295298f9652..a18ffc62ae05e86e9fdd474fa86f0e523f2099e9 100644 --- a/PuzzlePlayer/Board.cs +++ b/PuzzlePlayer/Board.cs @@ -133,10 +133,16 @@ 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 de33cd932d769657583bd58074a2d9801c1293bf..728b3b4c8ce6f7aaa70f0eeae70e5ec20963da4a 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,10 +51,39 @@ 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); + this.Paint += (object o, PaintEventArgs pea) => + { + board.Draw(bufferedGraphics.Graphics, boardspace); + //bufferedGraphics.Graphics.FillRectangle(Brushes.LightCoral, 220, 30, this.Width - 350, this.Height - 100); + //bufferedGraphics.Graphics.FillRectangle(Brushes.DarkRed, boardspace); + bufferedGraphics.Render(); + }; + this.Resize += (object o, EventArgs ea) => UpdateUI(); + this.Move += (object o, EventArgs ea) => this.Focus(); + this.KeyDown += (object o, KeyEventArgs kea) => Input(kea.KeyCode); + this.MouseClick += (object o, MouseEventArgs mea) => + { + if (mea.Button == MouseButtons.Left) + { + Input(Keys.LButton); + return; + } + if (mea.Button == MouseButtons.Right) Input(Keys.RButton); + }; + + Board = b; + CreateUI(); + + Board.boardState[1, 1] = 1; + Board.boardState[2, 2] = 0; + } + private void CreateUI() //sets up ui elements + { MenuStrip menuStrip = new MenuStrip { BackColor = SettingForm.secondaryColor, @@ -100,13 +130,15 @@ namespace PuzzlePlayer_Namespace } private void CreateUI() //sets up ui elements { + 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(); } @@ -131,6 +163,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"; @@ -138,7 +177,7 @@ namespace PuzzlePlayer_Namespace UPDATEBUTTON.FlatAppearance.BorderColor = UPDATEBUTTON.BackColor = Color.Pink; UPDATEBUTTON.MouseClick += (object sender, MouseEventArgs mea) => { - Board = Board; + this.Invalidate(); }; Controls.Add(titlebox); @@ -166,7 +205,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 @@ -184,44 +225,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) - { - 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) + 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.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"); } } }