public MoveResult OnMove(Move move, Guid matchId, Gameboard gameBoard) { var isEndOfMatch = false; var cleanGameBoard = BoardManager.ResetMoveRatings(gameBoard); Move computerMove = null; var computerMoveCriteria = ""; move.Captures = ScoreKeeper.GetMoveCaptures(move.Row, move.Column, move.PlayerNumber, cleanGameBoard); var gameBoardAfterMove = BoardManager.RecordMove(move, cleanGameBoard); var nextPlayerNumber = GetOtherPlayerNumber(move.PlayerNumber); var nextPlayerPotentialMoves = ScoreKeeper.GetNextMovesForPlayer(nextPlayerNumber, gameBoardAfterMove); if (nextPlayerPotentialMoves.Any( )) { gameBoardAfterMove = BoardManager.MapNextMoveTargets(nextPlayerPotentialMoves, gameBoardAfterMove); if (nextPlayerNumber == ComputerPlayerNumber) { var moveSelection = OthelloAI.MakeMove(nextPlayerPotentialMoves); computerMove = new Move(moveSelection.Position.Row, moveSelection.Position.Column, nextPlayerNumber); computerMoveCriteria = moveSelection.Criteria; } } else { nextPlayerNumber = move.PlayerNumber; var currentPlayerPotentialMoves = ScoreKeeper.GetNextMovesForPlayer(move.PlayerNumber, gameBoardAfterMove); if (currentPlayerPotentialMoves.Any( )) { gameBoardAfterMove = BoardManager.MapNextMoveTargets(currentPlayerPotentialMoves, gameBoardAfterMove); if (nextPlayerNumber == ComputerPlayerNumber) { var moveSelection = OthelloAI.MakeMove(currentPlayerPotentialMoves); computerMove = new Move(moveSelection.Position.Row, moveSelection.Position.Column, nextPlayerNumber); computerMoveCriteria = moveSelection.Criteria; } } else { isEndOfMatch = true; } } return(new MoveResult { CurrentPlayer = nextPlayerNumber, Gameboard = gameBoardAfterMove, MatchId = matchId, Captures = move.Captures, IsEndOfMatch = isEndOfMatch, ComputerMove = computerMove, Criteria = computerMoveCriteria }); }
public void MakeMove_choses_position_with_highest_score_if_no_corner_or_edge( ) { var positions = new List <Cell> { new Cell { Row = 2, Column = 5, PointValue = 2 }, new Cell { Row = 4, Column = 3, PointValue = 1 }, new Cell { Row = 3, Column = 3, PointValue = 7 }, new Cell { Row = 6, Column = 4, PointValue = 5 } }; var sut = OthelloAI.MakeMove(positions); Assert.Equal(7, sut.Position.PointValue); Assert.Equal(MoveSelectionCriteria.HighestScoringPosition, sut.Criteria); }
public void MakeMove_choses_corner_position_even_if_not_highest_scoring( ) { var positions = new List <Cell> { new Cell { Row = 0, Column = 0, PointValue = 2 }, // corner new Cell { Row = 0, Column = 3, PointValue = 1 }, new Cell { Row = 3, Column = 3, PointValue = 7 }, new Cell { Row = 7, Column = 4, PointValue = 5 } }; var sut = OthelloAI.MakeMove(positions); Assert.Equal(2, sut.Position.PointValue); Assert.Equal(MoveSelectionCriteria.CornerPositionAvailable, sut.Criteria); }
public void MakeMove_choses_edge_position_with_highest_score_if_multiple_available( ) { var positions = new List <Cell> { new Cell { Row = 7, Column = 5, PointValue = 2 }, // edge new Cell { Row = 0, Column = 3, PointValue = 1 }, // edge new Cell { Row = 3, Column = 3, PointValue = 7 }, new Cell { Row = 6, Column = 4, PointValue = 5 } }; var sut = OthelloAI.MakeMove(positions); Assert.Equal(2, sut.Position.PointValue); Assert.Equal(MoveSelectionCriteria.EdgePositionAvailable, sut.Criteria); }