static public Board makeMove(int value, Treenode <int> gameTree, Treenode <Board> boardTree) { int index = -1; for (int i = 0; i < gameTree.ChildrenCount; i++) { Treenode <int> choice = gameTree.GetChild(i); for (int j = 0; j < choice.ChildrenCount; j++) { int result = choice.GetChild(j).Value; if (result == value) { index = i; break; } } if (index == i) { break; } } Board move = boardTree.GetChild(index).Value; return(move); }
public void Add(string word) { if (word == "") { throw new System.ArgumentException("Parameter cannot be empty string"); } Treenode node = root; foreach (char letter in word) { if (node.firstchild == null) { node.firstchild = new Treenode(letter); node = node.firstchild; } else { if (node.firstchild.c != letter) { if (node.firstchild.sibling != null) { node = node.firstchild.sibling; while (node.c != letter & node.sibling != null) { node = node.sibling; } if (node.c == letter) { } else if (node.sibling == null) { node.sibling = new Treenode(letter); node = node.sibling; } } else if (node.firstchild.sibling == null) { node.firstchild.sibling = new Treenode(letter); node = node.firstchild.sibling; } } else if (node.firstchild.c == letter) { node = node.firstchild; } } } if (node.leaf == false) { node.leaf = true; } }
public Treenode(char letter) { firstchild = null; sibling = null; leaf = false; c = letter; }
public Tree(T value) { if (value == null) { throw new ArgumentNullException("Cannot insert null value!"); } this.root = new Treenode <T>(value); }
public bool Contains(string word) { if (word == "") { return(false); } Treenode node = root; foreach (char letter in word) { if (node.firstchild == null) { return(false); } else { if (node.firstchild.c != letter) { if (node.firstchild.sibling != null) { node = node.firstchild.sibling; while (node.c != letter & node.sibling != null) { node = node.sibling; } if (node.c == letter) { } else if (node.sibling == null) { return(false); } } else if (node.firstchild.sibling == null) { return(false); } } else if (node.firstchild.c == letter) { node = node.firstchild; } } } if (node.leaf == true) { return(true); } return(false); }
public void AddChild(Treenode <T> child) { if (child == null) { throw new ArgumentException("Cannot insert null value!"); } if (child.hasParent) { throw new ArgumentException("The node already has a parent!"); } child.hasParent = true; this.children.Add(child); }
//反序列化 public TreeNode Deserialize(Treenode root, List <int> stream) { if (stream[0] == null) { stream.RemoveAt(0); return(null); } root = new TreeNode(stream.RemoveAt(0)); root.left = Deserialize(root.left, stream); root.right = Deserialize(root.right, stream); return(root); }
static public int Minimax(int depth, Treenode <int> root, bool isMaxPlayer, int alpha, int beta) { if (depth == 2) { return(root.Value); } if (isMaxPlayer) { int best = MIN; for (int i = 0; i < root.ChildrenCount; i++) { int val = Minimax(depth + 1, root.GetChild(i), false, alpha, beta); best = Math.Max(best, val); alpha = Math.Max(best, alpha); if (alpha >= beta) { break; } } return(best); } else { int best = MAX; for (int i = 0; i < root.ChildrenCount; i++) { int val = Minimax(depth + 1, root.GetChild(i), true, alpha, beta); best = Math.Min(best, val); beta = Math.Min(best, beta); if (alpha >= beta) { break; } } return(best); } }
static public Treenode <int> buildGameTree(Board board) { int init = Eval.evaluation(board.game_board); Tree <int> gameTree = new Tree <int>(init); List <Space> currentTiles = new List <Space>(); List <Space> oppTiles = new List <Space>(); foreach (Space s in board.occupied) { if (s.getTile().owner == -1 && s.getTile().isFlower()) { currentTiles.Add(s); } if (s.getTile().owner == 1 && s.getTile().isFlower()) { oppTiles.Add(s); } } foreach (Tile t1 in board.ai) { if (board.game_board[9][17].isEmpty()) { Board cpy = board.copy2(); cpy.place_tile(9, 17, t1); int value = Eval.evaluation(cpy.game_board); Treenode <int> child1 = new Treenode <int>(value); gameTree.Root.AddChild(child1); foreach (Tile t2 in board.player) { if (cpy.game_board[9][1].isEmpty()) { Board cpy2 = cpy.copy2(); cpy2.place_tile(9, 1, t2); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } if (cpy.game_board[1][9].isEmpty()) { Board cpy2 = cpy.copy2(); cpy2.place_tile(1, 9, t2); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } if (cpy.game_board[17][9].isEmpty()) { Board cpy2 = cpy.copy2(); cpy2.place_tile(17, 9, t2); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } } foreach (Space s2 in oppTiles) { List <Space> oppMoves = cpy.poss_moves(s2); foreach (Space o in oppMoves) { Board cpy2 = cpy.copy2(); cpy2.move_tile(s2.i, s2.j, o.i, o.j); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } } } if (board.game_board[9][1].isEmpty()) { Board cpy = board.copy2(); cpy.place_tile(9, 1, t1); int value = Eval.evaluation(cpy.game_board); Treenode <int> child1 = new Treenode <int>(value); gameTree.Root.AddChild(child1); foreach (Tile t2 in board.player) { if (cpy.game_board[9][17].isEmpty()) { Board cpy2 = cpy.copy2(); cpy2.place_tile(9, 17, t2); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } if (cpy.game_board[1][9].isEmpty()) { Board cpy2 = cpy.copy2(); cpy2.place_tile(1, 9, t2); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } if (cpy.game_board[17][9].isEmpty()) { Board cpy2 = cpy.copy2(); cpy2.place_tile(17, 9, t2); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } } foreach (Space s2 in oppTiles) { List <Space> oppMoves = cpy.poss_moves(s2); foreach (Space o in oppMoves) { Board cpy2 = cpy.copy2(); cpy2.move_tile(s2.i, s2.j, o.i, o.j); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } } } if (board.game_board[1][9].isEmpty()) { Board cpy = board.copy2(); cpy.place_tile(1, 9, t1); int value = Eval.evaluation(cpy.game_board); Treenode <int> child1 = new Treenode <int>(value); gameTree.Root.AddChild(child1); foreach (Tile t2 in board.player) { if (cpy.game_board[9][17].isEmpty()) { Board cpy2 = cpy.copy2(); cpy2.place_tile(9, 17, t2); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } if (cpy.game_board[9][1].isEmpty()) { Board cpy2 = cpy.copy2(); cpy2.place_tile(9, 1, t2); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } if (cpy.game_board[17][9].isEmpty()) { Board cpy2 = cpy.copy2(); cpy2.place_tile(17, 9, t2); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } } foreach (Space s2 in oppTiles) { List <Space> oppMoves = cpy.poss_moves(s2); foreach (Space o in oppMoves) { Board cpy2 = cpy.copy2(); cpy2.move_tile(s2.i, s2.j, o.i, o.j); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } } } if (board.game_board[17][9].isEmpty()) { Board cpy = board.copy2(); cpy.place_tile(17, 9, t1); int value = Eval.evaluation(cpy.game_board); Treenode <int> child1 = new Treenode <int>(value); gameTree.Root.AddChild(child1); foreach (Tile t2 in board.player) { if (cpy.game_board[9][17].isEmpty()) { Board cpy2 = cpy.copy2(); cpy2.place_tile(9, 17, t2); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } if (cpy.game_board[9][1].isEmpty()) { Board cpy2 = cpy.copy2(); cpy2.place_tile(9, 1, t2); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } if (cpy.game_board[1][9].isEmpty()) { Board cpy2 = cpy.copy2(); cpy2.place_tile(1, 9, t2); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } } foreach (Space s2 in oppTiles) { List <Space> oppMoves = cpy.poss_moves(s2); foreach (Space o in oppMoves) { Board cpy2 = cpy.copy2(); cpy2.move_tile(s2.i, s2.j, o.i, o.j); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } } } } foreach (Space s1 in currentTiles) { List <Space> moves = board.poss_moves(s1); foreach (Space m in moves) { Board cpy = board.copy2(); cpy.move_tile(s1.i, s1.j, m.i, m.j); int value = Eval.evaluation(cpy.game_board); Treenode <int> child1 = new Treenode <int>(value); gameTree.Root.AddChild(child1); foreach (Tile t2 in board.player) { if (cpy.game_board[9][17].isEmpty()) { Board cpy2 = cpy.copy2(); cpy2.place_tile(9, 17, t2); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } if (cpy.game_board[9][1].isEmpty()) { Board cpy2 = cpy.copy2(); cpy2.place_tile(9, 1, t2); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } if (cpy.game_board[1][9].isEmpty()) { Board cpy2 = cpy.copy2(); cpy2.place_tile(1, 9, t2); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } if (cpy.game_board[17][9].isEmpty()) { Board cpy2 = cpy.copy2(); cpy2.place_tile(17, 9, t2); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } } foreach (Space s2 in oppTiles) { List <Space> oppMoves = cpy.poss_moves(s2); foreach (Space o in oppMoves) { Board cpy2 = cpy.copy2(); cpy2.move_tile(s2.i, s2.j, o.i, o.j); int oppValue = Eval.evaluation(cpy2.game_board); Treenode <int> child2 = new Treenode <int>(oppValue); child1.AddChild(child2); } } } } return(gameTree.Root); }
static void Main() { int playerturn = -1; bool harmonyFormed = false; string temp = null; Board board = new Board(); int turn = 0; bool win = false; bool win2 = false; for (int i = 3; i < 6; i++) { for (int j = 0; j < 3; j++) { board.player.Add(new Tile(i, 1, "R")); board.ai.Add(new Tile(i, -1, "R")); } } for (int i = 3; i < 6; i++) { for (int j = 0; j < 3; j++) { board.player.Add(new Tile(i, 1, "W")); board.ai.Add(new Tile(i, -1, "W")); } } while (win == false) { if (!harmonyFormed) { playerturn *= -1; } if (turn == 0) { Console.WriteLine("Welcome to PaiSho!"); } Console.WriteLine("Turn #" + turn + ":"); board.print(); Console.ForegroundColor = ConsoleColor.White; if (turn == 0) { userget: Console.Write("Please choose from [R3, R4, R5, W3, W4, W5] to plant: "); string input = Console.ReadLine(); temp = input.ToLower(); if (input.ToLower() == "r3") { board.place_tile(17, 9, new Tile(3, 1, "R")); board.player.RemoveAt(0); board.ai.RemoveAt(0); } else if (input.ToLower() == "r4") { board.place_tile(17, 9, new Tile(4, 1, "R")); board.player.RemoveAt(3); board.ai.RemoveAt(3); } else if (input.ToLower() == "r5") { board.place_tile(17, 9, new Tile(5, 1, "R")); board.player.RemoveAt(6); board.ai.RemoveAt(6); } else if (input.ToLower() == "w3") { board.place_tile(17, 9, new Tile(3, 1, "W")); board.player.RemoveAt(9); board.ai.RemoveAt(9); } else if (input.ToLower() == "w4") { board.place_tile(17, 9, new Tile(4, 1, "W")); board.player.RemoveAt(12); board.ai.RemoveAt(12); } else if (input.ToLower() == "w5") { board.place_tile(17, 9, new Tile(5, 1, "W")); board.player.RemoveAt(15); board.ai.RemoveAt(15); } else { Console.WriteLine("Please enter a valid selection."); goto userget; } } else if (turn == 1) { if (temp == "r3") { board.place_tile(1, 9, new Tile(3, -1, "R")); } else if (temp == "r4") { board.place_tile(1, 9, new Tile(4, -1, "R")); } else if (temp == "r5") { board.place_tile(1, 9, new Tile(5, -1, "R")); } else if (temp == "w3") { board.place_tile(1, 9, new Tile(3, -1, "W")); } else if (temp == "w4") { board.place_tile(1, 9, new Tile(4, -1, "W")); } else if (temp == "w5") { board.place_tile(1, 9, new Tile(5, -1, "W")); } Console.Write("It's your opponent's turn, hit [enter] to continue... "); while (Console.ReadKey().Key != ConsoleKey.Enter) { ; } } else if (playerturn == 1) { choice: Console.Write("Would you like to [plant] or [move] a piece? "); string input = Console.ReadLine(); if (input.ToLower() == "move") { move(board); } else if (input.ToLower() == "plant") { plant(board); } else { Console.WriteLine("Invalid choice"); goto choice; } } else { Console.Write("It's your opponent's turn, hit [enter] to continue... "); Treenode <int> gameTree = AI.buildGameTree(board); Treenode <Board> boardTree = AI.buildBoardTree(board); int val = AI.Minimax(0, gameTree, true, MIN, MAX); board = AI.makeMove(val, gameTree, boardTree); while (Console.ReadKey().Key != ConsoleKey.Enter) { ; } } turn++; Console.Clear(); //win = board.win_condition(1); //win2 = board.win_condition(-1); } board.print(); Console.ForegroundColor = ConsoleColor.White; if (win) { Console.WriteLine("Congratulations, you've won!"); } else if (win2) { Console.WriteLine("Sorry, you lose!"); } }
public Treenode Parse(IList <Token> tokenList) { //Token Enumerator: Input of Tokens for the parser IEnumerator <Token> tokenItr = tokenList.GetEnumerator(); //Stack for the recursion Stack <ParserSymbol> parserStack = new Stack <ParserSymbol>(); Treenode root; Productions productions = new Productions(); //Set up recursion with Start NTS program Treenode current = productions.Factories[NotTerminals.program][Terminals.PROGRAM](); root = current; foreach (Symbol s in productions.ParseTable[NotTerminals.program][Terminals.PROGRAM].Reverse()) { parserStack.Push(new ParserSymbol(s, current)); } //Get 1. Token bool hasToken = tokenItr.MoveNext(); //Recursion until there are no more productions (until ENDPROGRAM is hit) //or until there are no more Tokens in the TokenList while (parserStack.Count != 0 && hasToken) { ParserSymbol currSymbol = parserStack.Pop(); if (currSymbol.IsNotTerminal) { //If the symbol is a NTS: Add all Symbols of the correct parse table entry to the recursion //(next iteration will begin with the first symbol of this parse table entry) if (!productions.ParseTable.ContainsKey(currSymbol.S.NotTerminal.Value) || !productions.ParseTable[currSymbol.S.NotTerminal.Value].ContainsKey(tokenItr.Current.Terminal)) { //Throw a GrammarException if the needed parse table entry does not exist (eta entries count as existing entries) throw new GrammarException(String.Format("Row: {0} Col: {1} Msg: Unexpected token: {2}", tokenItr.Current.Row, tokenItr.Current.Column, tokenItr.Current)); } Symbol[] production = productions.ParseTable[currSymbol.S.NotTerminal.Value][tokenItr.Current.Terminal]; current = productions.Factories[currSymbol.S.NotTerminal.Value][tokenItr.Current.Terminal](); foreach (Symbol s in production.Reverse()) { parserStack.Push(new ParserSymbol(s, current)); } } else { //Add Token to the tree if (currSymbol.S.Terminal != tokenItr.Current.Terminal) { //Throw a GrammarException, if the token, which (grammatically) has to be the next token, is not the next token throw new GrammarException(String.Format("Row: {0} Col: {1} Msg: Unexpected token: {2}", tokenItr.Current.Row, tokenItr.Current.Column, tokenItr.Current)); } current = new Tokennode(tokenItr.Current); //Get next Token (The current Token was consumed) hasToken = tokenItr.MoveNext(); } currSymbol.ExecuteCallback(current); } //Throw an exception if there are too many or to few tokens to consume if (parserStack.Count != 0) { throw new GrammarException("Unexpected end of program"); } if (hasToken) { throw new GrammarException(String.Format("Row: {0} Col: {1} Msg: Invalid token after end of program: {2}", tokenItr.Current.Row, tokenItr.Current.Column, tokenItr.Current)); } return(root); }
public Treenode() { firstchild = null; sibling = null; leaf = false; }
public static Treenode parse(int index, Treenode parent, string query, string[] elements, int operand) { Treenode node = new Treenode(); string[] array1 = query.Split('['); if (array1[0].Equals("sel")) { node.NodeType = Treenode.Type.sel; string[] array2 = array1[1].Split(']'); node.Condition = new CompositeCondition(array2[0]); node.Parent = parent; if (parent != null) { if (operand == 1) parent.Operand1 = node; else parent.Operand2 = node; } parent = node; parse(index + 1, parent, elements[index + 1], elements, 1); } else if (array1[0].Equals("proj")) { node.NodeType = Treenode.Type.proj; string[] array2 = array1[1].Split(']'); node.ProjectionColl = new ProjectionCollection(array2[0]); node.Parent = parent; if (parent != null) { if (operand == 1) parent.Operand1 = node; else parent.Operand2 = node; } parent = node; parse(index + 1, parent, elements[index + 1], elements, 1); } else if (array1[0].Equals("join")) { node.NodeType = Treenode.Type.join; string[] array2 = array1[1].Split(']'); node.Condition = new CompositeCondition(array2[0]); node.Parent = parent; if (parent != null) { if (operand == 1) parent.Operand1 = node; else parent.Operand2 = node; } int Operand2index = -1; int rightbrackets = 0; int leftbrackets = 0; for (int i = index; i < elements.Length; i++) { rightbrackets += elements[i].Length - elements[i].ToLower().Replace(")", String.Empty).Length; leftbrackets++; if (rightbrackets >= leftbrackets && rightbrackets > 0) { Operand2index = i; break; } else if (rightbrackets == leftbrackets - 1 && rightbrackets > 0) { Operand2index = i + 1; break; } } parent = node; parse(index + 1, parent, elements[index + 1], elements, 1); parse(Operand2index, parent, elements[Operand2index], elements, 2); } else { node.NodeType = Treenode.Type.relation; node.RelationName = query.Replace(")", String.Empty); node.Parent = parent; if (parent != null) { if (operand == 1) parent.Operand1 = node; else parent.Operand2 = node; } } return node; }
public bool Contains(string word) { if (word == "") { return(false); } Treenode node = root; foreach (char letter in word.Substring(0, word.Length - 1)) { if (node.firstchild == null) { return(false); } else { if (node.firstchild.c != letter || node.firstchild.leaf == true) { if (node.firstchild.sibling != null) { node = node.firstchild.sibling; while ((node.c != letter | node.leaf == true) & node.sibling != null) { node = node.sibling; } if (node.c == letter) { } else if (node.sibling == null) { return(false); } } else if (node.firstchild.sibling == null) { return(false); } } else if (node.firstchild.c == letter) { node = node.firstchild; } } } char lastletter = word[word.Length - 1]; node = node.firstchild; if (node.c == lastletter && node.leaf == true) { return(true); } while (node.c != lastletter || node.leaf != true) { if (node.sibling == null) { return(false); } node = node.sibling; } return(true); }
public void ExecuteCallback(Treenode contains) { S.Callback(Node, contains); }
public ParserSymbol(Symbol symbol, Treenode node) { this.S = symbol; this.Node = node; }