From a0c1c187b791b3a29132f924dbc808f0feceb093 Mon Sep 17 00:00:00 2001 From: bionic85 <144353436+bionic85@users.noreply.github.com> Date: Wed, 20 Nov 2024 11:50:24 +0100 Subject: [PATCH] added enum --- PuzzlePlayer/Board.cs | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/PuzzlePlayer/Board.cs b/PuzzlePlayer/Board.cs index d0658e1..ff50469 100644 --- a/PuzzlePlayer/Board.cs +++ b/PuzzlePlayer/Board.cs @@ -8,6 +8,13 @@ using System.Windows.Forms; namespace PuzzlePlayer_Namespace { + public enum SOLUTIONS + { + NONE = 0, + UNIQUE = 1, + MULTIPLE = 2 + } + /* * 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 @@ -22,14 +29,28 @@ namespace PuzzlePlayer_Namespace public int[,] boardState { get { return boardState; } - set { if (IsBoardValid(value) && Solve(value) != null) boardState = value; } // checks if the board is valid and solvable before setting the variable. + set { setBoardState(value); } + } + + // checks if the board is valid and solvable before setting the variable. + private bool setBoardState(int[,] newState) + { + int[,] copy = boardState; + boardState = newState; + if (IsBoardValid(newState) && Solve() == SOLUTIONS.UNIQUE) + return true; + else + { + boardState = copy; + return false; + } } // a methode for solving the whole board. It uses the private SolveStep methode untill the whole board is solved - public Board Solve() + public SOLUTIONS Solve() { // two variables for storing the result and the next solveStep - int[,] result = boardToSolve; + int[,] result = boardState; int[,] step; // keep looping until the SolveStep returns null @@ -44,9 +65,12 @@ namespace PuzzlePlayer_Namespace for (int i = 0; i < result.GetLength(0); i++) for (int j = 0; j < result.GetLength(1); j++) if (result[i, j] == emptySpace) - return null; + { + return SOLUTIONS.NONE; + } - return result; + boardState = result; + return SOLUTIONS.UNIQUE; } // abstract methode for solving one step -- GitLab