/// <summary> /// Constructs a new TicTacToe game using the specified player's pieces. /// /// </summary> /// <param name="player1Piece">Player one's piece</param> /// <param name="player2Piece">Player two's piece</param> public TicTacToeGame(Board.Pieces player1Piece, Board.Pieces player2Piece) { this.player1Piece = player1Piece; this.player2Piece = player2Piece; board = new Board(); moves = new Stack <TicTacToeMove>(); }
/// <summary> /// Constructs a new TicTacToe game using the specified player's pieces. /// /// </summary> /// <param name="player1Piece">Player one's piece</param> /// <param name="player2Piece">Player two's piece</param> public TicTacToeGame(Board.Pieces player1Piece, Board.Pieces player2Piece) { this.player1Piece = player1Piece; this.player2Piece = player2Piece; board = new Board(); moves = new Stack<TicTacToeMove>(); }
/// <summary> /// Constructs a new Node /// </summary> /// <param name="b">The board that the Node will use to evaluate itself and generate its children</param> /// <param name="parent">The parent of this node</param> /// <param name="move">The move from the parent's board that generated this node's board</param> public Node(Board b, Node parent, TicTacToeMove move) { this.board = b; this.parent = parent; this.move = move; if (parent != null) myPiece = Board.GetOponentPiece(parent.MyPiece); children = new List<Node>(); }
/// <summary> /// Constructs a new Node /// </summary> /// <param name="b">The board that the Node will use to evaluate itself and generate its children</param> /// <param name="parent">The parent of this node</param> /// <param name="move">The move from the parent's board that generated this node's board</param> public Node(Board b, Node parent, TicTacToeMove move) { this.board = b; this.parent = parent; this.move = move; if (parent != null) { myPiece = Board.GetOponentPiece(parent.MyPiece); } children = new List <Node>(); }
// draws each piece on the board onto the panel private void DrawPieces(Graphics g) { for (int i = 0; i < game.Rows * game.Columns; i++) { Board.Pieces piece = game.GameBoard.GetPieceAtPosition(i); if (piece == Board.Pieces.X || piece == Board.Pieces.O) { DrawPiece(piece, g, i); } } }
// returns the Player who has the specified piece protected TicTacToeGame.Players GetPlayerWhoHasPiece(Board.Pieces piece) { if (piece == player1Piece) { return(Players.Player1); } else { return(Players.Player2); } }
/// <summary> /// Returns the piece representing the opponent /// </summary> /// <param name="yourPiece">The piece representing the player</param> /// <returns></returns> public static Board.Pieces GetOponentPiece(Board.Pieces yourPiece) { if (yourPiece == Board.Pieces.X) { return(Board.Pieces.O); } else if (yourPiece == Board.Pieces.O) { return(Board.Pieces.X); } else { throw new Exception("Invalid Piece!"); } }
/// <summary> /// Draws the specified piece on the board in the designated position. /// Position 0 is the upper left square on the board and position 8 is the lower right /// corner square on the board /// </summary> /// <param name="p">The piece we wish to draw</param> /// <param name="g">The graphics object we are drawing on</param> /// <param name="position">The position on the board to draw at</param> private void DrawPiece(Board.Pieces p, Graphics g, int position) { float pixelsPerRow = ticTacToePanel.Width / game.Rows; float pixelsPerColumn = ticTacToePanel.Height / game.Columns; PointF point = GetPieceDrawingCoordsFromPosition(position); Font f = new Font("Arial", 40); if (p == Board.Pieces.X) { g.DrawString("X", f, Brushes.Blue, point); } else if (p == Board.Pieces.O) { g.DrawString("O", f, Brushes.Red, point); } }
/// <summary> /// Evaluates the favorability of the current board configuration for maxPiece. Higher values /// indicate better configuration for maxPiece /// </summary> /// <param name="b">the game board to evaluate</param> /// <param name="maxPiece">the piece representing MAX</param> /// <returns></returns> public double Evaluate(Board b, Board.Pieces maxPiece) { functionCalls++; if (b.HasWinner()) { if (b.WinningPiece == maxPiece) { return(double.MaxValue); } else { return(double.MinValue); } } double maxValue = EvaluatePiece(b, maxPiece); double minValue = EvaluatePiece(b, Board.GetOponentPiece(maxPiece)); return(maxValue - minValue); }
// over all rows sums the number of pieces in the row if // the specified piece can still win in that row i.e. the row // does not contain an opponent's piece private double EvaluateColumns(Board b, Board.Pieces p) { int cols = b.Columns; int rows = b.Rows; double score = 0.0; int count; // check the rows for (int j = 0; j < b.Columns; j++) { count = 0; bool rowClean = true; for (int i = 0; i < b.Columns; i++) { Board.Pieces boardPiece = b.GetPieceAtPoint(i, j); if (boardPiece == p) { count++; } else if (boardPiece == Board.GetOponentPiece(p)) { rowClean = false; break; } } // if we get here then the row is clean (an open row) if (rowClean && count != 0) { score += count; //Math.Pow(count, count); } } return(score); }
// over both diagonals sums the number of pieces in the diagonal if // the specified piece can still win in that diagonal i.e. the diagonal // does not contain an opponent's piece private double EvaluateDiagonals(Board b, Board.Pieces p) { // go down and to the right diagonal first int count = 0; bool diagonalClean = true; double score = 0.0; for (int i = 0; i < b.Columns; i++) { Board.Pieces boardPiece = b.GetPieceAtPoint(i, i); if (boardPiece == p) { count++; } if (boardPiece == Board.GetOponentPiece(p)) { diagonalClean = false; break; } } if (diagonalClean && count > 0) { score += count;// Math.Pow(count, count); } // now try the other way int row = 0; int col = 2; count = 0; diagonalClean = true; while (row < b.Rows && col >= 0) { Board.Pieces boardPiece = b.GetPieceAtPoint(row, col); if (boardPiece == p) { count++; } if (boardPiece == Board.GetOponentPiece(p)) { diagonalClean = false; break; } row++; col--; } if (count > 0 && diagonalClean) { score += count; } return(score); }
// sums up all the possible ways to win for the specified board piece private double EvaluatePiece(Board b, Board.Pieces p) { return(EvaluateRows(b, p) + EvaluateColumns(b, p) + EvaluateDiagonals(b, p)); }
/// <summary> /// Constructs a TicTacToeMove /// </summary> /// <param name="position">The position to move to</param> /// <param name="piece">The piece that is moving</param> public TicTacToeMove(int position, Board.Pieces piece) { this.Position = position; this.Piece = piece; }
public Player(string name, Board.Pieces p) { this.Name = name; this.PlayerPiece = p; }
public HumanPlayer(string name, Board.Pieces p, TicTacToeForm tttf) : base(name, p) { this.ticTacToeForm = tttf; }
/// <summary> /// Constructs a new computer player /// </summary> /// <param name="name">The name of the player</param> /// <param name="p">The piece the player is using</param> /// <param name="searchDepth">The depth to search for moves in the game tree.</param> public ComputerPlayer(string name, Board.Pieces p, int searchDepth) : base(name, p) { this.SearchDepth = searchDepth; }
/// <summary> /// Constructs a new computer player. The DEFAULT_SEARCH_DEPTH is used /// </summary> /// <param name="name">The name of the player</param> /// <param name="p">The piece this player is using in the came</param> public ComputerPlayer(string name, Board.Pieces p) : this(name, p, DEFAULT_SEARCH_DEPTH) { }