From a0ca0436bf8ae435538dcfadbe65d8048aa62100 Mon Sep 17 00:00:00 2001 From: bionic85 <144353436+bionic85@users.noreply.github.com> Date: Fri, 22 Nov 2024 17:31:47 +0100 Subject: [PATCH] baaa help --- PuzzlePlayer/Binary.cs | 106 +++++++++++++++---------------------- PuzzlePlayer/Board.cs | 16 +++--- PuzzlePlayer/PuzzleForm.cs | 43 ++++++++------- 3 files changed, 75 insertions(+), 90 deletions(-) diff --git a/PuzzlePlayer/Binary.cs b/PuzzlePlayer/Binary.cs index af85747..7e896f2 100644 --- a/PuzzlePlayer/Binary.cs +++ b/PuzzlePlayer/Binary.cs @@ -12,15 +12,13 @@ namespace PuzzlePlayer_Namespace * 0 for a space with a zero and 1 for a space with a one * The empty space is a constant defined in the abstract Board class */ - + internal class Binary : Board { - - // constructor with baordSize parameter (default is set to 8 but can be changed) - public Binary(int boardSize = 8) : base(boardSize) + // constructor with boardSize parameter (default is set to 8 but can be changed) + public Binary(int boardSize = 8) // should be even { - // create a clear board with the specifide size - setBoardState(GetClearBoard(boardSize)); + boardState = 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\r\n- Each row and column cannot appear multiple times on the board"; @@ -29,37 +27,37 @@ namespace PuzzlePlayer_Namespace public override void Draw (Graphics gr, Point p, Size s) { gr.FillRectangle(Brushes.Beige, p.X, p.Y, s.Width, s.Height); - for (int i = 0; i < BoardState.GetLength(0); i++) + for (int i = 0; i < boardState.GetLength(0); i++) { - for(int j = 0; j < BoardState.GetLength(1); j++) + 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)); - if (BoardState[i,j] == 0) + 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)); + 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)(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); } - if (BoardState[i, j] == 1) + 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)(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); } } } } // static meathode for filling a int[,] with -1 - private static int[,] GetClearBoard(int boardSize) + public static int[,] GetClearBoard(int boardSize) { int[,] result = new int[boardSize, boardSize]; // fill the board with empty spaces (-1) @@ -72,51 +70,16 @@ namespace PuzzlePlayer_Namespace return result; } - /* this static methode returns true if the board is valid in a few steps - 1: It checks if the size from boardToCheck is equal to the size from the boardState variable - 2: it loops though the board and checks if one of the spaces contains something other than a -1,0 or 1 - */ - public override bool IsBoardValid(int[,] boardToCheck) - { - // check if the size is NOT the same - if(!(boardToCheck.GetLength(0) == BoardState.GetLength(0) && boardToCheck.GetLength(1) == BoardState.GetLength(1))) - return false; - - - // check if any of the spaces doesn't contain a -1,0 or 1 - for(int i = 0;i < boardToCheck.GetLength(0);i++) - { - for(int j = 0; j < boardToCheck.GetLength(1);j++) - { - switch(boardToCheck[i,j]) - { - case -1: - case 0: - case 1: - break; - default: - return false; - } - } - } - - return true; - } - // generates a random solvable board public override void Generate() { // start with a clear board - int[,] startBoard = GetClearBoard(BoardState.GetLength(0)); + int[,] startBoard = GetClearBoard(boardState.GetLength(0)); // generate a board int[,] generatedBoard = BackTrackAlgorithm(startBoard); - if (setBoardState(generatedBoard)) - Debug.WriteLine("succesfully generated a board"); - else - Debug.WriteLine("failed to generate a board"); - + boardState = generatedBoard; } @@ -136,8 +99,12 @@ namespace PuzzlePlayer_Namespace // if there aren't any solutions then that could mean that the whole board is filled in, or that we are stuck and need to backtrack // and because we already did a check if the board is completly filled-in, we know that we need to backtrack if (choices.Count == 0) + { + //Debug.WriteLine("backtrack want count ==0"); return null; // backtrack + } + /* // shuffle the choices in the list around to get different results every time // First answer from: https://stackoverflow.com/questions/273313/randomize-a-listt Random random = new Random(); @@ -150,6 +117,8 @@ namespace PuzzlePlayer_Namespace choices[rand] = choices[n]; choices[n] = copy; } + //*/ + // do the algorithm for every move foreach (Move m in choices) @@ -163,6 +132,8 @@ namespace PuzzlePlayer_Namespace return result; } + //Debug.WriteLine("backtrack want moves empty"); + // if all choices fail then we should also return null return null; } @@ -183,10 +154,16 @@ namespace PuzzlePlayer_Namespace if (board[i, j] != emptySpace) continue; + if (SideCheck(i, j, board, checkFor) || MiddleCheck(i, j, board, checkFor)) + continue; + if (EvenCheck(i, j, board, checkFor)) + continue; + + /* // if one of the checks succeeds then it is an invalid move if(DoAllChecks(i,j,board,checkFor)) continue; - + //*/ // if all checks pass then add the move to the list choices.Add(new Move(i, j, checkFor)); } @@ -225,7 +202,7 @@ namespace PuzzlePlayer_Namespace // empty check if (boardToSolve[x, y] != emptySpace) return null; - + //Debug.WriteLine("wa"); // loop two times for checking 0 and 1 for (int i = 0; i <= 1; i++) { @@ -237,7 +214,12 @@ namespace PuzzlePlayer_Namespace // check if it is a valid move if (DoAllChecks(x, y, boardToSolve, opposite)) + { + Debug.WriteLine($"true {x} {y} {i}"); + return new Move(x, y, i); + } + } // if both 0 and 1 fail, then the move is invalid and null is returned diff --git a/PuzzlePlayer/Board.cs b/PuzzlePlayer/Board.cs index a3d630e..cd4170a 100644 --- a/PuzzlePlayer/Board.cs +++ b/PuzzlePlayer/Board.cs @@ -41,24 +41,22 @@ namespace PuzzlePlayer_Namespace public string description; // a property for getting and setting the boardsstate. The boardstate should only be setted if the imputed int[,] is valid. - private int[,] boardState; + public int[,] boardState; + /* public int[,] BoardState { get { return boardState; } } + */ - public Board(int size) - { - boardState = new int[size,size]; - } - + /* // checks if the board is valid and solvable before setting the variable. public bool setBoardState(int[,] newState) { int[,] copy = boardState; boardState = newState; - if (IsBoardValid(newState) && Solve(true) == SOLUTIONS.UNIQUE) + if (IsBoardValid(newState) && Solve(true) == SOLUTIONS.UNIQUE) //HIER GAAT HET FOUT return true; else { @@ -66,6 +64,7 @@ namespace PuzzlePlayer_Namespace return false; } } + */ public abstract void Draw(Graphics gr, Point p, Size s); // a methode for solving the whole board. It uses the private SolveStep methode untill the whole board is solved @@ -121,8 +120,5 @@ namespace PuzzlePlayer_Namespace // abstract methode for generating a random board public abstract void Generate(); - - // abstract methode for checking if a imputed boardstate is valid - public abstract bool IsBoardValid(int[,] boardToCheck); } } diff --git a/PuzzlePlayer/PuzzleForm.cs b/PuzzlePlayer/PuzzleForm.cs index e9796b6..74d1d0f 100644 --- a/PuzzlePlayer/PuzzleForm.cs +++ b/PuzzlePlayer/PuzzleForm.cs @@ -45,25 +45,31 @@ namespace PuzzlePlayer_Namespace { UpdateUI(); }; - + // example board: https://imgur.com/spyYaPl - /* - board.boardState[0, 0] = 1; - board.boardState[1, 0] = 1; - board.boardState[6, 0] = 1; - board.boardState[3, 1] = 1; - board.boardState[0, 2] = 1; - board.boardState[1, 2] = 1; - board.boardState[3, 2] = 1; - board.boardState[5, 4] = 1; - board.boardState[6, 4] = 1; - board.boardState[1, 5] = 1; - board.boardState[2, 5] = 1; - board.boardState[5, 5] = 1; - board.boardState[1, 7] = 1; - board.boardState[3, 7] = 0; - board.boardState[6, 7] = 1; - */ + ///* + int[,] test = Binary.GetClearBoard(Board.boardState.GetLength(0)); + + test[0, 0] = 1; + test[1, 0] = 1; + test[6, 0] = 1; + test[3, 1] = 1; + test[0, 2] = 1; + test[1, 2] = 1; + test[3, 2] = 1; + test[5, 4] = 1; + test[6, 4] = 1; + test[1, 5] = 1; + test[2, 5] = 1; + test[5, 5] = 1; + test[1, 7] = 1; + test[3, 7] = 0; + test[6, 7] = 1; + + board.boardState = test; + //Board.setBoardState(test); + + //*/ } private void CreateUI() { @@ -88,6 +94,7 @@ namespace PuzzlePlayer_Namespace hintbutton.Text = "Hint"; hintbutton.MouseClick += (object sender, MouseEventArgs mea) => { + board.Draw(graphics, boardP, boardS); MessageBox.Show("Hint: geef op"); }; CreateButton(solvebutton); -- GitLab