public void PlaceChess() { var offset = new DirectionOffset(10); var table = new AdjacentInfoTable(100, offset); table.PlaceChessPiece(22, Player.Opponent); table.PlaceChessPiece(33, Player.Opponent); var opponentTable = table.Opponent; Assert.Equal(2, opponentTable[11, Direction.MainDiagonal]); Assert.Equal(1, opponentTable[23, Direction.Horizontal.O]); Assert.Equal(0, opponentTable[23, Direction.Horizontal]); Assert.Equal(0, table.Own[11, Direction.MainDiagonal]); }
public void ExactFiveWinner() { var board = new Player[100]; board[22] = board[33] = board[44] = board[55] = board[77] = Player.Own; var offset = new DirectionOffset(10); var table = new AdjacentInfoTable(board, offset) { ExactFive = true }; table.PlaceChessPiece(66, Player.Own); Assert.Equal(Player.Empty, table.Winner); }
public void Initialize() { var board = new Player[100]; board[22] = board[33] = board[44] = board[43] = Player.Own; var offset = new DirectionOffset(10); var table = new AdjacentInfoTable(board, offset); var ownTable = table.Own; Assert.Equal(3, ownTable[11, Direction.MainDiagonal]); Assert.Equal(3, ownTable[55, Direction.MainDiagonal.O]); Assert.Equal(1, ownTable[23, Direction.Horizontal.O]); Assert.Equal(2, ownTable[23, Direction.Vertical]); Assert.Equal(2, ownTable[45, Direction.Horizontal.O]); for (int i = 0; i < Direction.TotalDirection; i++) { Assert.Equal(0, ownTable[66, i]); } Assert.Equal(0, table.Opponent[11, Direction.MainDiagonal]); }
private bool CheckRowAtWithDirection(PlayerId player, uint startX, uint startY, CheckDirection direction) { DirectionOffset offset = directionMap[direction]; // You need 3 symbols in a row to win const uint RowWinLength = 3; for (int i = 0; i < RowWinLength; ++i) { // Cast to int to avoid signed/unsigned madness int xInt = (int)startX + i * offset.X; int yInt = (int)startY + i * offset.Y; if (xInt < 0 || yInt < 0) { return(false); } // Cast back to unsigned now that we have verified that the result is positive uint x = (uint)xInt; uint y = (uint)yInt; BoardState playerSymbolState = player switch { PlayerId.X => BoardState.X, PlayerId.O => BoardState.O, _ => throw new NotImplementedException("Invalid player to check") }; // Make sure to short-circuit on invalid location. // If any of these conditions fail, there is no winning direction here if (!board.IsValidLocation(x, y) || board[x, y] != playerSymbolState) { return(false); } } // If we made it through the loop, there is a winning row here return(true); }