/// <summary> /// Checks for X or 0 in every position of the array. Sets winner = 0 if full. /// </summary> /// <returns>true if the board is full</returns> private bool IsFull() { for (int i = 0; i < gameBoard.GetBoardSize(); i++) { if (gameBoard.GetBoardValue(i).Equals(BoardValue.Empty)) { return(false); } } winner = 0; return(true); }
/// <summary> /// Chooses randomly from the free positions in the array /// </summary> /// <returns></returns> public int RandomMove() { List <int> freePositions = new List <int>(); for (int i = 0; i < GameBoard.GetBoardSize(); i++) { if (GameBoard.GetBoardValue(i).Equals(BoardValue.Empty)) { freePositions.Add(i); } } return(freePositions[random.Next(0, freePositions.Count)]); }
private PlayerTurn CurrentTurn; // Describes which player's turn it is public GameLogic(GameBoard board) { Board = board; NumWinPieces = Board.GetBoardSize(); }
public WinState GetWinState() { FieldState LastPiece = FieldState.EMPTY; int EqualPiecesInRow = 0; // Check columns for (int column = 0; column < Board.GetBoardSize(); column++) { for (int row = 0; row < Board.GetBoardSize(); row++) { if (LastPiece != Board.BoardState[column, row]) { EqualPiecesInRow = 0; } LastPiece = Board.BoardState[column, row]; EqualPiecesInRow++; if (EqualPiecesInRow == NumWinPieces && LastPiece != FieldState.EMPTY) { return(LastPiece == FieldState.X ? WinState.XWIN : WinState.OWIN); } } LastPiece = FieldState.EMPTY; EqualPiecesInRow = 0; } LastPiece = FieldState.EMPTY; EqualPiecesInRow = 0; // Check rows for (int row = 0; row < Board.GetBoardSize(); row++) { for (int column = 0; column < Board.GetBoardSize(); column++) { if (LastPiece != Board.BoardState[column, row]) { EqualPiecesInRow = 0; } LastPiece = Board.BoardState[column, row]; EqualPiecesInRow++; if (EqualPiecesInRow == NumWinPieces && LastPiece != FieldState.EMPTY) { return(LastPiece == FieldState.X ? WinState.XWIN : WinState.OWIN); } } LastPiece = FieldState.EMPTY; EqualPiecesInRow = 0; } LastPiece = FieldState.EMPTY; EqualPiecesInRow = 0; // Check diagonals for (int row = NumWinPieces - 1; row < Board.GetBoardSize(); row++) { for (int column = 0; column <= row; column++) { if (LastPiece != Board.BoardState[column, row - column]) { EqualPiecesInRow = 0; } LastPiece = Board.BoardState[column, row - column]; EqualPiecesInRow++; if (EqualPiecesInRow == NumWinPieces && LastPiece != FieldState.EMPTY) { return(LastPiece == FieldState.X ? WinState.XWIN : WinState.OWIN); } } if (row > 0) { LastPiece = FieldState.EMPTY; EqualPiecesInRow = 0; for (int column = Board.GetBoardSize() - 1; column >= row; column--) { if (LastPiece != Board.BoardState[column, row + (Board.GetBoardSize() - 1 - column)]) { EqualPiecesInRow = 0; } LastPiece = Board.BoardState[column, row + (Board.GetBoardSize() - 1 - column)]; EqualPiecesInRow++; if (EqualPiecesInRow == NumWinPieces && LastPiece != FieldState.EMPTY) { return(LastPiece == FieldState.X ? WinState.XWIN : WinState.OWIN); } } } LastPiece = FieldState.EMPTY; EqualPiecesInRow = 0; } // Check anti diagonals for (int row = Board.GetBoardSize() - 1 - (NumWinPieces - 1); row >= 0; row--) { for (int column = 0; column <= Board.GetBoardSize() - 1 - row; column++) { if (LastPiece != Board.BoardState[column, row + column]) { EqualPiecesInRow = 0; } LastPiece = Board.BoardState[column, row + column]; EqualPiecesInRow++; if (EqualPiecesInRow == NumWinPieces && LastPiece != FieldState.EMPTY) { return(LastPiece == FieldState.X ? WinState.XWIN : WinState.OWIN); } } if (row > 0) { LastPiece = FieldState.EMPTY; EqualPiecesInRow = 0; for (int column = Board.GetBoardSize() - 1; column >= row; column--) { if (LastPiece != Board.BoardState[column, row + (Board.GetBoardSize() - 1 - column)]) { EqualPiecesInRow = 0; } LastPiece = Board.BoardState[column, row + (Board.GetBoardSize() - 1 - column)]; EqualPiecesInRow++; if (EqualPiecesInRow == NumWinPieces && LastPiece != FieldState.EMPTY) { return(LastPiece == FieldState.X ? WinState.XWIN : WinState.OWIN); } } } LastPiece = FieldState.EMPTY; EqualPiecesInRow = 0; } return(Board.IsFull() ? WinState.DRAW : WinState.INGAME); }