public RandomComputerTicTacToePlayer( TicTacToePiece piece, IRandomNumberPicker numberPicker, string name ) { _piece = piece; _numberPicker = numberPicker; Name = name; }
private bool AreCellsAvailable(TicTacToePiece[,] cells) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (cells[i, j] == TicTacToePiece.None) return true; } } return false; }
private static int DoHumanMove(TicTacToeBoard board, TicTacToePiece human) { var location = -1; var legalMoves = new HashSet <int>(board.GetLegalMoves()); var availableMoves = string.Join(",", board.GetLegalMoves().Select(m => $"{m}")); while (!legalMoves.Contains(location)) { Console.WriteLine($"please, enter where to put next {human} [{availableMoves}]"); location = int.TryParse(new string(Console.ReadKey().KeyChar, 1), out var loc) ? loc : -1; } return(location); }
public TicTacToePiece[,] Cells() { var result = new TicTacToePiece[3, 3]; for (var i = 0; i < 3; i++) { for (var j = 0; j < 3; j++) { result[i, j] = _cells[i, j]; } } return result; }
private static void PrintResult(int moveNumber, TicTacToeBoard board, TicTacToePiece human) { Console.Clear(); Console.WriteLine($"After {moveNumber} steps"); Console.WriteLine(board.ToString()); if (board.IsWin()) { var winnerName = board.GetTurn() == human ? "computer" : "human"; Console.WriteLine($"{winnerName} wins!"); } else { Console.WriteLine($"It's a draw!"); } }
public TicTacToeBoard(TicTacToePiece r0c0, TicTacToePiece r0c1, TicTacToePiece r0c2, TicTacToePiece r1c0, TicTacToePiece r1c1, TicTacToePiece r1c2, TicTacToePiece r2c0, TicTacToePiece r2c1, TicTacToePiece r2c2) { this.R0C0 = r0c0; this.R0C1 = r0c1; this.R0C2 = r0c2; this.R1C0 = r1c0; this.R1C1 = r1c1; this.R1C2 = r1c2; this.R2C0 = r2c0; this.R2C1 = r2c1; this.R2C2 = r2c2; }
public void AddPieceToBoard(TicTacToePiece piece, int x, int y) { if (x > 2 || x < 0) { throw new ArgumentException("X value must be between 0 and 2"); } if (y > 2 || y < 0) { throw new ArgumentException("X value must be between 0 and 2"); } if (piece == TicTacToePiece.None) { throw new ArgumentException("Piece must be either x or o"); } _cells[x,y] = piece; }
private bool CheckIfPlayerWins(TicTacToePiece player) { var rowMatch = _positions.Select((p, i) => (p, i)) .GroupBy(t => t.i / 3, t => t.p) //group cells of the same row .Select(g => g.All(p => p == player)) .Any(g => g); if (rowMatch) { return(true); } var colMatch = _positions.Select((p, i) => (p, i)) .GroupBy(t => t.i % 3, t => t.p) //group cells of the same col .Select(g => g.All(p => p == player)) .Any(g => g); if (colMatch) { return(true); } var mainDiagonalMatch = new[] { 0, 4, 8 }.All(i => _positions[i] == player); if (mainDiagonalMatch) { return(true); } var secondaryDiagonalMatch = new[] { 2, 4, 6 }.All(i => _positions[i] == player); if (secondaryDiagonalMatch) { return(true); } return(false); }
private Tuple<int, int> PickMove(TicTacToePiece[,] cells) { var availableMoves = new List<Tuple<int,int>>(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (cells[i, j] == TicTacToePiece.None) { availableMoves.Add(new Tuple<int, int>(i, j)); } } } if (availableMoves.Count == 0) { throw new Exception("No available moves"); } var num = _numberPicker.Pick(1, availableMoves.Count); return availableMoves[num-1]; }
public TicTacToeBoard(TicTacToePiece[] positions, TicTacToePiece turn) { _positions = positions; _turn = turn ?? throw new ArgumentNullException(nameof(turn)); }
//the very first turn game state public TicTacToeBoard() { _positions = Enumerable.Range(1, CellAmount).Select(_ => TicTacToePiece.E).ToArray(); //the field is empty _turn = TicTacToePiece.X; //first player puts X }
public TicTacToeBoard() { BoardPieces = new TicTacToePiece[3, 3]; }
private TicTacToePiece IsWinOnDiagonal(TicTacToePiece[,] cells) { if (IsWinOnDiagonal(cells, TicTacToePiece.O)) { return TicTacToePiece.O; } if (IsWinOnDiagonal(cells, TicTacToePiece.X)) { return TicTacToePiece.X; } return TicTacToePiece.None; }
private bool IsWon(TicTacToePiece[,] cells) { return !(IsWinOnDiagonal(cells) == TicTacToePiece.None && IsWinOnVertical(cells) == TicTacToePiece.None && IsWinOnHorizontal(cells) == TicTacToePiece.None); }
private bool IsWinOnVertical(TicTacToePiece[,] cells, TicTacToePiece piece) { return (cells[0, 0] == piece && cells[1, 0] == piece && cells[2, 0] == piece) || (cells[0, 1] == piece && cells[1, 1] == piece && cells[2, 1] == piece) || (cells[0, 2] == piece && cells[1, 2] == piece && cells[2, 2] == piece); }