public static AbstractBoard.Player GetOponentPiece(AbstractBoard.Player yourPiece) { if (yourPiece == AbstractBoard.Player.X) return AbstractBoard.Player.O; else if (yourPiece == AbstractBoard.Player.O) return AbstractBoard.Player.X; else throw new Exception("Invalid Piece!"); }
protected AbstractBoard.Player opponentPiece; // the opponents piece public Node(AbstractBoard b, Node parent, Space move) { this.board = b.Clone(); this.parent = parent; this.move = move; if (parent != null) myPiece = AbstractBoard.GetOponentPiece(parent.myPiece); children = new List<Node>(); }
private void mediumBoard_Load(object sender, EventArgs e) { gb = new _4by4(); this.LoadBoard(); cell1.Enabled = true; cell2.Enabled = true; cell3.Enabled = true; cell4.Enabled = true; cell5.Enabled = true; cell6.Enabled = true; cell7.Enabled = true; cell8.Enabled = true; cell9.Enabled = true; }
private void GUI_Load(object sender, EventArgs e) { gb = new tictactoe(); this.LoadBoard(); cell1.Enabled = true; cell2.Enabled = true; cell3.Enabled = true; cell4.Enabled = true; cell5.Enabled = true; cell6.Enabled = true; cell7.Enabled = true; cell8.Enabled = true; cell9.Enabled = true; }
// create virtual board //setup virtual game and play. //sub nodes for each tree . //recursive method public static Space GetBestMove(AbstractBoard gb, AbstractBoard.Player p) { Space? bestSpace = null;List<Space> openSpaces = gb.OpenSquares; AbstractBoard newBoard; for (int i = 0; i < openSpaces.Count; i++) { newBoard = gb.Clone(); Space newSpace = openSpaces[i]; newBoard[newSpace.X, newSpace.Y] = p; if (newBoard.Winner == AbstractBoard.Player.Open && newBoard.OpenSquares.Count > 0) { Space tempMove = GetBestMove(newBoard, ((AbstractBoard.Player)(-(int)p))); newSpace.Rank = tempMove.Rank; } else { if (newBoard.Winner == AbstractBoard.Player.Open) newSpace.Rank = 0; else if (newBoard.Winner == AbstractBoard.Player.X) newSpace.Rank = -1; else if (newBoard.Winner == AbstractBoard.Player.O) newSpace.Rank = 1; } //If the new move is better than our previous move, take it if (bestSpace == null || (p == AbstractBoard.Player.X && newSpace.Rank < ((Space)bestSpace).Rank) || (p == AbstractBoard.Player.O && newSpace.Rank > ((Space)bestSpace).Rank)) { bestSpace = newSpace; } } return (Space)bestSpace; }
private double EvaluateDiagonals(AbstractBoard b, AbstractBoard.Player 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++) { AbstractBoard.Player boardPiece = b[i, i]; if (boardPiece == p) count++; if (boardPiece == AbstractBoard.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) { AbstractBoard.Player boardPiece = b[row, col]; if (boardPiece == p) count++; if (boardPiece == AbstractBoard.GetOponentPiece(p)) { diagonalClean = false; break; } row++; col--; } if (count > 0 && diagonalClean) score += count; return score; }
private double EvaluateColumns(AbstractBoard b, AbstractBoard.Player p) { int cols = b.COLUMNS; int rows = b.ROWS; double score = 0.0; int count; for (int j = 0; j < b.COLUMNS; j++) { count = 0; bool rowClean = true; for (int i = 0; i < b.COLUMNS; i++) { AbstractBoard.Player boardPiece = b[i, j]; if (boardPiece == p) count++; else if (boardPiece == AbstractBoard.GetOponentPiece(p)) { rowClean = false; break; } } if (rowClean && count != 0) score += count; //Math.Pow(count, count); } return score; }
public double evaluateRows(AbstractBoard b, AbstractBoard.Player p) { int cols = b.COLUMNS; int rows = b.ROWS; double score = 0.0; int count; // check the rows for (int i = 0; i < b.ROWS; i++) { count = 0; bool rowClean = true; for (int j = 0; j < b.COLUMNS; j++) { AbstractBoard.Player boardPiece = b[i, j]; if (boardPiece == p) count++; else if (boardPiece == AbstractBoard.GetOponentPiece(p)) { rowClean = false; break; } } // if we get here then the row is clean (an open row) if (rowClean && count != 0) score += count; } return score; }
// sums up all the possible ways to win for the specified board piece private double EvaluatePiece(AbstractBoard b, AbstractBoard.Player p) { return evaluateRows(b, p) + EvaluateColumns(b, p) + EvaluateDiagonals(b, p); }
public double Evaluate(AbstractBoard b, AbstractBoard.Player maxPiece) { functionCalls++; if (b.Winner == AbstractBoard.Player.Open || b.Winner == AbstractBoard.Player.O || b.Winner == AbstractBoard.Player.X) { if (b.Winner == maxPiece) return double.MaxValue; else return double.MinValue; } double maxValue = EvaluatePiece(b, maxPiece); double minValue = EvaluatePiece(b, AbstractBoard.GetOponentPiece(maxPiece)); return maxValue - minValue; }