From 28309d84bde5b3d444546508128d7079aecda294 Mon Sep 17 00:00:00 2001 From: bionic85 <144353436+bionic85@users.noreply.github.com> Date: Wed, 20 Nov 2024 14:34:58 +0100 Subject: [PATCH] Move shit gefixt --- PuzzlePlayer/Binary.cs | 53 +++++++++++++++++++++++------------------- PuzzlePlayer/Board.cs | 29 ++++++++++++++++++----- 2 files changed, 52 insertions(+), 30 deletions(-) diff --git a/PuzzlePlayer/Binary.cs b/PuzzlePlayer/Binary.cs index f4edeac..6ef9b62 100644 --- a/PuzzlePlayer/Binary.cs +++ b/PuzzlePlayer/Binary.cs @@ -18,13 +18,11 @@ namespace PuzzlePlayer_Namespace // constructor with baordSize parameter (default is set to 8 but can be changed) public Binary(int boardSize = 8) { - // create a board with the specifide size - boardState = new int[boardSize,boardSize]; + // create a clear board with the specifide size + 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"; - - // clear the board (fill it in with -1) - Clear(); + } public void Draw (Graphics gr, Point p, Size s) @@ -53,15 +51,18 @@ namespace PuzzlePlayer_Namespace } } - private void Clear() + // static meathode for filling a int[,] with -1 + private static int[,] GetClearBoard(int boardSize) { - int size = boardState.GetLength(0); + int[,] result = new int[boardSize, boardSize]; // fill the board with empty spaces (-1) - for (int i = 0; i < size; i++) + for (int i = 0; i < boardSize; i++) { - for (int j = 0; j < size; j++) - boardState[i, j] = emptySpace; + for (int j = 0; j < boardSize; j++) + result[i, j] = emptySpace; } + + return result; } /* this static methode returns true if the board is valid in a few steps @@ -95,30 +96,36 @@ namespace PuzzlePlayer_Namespace return true; } + // generates a random solvable board public override void Generate() { - throw new NotImplementedException(); + // start with a clear board + int[,] result = GetClearBoard(boardState.GetLength(0)); + + + } - protected override List<int[,]> GetSolveList(int[,] boardToSolve) + + + // gets a list with all the possible moves + protected override List<Move> GetSolveList(int[,] boardToSolve) { - List<int[,]> result = new List<int[,]>(); + List<Move> result = new List<Move>(); for (int i = 0; i < boardToSolve.GetLength(0); i++) for (int j = 0; j < boardToSolve.GetLength(1);j++) { - int[,] move = CheckMove(i, j, boardToSolve); - if (move != null) - result.Add(move); + Move ?m = CheckMove(i, j, boardToSolve); + if (m != null) + result.Add(m); } return result; } - private int[,] CheckMove(int x, int y, int[,] boardToSolve) + private Move? CheckMove(int x, int y, int[,] boardToSolve) { - int[,] result = boardToSolve; - bool validForZero = false; bool validForOne = false; @@ -153,13 +160,11 @@ namespace PuzzlePlayer_Namespace // change the specifided place in a 0 or 1 if they are valid. if both are not valid then there is no valid move on the checked space and null is returnd. if (validForZero) - result[x, y] = 0; + return new Move(x, y, 0); else if (validForOne) - result[x, y] = 1; + return new Move(x, y, 1); else return null; - - return result; } // check if the space is surrounded on both sides by the same number. If it is, the checked space should be the opposite number @@ -194,7 +199,7 @@ namespace PuzzlePlayer_Namespace return false; } - // every row and colom should have an even number of 1's and 0's. So if the total number of 1's in a row is equal to half the with of the row a 0 should be filled in. + // every row and colom should have an even number of 1'boardSize and 0'boardSize. So if the total number of 1'boardSize in a row is equal to half the with of the row a 0 should be filled in. private bool EvenCheck(int x, int y, int[,] b, int checkFor) { // check for a row and colum (provided that the width and height of the board is the same) diff --git a/PuzzlePlayer/Board.cs b/PuzzlePlayer/Board.cs index 52484ce..7c09ba4 100644 --- a/PuzzlePlayer/Board.cs +++ b/PuzzlePlayer/Board.cs @@ -15,6 +15,20 @@ namespace PuzzlePlayer_Namespace MULTIPLE = 2 } + // a struct for a move on a certain location. changeTo is in what the place on the coordinates should change to + public struct Move + { + public int x, y; + public int changeTo; + + public Move(int x, int y, int changeTo) + { + this.x = x; + this.y = y; + this.changeTo = changeTo; + } + } + /* * This abstract class can be used to implement any kind of puzzle * One thing that is common for all puzzles is that a empty space is equal to -1 @@ -51,13 +65,16 @@ namespace PuzzlePlayer_Namespace { // two variables for storing the result and the next solveStep int[,] result = boardState; - int[,] step; + List<Move> possibleMoves; // keep looping until the SolveStep returns null // if SolveStep returns null that could mean that either the whole board is solved or it is imposible to solve the board - while ((step = SolveStep(result)) != null) + while ((possibleMoves = GetSolveList(result)) != null) { - result = step; + foreach (Move m in possibleMoves) + { + result[m.x, m.y] = m.changeTo; + } } // check if the whole board is filled @@ -74,10 +91,10 @@ namespace PuzzlePlayer_Namespace } // abstract methode for solving one step - private int[,] SolveStep(int[,] currentBoardState) + private Move? SolveStep(int[,] currentBoardState) { // get a list with all the possible moves - List<int[,]> moves = GetSolveList(currentBoardState); + List<Move> moves = GetSolveList(currentBoardState); // if there are no moves found then null is returned if (moves.Count == 0) @@ -90,7 +107,7 @@ namespace PuzzlePlayer_Namespace } // a abstract methode to get a list of all possible moves - protected abstract List<int[,]> GetSolveList(int[,] boardToSolve); + protected abstract List<Move> GetSolveList(int[,] boardToSolve); // abstract methode for generating a random board public abstract void Generate(); -- GitLab