diff --git a/PuzzlePlayer/Binair.cs b/PuzzlePlayer/Binair.cs index f150864f42763ff5a6ee2fb33355af4e027e4298..b887de5cad3f78ee57becb303cc59d923cc7c4e0 100644 --- a/PuzzlePlayer/Binair.cs +++ b/PuzzlePlayer/Binair.cs @@ -1,31 +1,96 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Runtime.CompilerServices; namespace PuzzlePlayer_Namespace -{ +{ + /* The binair board consist of 1 and 0 + * This means that the possible states a cell can be are: empty, one or zero + * To specify this in an int[,] array we will use -1 for an empty space, + * 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 Binair : Board { - // - public int[,] boardState { get; set; } - private int[,] board; + // constructor with baordSize parameter (default is set to 8 but can be changed) + public Binair(int boardSize = 8) + { + // create a board with the specifide size + boardState = new int[boardSize,boardSize]; + + // clear the board (fill it in with -1) + Clear(); + } + + private void Clear() + { + int size = boardState.GetLength(0); + // fill the board with empty spaces (-1) + for (int i = 0; i < size; i++) + { + for (int j = 0; j < size; j++) + boardState[i, j] = emptySpace; + } + } - public Binair(int boardSize) + /* 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) { - board = new int[boardSize,boardSize]; + // 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; } - public static void Generate() + public override void Generate() { throw new NotImplementedException(); } - public static void Solve() + protected override int[,] SolveStep(int[,] currentBoardState) { + // 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 f83cf05d332bccffba97203a1e7e0fc0bfbf020f..4dcf94352d689de70cac945237d1506732a5f10b 100644 --- a/PuzzlePlayer/Board.cs +++ b/PuzzlePlayer/Board.cs @@ -1,19 +1,59 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection.Metadata; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace PuzzlePlayer_Namespace { - public interface Board + /* + * 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 + */ + public abstract class Board { - // interface should have a property to get or set the current boardstate - int[,] boardState { get; set; } + protected const int emptySpace = -1; // for every puzzle -1 represents a empty space - public abstract static void Solve(); + // a property for getting and setting the boardsstate. The boardstate should only be setted if the imputed int[,] is valid. + 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. + } - public abstract static void Generate(); + // 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) + { + // two variables for storing the result and the next solveStep + int[,] result = boardToSolve; + int[,] step; + + // 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) + { + result = step; + } + + // check if the whole board is filled + // if not then the methode will return null because it is imposible to solve the board + 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 result; + } + + // abstract methode for solving one step + protected abstract int[,] SolveStep(int[,] currentBoardState); + + // 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); } }