From a285630790fd0c3f377bb4391ab21546dcff0612 Mon Sep 17 00:00:00 2001 From: bionic85 <144353436+bionic85@users.noreply.github.com> Date: Mon, 18 Nov 2024 15:19:28 +0100 Subject: [PATCH] small things --- PuzzlePlayer/Binair.cs | 19 +------------------ PuzzlePlayer/Board.cs | 23 ++++++++++++++++++++--- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/PuzzlePlayer/Binair.cs b/PuzzlePlayer/Binair.cs index b887de5..2c38c4f 100644 --- a/PuzzlePlayer/Binair.cs +++ b/PuzzlePlayer/Binair.cs @@ -71,26 +71,9 @@ namespace PuzzlePlayer_Namespace throw new NotImplementedException(); } - protected override int[,] SolveStep(int[,] currentBoardState) + protected override List<int[,]> GetSolveList(int[,] boardToSolve) { - // list with all the possible moves - List<int[,]> moves = new List<int[,]>(); - - int[,] result = currentBoardState; - - // generate all possible moves - - //TODO throw new NotImplementedException(); - - - // if there are no moves found then null is returned - if (moves.Count == 0) - return null; - - // return one of the possible moves - Random rnd = new Random(); - return moves[rnd.Next(0, moves.Count - 1)]; } } } diff --git a/PuzzlePlayer/Board.cs b/PuzzlePlayer/Board.cs index 4dcf943..f229134 100644 --- a/PuzzlePlayer/Board.cs +++ b/PuzzlePlayer/Board.cs @@ -11,6 +11,7 @@ namespace PuzzlePlayer_Namespace /* * 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 + * GetSolveList and IsBoardValid need to be overwriten in a subclass of Board */ public abstract class Board { @@ -23,8 +24,8 @@ namespace PuzzlePlayer_Namespace set { if (IsBoardValid(value) && Solve(value) != null) boardState = value; } // checks if the board is valid and solvable before setting the variable. } - // a virtual methode for solving the whole board. It uses the abstract SolveStep methode untill the whole board is solved - public virtual int[,] Solve(int[,] boardToSolve) + // a methode for solving the whole board. It uses the private SolveStep methode untill the whole board is solved + public int[,] Solve(int[,] boardToSolve) { // two variables for storing the result and the next solveStep int[,] result = boardToSolve; @@ -48,7 +49,23 @@ namespace PuzzlePlayer_Namespace } // abstract methode for solving one step - protected abstract int[,] SolveStep(int[,] currentBoardState); + private int[,] SolveStep(int[,] currentBoardState) + { + // get a list with all the possible moves + List<int[,]> moves = GetSolveList(currentBoardState); + + // if there are no moves found then null is returned + if (moves.Count == 0) + return null; + + // return one of the possible moves + // (if the first one is always chosen than that may lead to expected behavior. For example if the possible moves are checked in a certain order) + Random rnd = new Random(); + return moves[rnd.Next(0, moves.Count - 1)]; + } + + // a abstract methode to get a list of all possible moves + protected abstract List<int[,]> GetSolveList(int[,] boardToSolve); // abstract methode for generating a random board public abstract void Generate(); -- GitLab