/// <summary> /// Get the players self defined score of a column index as its next move. /// </summary> /// <param name="board">Game board.</param> /// <param name="columnIndexForPotentialMove">Column index for next potential move.</param> /// <returns>Score of the column choice as players next move.</returns> public long GetColumnScoreAfterPotentialMove(IBoard board, int columnIndexForPotentialMove) { IBoardAnalytics boardAnalytics = new BoardAnalytics(); long finalScore = 0; // Get score for making the move finalScore += boardAnalytics.NumberOfPiecesInARowOnBoardFromPotentialMove(Board.Board.TwoInARow, new GameMove(GamePiece, columnIndexForPotentialMove), board) * Characteristics.ScoreConnectTwo; finalScore += boardAnalytics.NumberOfPiecesInARowOnBoardFromPotentialMove(Board.Board.ThreeInARow, new GameMove(GamePiece, columnIndexForPotentialMove), board) * Characteristics.ScoreConnectThree; finalScore += boardAnalytics.NumberOfPiecesInARowOnBoardFromPotentialMove(Board.Board.FourInARow, new GameMove(GamePiece, columnIndexForPotentialMove), board) * Characteristics.ScoreConnectFour; // Setup for checking opponents potential move IBoard boardAfterPotentialMove = board.Clone(); boardAfterPotentialMove.AddPiece(new GameMove(GamePiece, columnIndexForPotentialMove)); var opponentsGamePiece = GamePiece == ConnectFourGame.PlayerOneGamePiece ? ConnectFourGame.PlayerTwoGamePiece : ConnectFourGame.PlayerOneGamePiece; // Subtract points for the potential move the opponent gets if (!boardAfterPotentialMove.IsColumnFull(columnIndexForPotentialMove)) { finalScore += boardAnalytics.NumberOfPiecesInARowOnBoardFromPotentialMove(Board.Board.TwoInARow, new GameMove(opponentsGamePiece, columnIndexForPotentialMove), boardAfterPotentialMove) * Characteristics.ScoreGivingConnectTwo; finalScore += boardAnalytics.NumberOfPiecesInARowOnBoardFromPotentialMove(Board.Board.ThreeInARow, new GameMove(opponentsGamePiece, columnIndexForPotentialMove), boardAfterPotentialMove) * Characteristics.ScoreGivingConnectThree; finalScore += boardAnalytics.NumberOfPiecesInARowOnBoardFromPotentialMove(Board.Board.FourInARow, new GameMove(opponentsGamePiece, columnIndexForPotentialMove), boardAfterPotentialMove) * Characteristics.ScoreGivingConnectFour; } return(finalScore); }
public void BoardCloneTest() { IBoard board = BoardCreator.GetBoardWithVerticalLine(4); IBoard clonedBoard = board.Clone(); clonedBoard.AddPiece(new GameMove(BoardCreator.PlayerTwoGamePiece, 0)); Assert.AreNotEqual(board.GetBoard(), clonedBoard.GetBoard()); }
/// <summary> /// Apply a game piece to board. /// </summary> /// <param name="board">Connect four game board.</param> public Point MakeMove(IBoard board) { if (GamePiece == Board.Board.DefaultBoardValue) { throw new GamePieceNotSetException($"{Id} does not have a game piece."); } return(board.AddPiece(new GameMove(GamePiece, PickNextMoveColumn(board)))); }
private void AddPawnsToRow(IPlayer player, IBoard board, int boardRow) { for (int i = 0; i < 8; i++) { var pawn = new Pawn(player.Color); player.AddPiece(pawn); var position = new Position(boardRow, (char)(i + 'a')); board.AddPiece(pawn, position); } }
/// <summary> /// Adds a vertical line of a specified length to the provided board. /// </summary> /// <param name="lineLength">Length of game pieces to add in a line.</param> /// <param name="board">Board to add game pieces to.</param> /// <returns></returns> public static Point AddVerticalLineToBoard(int lineLength, IBoard board) { Point lastMove = null; for (int i = 0; i < lineLength; i++) { lastMove = board.AddPiece(new GameMove(PlayerOneGamePiece, 0)); } return(lastMove); }
//Fill the other Row without the Pawns private void AddPiecesToRow(IPlayer player, IBoard board, int boardRow) { for (int i = 0; i < 8; i++) { var pieceType = pieceTypes[i]; var pieceInstance = (IPiece)Activator.CreateInstance(pieceType, player.Color); player.AddPiece(pieceInstance); var position = new Position(boardRow, (char)(i + 'a')); board.AddPiece(pieceInstance, position); } }
/// <summary> /// Adds a backward diagonal line of a specified length to the provided board. /// </summary> /// <param name="lineLength">Length of game pieces to add in a line.</param> /// <param name="board">Board to add game pieces to.</param> /// <returns></returns> public static Point AddBackwardDiagonalLineToBoard(int lineLength, IBoard board) { Point lastMove = null; // Add opponents pieces to setup for main run for (int i = 0; i > lineLength; i++) { for (int j = 0; j < lineLength - i; j++) { board.AddPiece(new GameMove(PlayerTwoGamePiece, i)); } } // Add first player pieces to form requested line run for (int i = 0; i < lineLength; i++) { lastMove = board.AddPiece(new GameMove(PlayerOneGamePiece, i)); } return(lastMove); }
/// <summary> /// Helper function to perform a single turn /// Very useful for unit testing /// </summary> public void PerformSingleTurn(IPlayer player) { // validate the arguments // if player is null, throw an ArgumentNullException if (player == null) { throw new ArgumentNullException(); } // tell the player it's their turn using their name, and ask them for their move // using the output provider to print // TODO: CHANGE WHEN UI IS IMPLEMENTED Console.WriteLine($"{player.Name}, it's your turn. Where would you like to move?"); // set game state to WaitingForUserInput GameState = GameState.WaitingForUserInput; // get the move from the player var column = Convert.ToInt32(Console.ReadLine()); var move = moveProvider.GetMove(column); // set game state to PerformingMove GameState = GameState.PerformingMove; // create a piece with the player as the owner // TO DO BY VILDE // make a move using the board, passing in that piece board.AddPiece(piece, move); // set game state to CheckingForGameOver GameState = GameState.CheckingForGameOver; // Did the current player win? (ask the win checker) // If yes, change the game state to GameState.Winner and return if (winChecker.IsWin(board, move)) { GameState = GameState.Winner; } // Is the board full? (ask the board) // If yes, change the game state to GameState.Draw if (board.IsFull()) { GameState = GameState.Draw; } }
/// <summary> /// Apply a game piece to a random column on the board. /// </summary> /// <param name="board">Connect four game board.</param> public Point MakeMove(IBoard board) { if (board.IsFull()) { throw new BoardFullException(); } Random rndm = new Random(); var nextColumn = rndm.Next(0, board.GetColumnCount()); while (board.IsColumnFull(nextColumn)) { nextColumn++; if (nextColumn >= board.GetColumnCount()) { nextColumn = 0; } } return(board.AddPiece(new GameMove(GamePiece, nextColumn))); }