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>(); }
// 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; }