public void CountOneMoveTest() { CandidateMovesAll allMoves = new CandidateMovesAll(); HexBoard testBoard = new HexBoard(BoardSize); testBoard.PlayMove(5, 5, true); IEnumerable<Location> moves = allMoves.CandidateMoves(testBoard, 0); Assert.AreEqual(BoardCellCount - 1, moves.Count()); }
public void BoardCopyTest() { HexBoard hexBoard = new HexBoard(BoardSize); HexBoard copyBoard = new HexBoard(hexBoard); Assert.IsTrue(copyBoard.Equals(hexBoard)); copyBoard.PlayMove(0, 0, true); Assert.IsFalse(copyBoard.Equals(hexBoard)); }
public void CountOneMoveTest() { GoodMoves goods = new GoodMoves(); CandidateMovesSelective moveFinder = new CandidateMovesSelective(goods, 0); HexBoard testBoard = new HexBoard(BoardSize); testBoard.PlayMove(5, 5, true); IEnumerable<Location> moves = moveFinder.CandidateMoves(testBoard, 0); Assert.Greater(BoardCellCount - 1, moves.Count()); }
public void ClearTest() { HexBoard hexBoard = new HexBoard(BoardSize); // set a cell hexBoard.PlayMove(1, 1, true); Assert.AreEqual(Occupied.PlayerX, hexBoard.GetCellOccupiedAt(1, 1)); Location loc11 = new Location(1, 1); Assert.AreEqual(Occupied.PlayerX, hexBoard.GetCellOccupiedAt(1, 1)); Assert.AreEqual(Occupied.PlayerX, hexBoard.GetCellOccupiedAt(loc11)); // reset it hexBoard.Clear(); Assert.AreEqual(Occupied.Empty, hexBoard.GetCellOccupiedAt(1, 1)); Assert.AreEqual(Occupied.Empty, hexBoard.GetCellOccupiedAt(loc11)); }
public void CountDownTest() { CandidateMovesAll allMoves = new CandidateMovesAll(); HexBoard testBoard = new HexBoard(BoardSize); int emptyCellCount = BoardSize * BoardSize; for (int x = 0; x < BoardSize; x++) { for (int y = 0; y < BoardSize; y++) { // as cells are played, less empty cells are left testBoard.PlayMove(x, y, true); emptyCellCount--; IEnumerable<Location> moves = allMoves.CandidateMoves(testBoard, 0); Assert.AreEqual(emptyCellCount, moves.Count()); } } }
private static void PlayerScoreAlmostBarricaded(IPathLengthFactory pathLengthFactory) { HexBoard hexBoard = new HexBoard(BoardSize); PathLengthBase pathLength = pathLengthFactory.CreatePathLength(hexBoard); // almost barricated board for (int y = 0; y < hexBoard.Size; y++) { if (y != 3) { hexBoard.PlayMove(2, y, true); } } int xScore = pathLength.PlayerScore(true); Assert.IsTrue(xScore > 0); int yScore = pathLength.PlayerScore(false); Assert.IsTrue(yScore > xScore); // strong advantage to player 1 int advantageMoveScore = pathLength.SituationScore(); Assert.IsTrue(advantageMoveScore > 0); }
private static HexBoard RandomBoard(int size, int moves, Random rands) { HexBoard result = new HexBoard(size); for (int loopCount = 0; loopCount < moves; loopCount++) { int x = rands.Next(size); int y = rands.Next(size); if (result.GetCellOccupiedAt(x, y) == Occupied.Empty) { result.PlayMove(x, y, (loopCount % 2 == 0)); } } return result; }
private static void PlayFourMoves(HexBoard board) { board.PlayMove(2, 0, true); // playerX board.PlayMove(0, 2, false); // PlayerY board.PlayMove(2, 4, true); // playerX board.PlayMove(4, 2, false); // PlayerY }
private static void PlayTwoMoves(HexBoard playBoard) { // best move (and winning move ) is the middle (1,1) or corner (0, 2) // but it takes lookahead or 3 or more to see that // Should be quick to calc since there's only 9 cells // Search tree is not broad, we can go deep playBoard.PlayMove(1, 0, true); // PlayerX playBoard.PlayMove(2, 1, false); // PlayerY }
private static void PlayFourMoves(HexBoard playBoard) { // set up so the best move should be that the middle - a winning move for either player // can see this just looking at one move playBoard.PlayMove(1, 0, true); // PlayerX playBoard.PlayMove(2, 1, false); // PlayerY playBoard.PlayMove(0, 1, false); // PlayerX playBoard.PlayMove(1, 2, true); // PlayerY }
public void TestCalculateMove4() { HexBoard board = new HexBoard(3); board.PlayMove(0, 0, true); // playerX board.PlayMove(0, 1, false); // PlayerY board.PlayMove(0, 2, true); // playerX board.PlayMove(1, 0, false); // PlayerY board.PlayMove(1, 1, true); // PlayerX board.PlayMove(1, 2, false); // playerY Minimax minimax = MakeMinimaxForBoard(board); // test score at this point PathLengthLoop pathLength = new PathLengthLoop(board); int playerScore = pathLength.PlayerScore(true); Assert.AreEqual(1, playerScore); playerScore = pathLength.PlayerScore(false); Assert.AreEqual(1, playerScore); // only 3 cells are vacant minimax.DoMinimax(1, false); minimax.DoMinimax(1, true); minimax.DoMinimax(2, false); minimax.DoMinimax(2, true); minimax.DoMinimax(3, false); minimax.DoMinimax(3, true); minimax.DoMinimax(4, false); minimax.DoMinimax(4, true); minimax.DoMinimax(5, false); minimax.DoMinimax(5, true); }
public void DuplicatePlayTest() { HexBoard hexBoard = new HexBoard(BoardSize); // set a cell hexBoard.PlayMove(1, 1, true); hexBoard.PlayMove(1, 1, true); }
private static void PlayToWinInThreeMoves(HexBoard board) { // win in 3 moves board.PlayMove(2, 0, true); // playerX board.PlayMove(2, 1, true); // playerX board.PlayMove(2, 4, true); // playerX board.PlayMove(2, 5, true); // playerX board.PlayMove(0, 2, false); // playery board.PlayMove(3, 2, false); // playery board.PlayMove(4, 2, false); // playery board.PlayMove(5, 2, false); // playery }
public void MovesPlayedCountIncrementsAfterMove() { HexBoard hexBoard = new HexBoard(BoardSize); Assert.AreEqual(0, hexBoard.MovesPlayedCount); hexBoard.PlayMove(1, 1, true); Assert.AreEqual(1, hexBoard.MovesPlayedCount); }
public void ToStringTest() { HexBoard board = new HexBoard(4); board.PlayMove(1, 1, true); string out1 = board.ToString(); Assert.IsFalse(String.IsNullOrEmpty(out1)); board.PlayMove(2, 2, false); string out2 = board.ToString(); Assert.IsFalse(String.IsNullOrEmpty(out2)); Assert.AreNotEqual(out1, out2); }
public void MoveSetsCellOccupied() { HexBoard hexBoard = new HexBoard(BoardSize); Assert.AreEqual(Occupied.Empty, hexBoard.GetCellOccupiedAt(1, 1)); hexBoard.PlayMove(1, 1, true); Assert.AreEqual(Occupied.PlayerX, hexBoard.GetCellAt(1, 1).IsOccupied); Assert.AreEqual(Occupied.PlayerX, hexBoard.GetCellOccupiedAt(1, 1)); }
public void GetCellsTest() { HexBoard hexBoard = new HexBoard(BoardSize); Cell[,] cells = hexBoard.GetCells(); Assert.AreEqual(cells[1, 1].IsOccupied, Occupied.Empty); Assert.AreEqual(cells[2, 2].IsOccupied, Occupied.Empty); Assert.AreEqual(cells[3, 3].IsOccupied, Occupied.Empty); hexBoard.PlayMove(1, 1, true); Assert.AreEqual(cells[1, 1].IsOccupied, Occupied.PlayerX); Assert.AreEqual(cells[2, 2].IsOccupied, Occupied.Empty); Assert.AreEqual(cells[3, 3].IsOccupied, Occupied.Empty); hexBoard.PlayMove(2, 2, false); Assert.AreEqual(cells[1, 1].IsOccupied, Occupied.PlayerX); Assert.AreEqual(cells[2, 2].IsOccupied, Occupied.PlayerY); Assert.AreEqual(cells[3, 3].IsOccupied, Occupied.Empty); }
public void EmptyCellsTest() { const int SmallBoardSize = 4; const int BoardCellCount = SmallBoardSize * SmallBoardSize; HexBoard source = new HexBoard(SmallBoardSize); Location[] emptyCells1 = source.EmptyCells().ToArray(); Assert.IsNotNull(emptyCells1); Assert.AreEqual(BoardCellCount, emptyCells1.Length); source.PlayMove(2, 2, false); Location[] emptyCells2 = source.EmptyCells().ToArray(); Assert.IsNotNull(emptyCells2); Assert.AreEqual(BoardCellCount - 1, emptyCells2.Length); }
public void TestNoLingering2() { // prefer a quick win to a slow one HexBoard board = new HexBoard(5); Minimax minimax = MakeMinimaxForBoard(board); // diagonal - one move to win board.PlayMove(4, 0, true); // playerX board.PlayMove(3, 1, true); // playerX // this.board.PlayMove(2, 2, true); // playerX board.PlayMove(1, 3, true); // playerX board.PlayMove(0, 4, true); // playerX for (int depth = 1; depth < 6; depth++) { DoTestTestNoLingering2Win(minimax, depth); } }
public void TestMinimax4() { HexBoard board = new HexBoard(5); Minimax minimax = MakeMinimaxForBoard(board); /* on a 5 * 5 board, red(playerx) has 3, 0 and 1, 4 and 2,2 PlayerX has won, even if PlayerY goes next should know this at look ahead 5 */ board.PlayMove(3, 0, true); board.PlayMove(1, 4, true); board.PlayMove(2, 2, true); MinimaxResult bestMove = minimax.DoMinimax(4, false); AssertWinner(bestMove.Score, Occupied.PlayerX); }
private static void PlayToWinInOneMove(HexBoard board, bool playerX) { // one move at 0,4 to win board.PlayMove(4, 0, playerX); board.PlayMove(3, 1, playerX); board.PlayMove(2, 2, playerX); board.PlayMove(1, 3, playerX); }
public void TestMinimax3() { HexBoard board = new HexBoard(5); Minimax minimax = MakeMinimaxForBoard(board); /* on a 5 * 5 board, red(playerx) has 3, 0 and 1, 4 needs to play 2,2 to win - should know this at look ahead 5 */ board.PlayMove(3, 0, true); board.PlayMove(1, 4, true); MinimaxResult bestMove = minimax.DoMinimax(5, true); Location expectedMove = new Location(2, 2); Assert.IsTrue(MoveScoreConverter.IsWin(bestMove.Score), "No win " + bestMove.Score); Assert.AreEqual(expectedMove, bestMove.Move, "Wrong expected move"); }
private static void PlayerScoreZigZag(IPathLengthFactory pathLengthFactory) { HexBoard hexBoard = new HexBoard(BoardSize); PathLengthBase pathLength = pathLengthFactory.CreatePathLength(hexBoard); for (int y = 0; y < hexBoard.Size - 1; y++) { hexBoard.PlayMove(2, y, true); hexBoard.PlayMove(5, hexBoard.Size - (1 + y), true); int xScore = pathLength.PlayerScore(true); Assert.IsTrue(xScore > 0); int yScore = pathLength.PlayerScore(false); Assert.IsTrue(yScore >= hexBoard.Size); if (y > (hexBoard.Size / 2)) { Assert.IsTrue(yScore > xScore); } // some advantage to player 1 int advantageMoveScore = pathLength.SituationScore(); Assert.IsTrue(advantageMoveScore >= y); } }
public void TestCalculateMove2SituationScoreMiddle() { HexBoard board = new HexBoard(3); PlayTwoMoves(board); // playing middle gets adavantage board.PlayMove(1, 1, true); PathLengthLoop pathLength = new PathLengthLoop(board); int moveScore = pathLength.SituationScore(); Assert.AreEqual(1, moveScore); board.GetCellAt(1, 1).IsOccupied = Occupied.PlayerY; moveScore = pathLength.SituationScore(); Assert.AreEqual(-1, moveScore); board.GetCellAt(1, 1).IsOccupied = Occupied.Empty; }
private static void PlayerScoreBaricaded(IPathLengthFactory pathLengthFactory) { HexBoard hexBoard = new HexBoard(BoardSize); PathLengthBase pathLength = pathLengthFactory.CreatePathLength(hexBoard); // baricaded board for (int y = 0; y < hexBoard.Size; y++) { hexBoard.PlayMove(2, y, true); } int xScore = pathLength.PlayerScore(true); Assert.AreEqual(xScore, 0); int yScore = pathLength.PlayerScore(false); Assert.IsTrue(yScore > hexBoard.Size); // winning advantage to player 1 int winMoveScore = pathLength.SituationScore(); AssertWinner(winMoveScore, Occupied.PlayerX); }
public void TestGetEmptyNeighbours2() { HexBoard hexBoard = new HexBoard(BoardSize); // get the neighbours2 with nothing inbetween Cell focus = hexBoard.GetCellAt(3, 3); Cell[][] neighbours2EmptyBoard = hexBoard.EmptyNeighbours2(focus); // should be six sets Assert.AreEqual(6, neighbours2EmptyBoard.Length); // playing an ajoining cell removes 2 sets hexBoard.PlayMove(3, 4, false); Cell[][] neighbours2PlayedCell = hexBoard.EmptyNeighbours2(focus); Assert.AreEqual(4, neighbours2PlayedCell.Length); }