public UltimateTicTacToeBoard() { for (int i = 0; i < 9; i++) { _grid[i] = new TicTacToeBoard(); } State = GameState.Open; WinningMove = null; }
private static short CountWins(Player player, TicTacToeBoard board) { var wins = (short)(CanWin(player, board, new[] { 0, 1, 2 }) ? 1 : 0); wins += (short)(CanWin(player, board, new[] { 3, 4, 5 }) ? 1 : 0); wins += (short)(CanWin(player, board, new[] { 6, 7, 8 }) ? 1 : 0); wins += (short)(CanWin(player, board, new[] { 0, 3, 6 }) ? 1 : 0); wins += (short)(CanWin(player, board, new[] { 1, 4, 7 }) ? 1 : 0); wins += (short)(CanWin(player, board, new[] { 2, 5, 8 }) ? 1 : 0); wins += (short)(CanWin(player, board, new[] { 0, 4, 8 }) ? 1 : 0); wins += (short)(CanWin(player, board, new[] { 2, 4, 6 }) ? 1 : 0); return(wins); }
private static short CalculateScore(TicTacToeBoard board) { short xWins = 0; short oWins = 0; if (board.State == GameState.Xwin) { xWins = 100; } else if (board.State == GameState.Owin) { oWins = 100; } else if (board.State == GameState.Open) { xWins = CountWins(Player.X, board); oWins = CountWins(Player.O, board); } return((short)(xWins - oWins)); }
internal TicTacToeBoard Clone(bool cloneMoves = false) { var clonedBoard = new TicTacToeBoard(); if (cloneMoves) { foreach (var move in _moves) { clonedBoard._moves.AddLast(move); } } else if (_moves.Count >= 1) { clonedBoard._moves.AddLast(_moves.Last.Value); } clonedBoard.WinningMove = WinningMove; clonedBoard.State = State; _grid.CopyTo(clonedBoard._grid, 0); return(clonedBoard); }
private static bool CanWin(Player player, TicTacToeBoard board, IEnumerable <int> cells) { return(cells.All(cell => board.CellValue((byte)cell) == Player.Empty || board.CellValue((byte)cell) == player)); }