public int[] makeNextMove(Grid board) { PriorityQueue<MoveNode> q = new PriorityQueue<MoveNode>(); q.initialize(expandNodes(board, null, 1)); MoveNode current = q.Dequeue(); while ((current.Ply < depth) && !(q.isEmpty)) { MoveNode next = q.Dequeue(); if(next.Rank> current.Rank) { current = next; MoveNode[] expnd = expandNodes(current.Board, current, (current.Ply + 1)); if(expnd.Length > 0) q.EnqueueArray(expnd); } } while(current.Ply > 1) { current = current.PreviousMove; } //this will be the best move to make after expanding down to depth plys. int[] result = { current.Group.X, current.Group.Y }; Console.WriteLine("rank: " + current.Rank); return result; }
public double rankGrid(Grid board) { double rank = 0; int largest = 0; PriorityQueue<Group> moves = board.calculateGroupsQueue(); if(!moves.isEmpty) largest = moves.Dequeue().Bubbles; int groups = 0; int singles = 0; int remaining = 0; while (!(moves.isEmpty) && (moves.Peek().Bubbles > 1)) { groups++; remaining += moves.Dequeue().Bubbles; } singles = moves.Count; remaining += singles; if(board.checkWin()) rank = (board.X * board.X) *1000; else if (board.checkLocked()) rank = 0 - singles; else rank = (remaining - singles) + largest; return rank; }
public MoveNode(int x, int y, double rank) { group = new Group(x, y, 0); this.rank = rank; ply = 0; board = null; previousMove = null; }
public MoveNode(Group m, double rank, int ply, Grid board, MoveNode last) { this.group = m; this.rank = rank; this.ply = ply; this.board = board; previousMove = last; }
public MoveNode() { group = new Group(0, 0, 0); rank = 0; ply = 0; board = null; previousMove = null; }
public MoveNode[] expandNodes(Grid board, MoveNode last, int ply) { List<MoveNode> nodes = new List<MoveNode>(); List<Group> moves = board.calculateGroups(); foreach(Group move in moves) { Grid b = board.copy(); if (b.checkMove(move.X, move.Y)) { b.removeGroup(move.X, move.Y); b.compressGrid(); nodes.Add(new MoveNode(move, rankGrid(b), ply, b, last)); } } return nodes.ToArray(); }
public static void newGame(Grid game) { bool GameLoop = true; while (GameLoop) { game.displayGrid(); Console.Write("Enter Move ( as 'x y' ): "); string move = Console.ReadLine(); string[] coord = move.Split(' '); Int32 val; if((coord.Length==2) && (Int32.TryParse(coord[0], out val) && Int32.TryParse(coord[1], out val)) && game.checkMove(Int32.Parse(coord[0]), Int32.Parse(coord[1]))) { game.removeGroup(Int32.Parse(coord[0]), Int32.Parse(coord[1])); game.Logger.addLog("MOVE: (" + coord[0] + "," + coord[1] + ")"); game.Logger.addLog(" --Points: " + game.calculatePoints()); } else { Console.WriteLine("Invalid Move! Try again!"); } game.compressGrid(); if (game.checkWin()) { GameLoop = false; Console.WriteLine("You Win!"); game.Logger.addLog("GAME WIN: All bubbles eliminated"); game.Logger.addLog("FINAL POINTS: " + game.Points); game.Logger.close(); Console.ReadLine(); } else if (game.checkLocked()) { GameLoop = false; //display final game board showing locked game game.displayGrid(); Console.WriteLine("You Lose!"); game.Logger.addLog("GAME LOSS: Gameboard locked!"); game.Logger.addLog("FINAL POINTS: " + game.Points); game.Logger.close(); Console.ReadLine(); } } }
public Grid copy() { string[,] gridCopy = new string[x+2, x+2]; Array.Copy(grid, gridCopy, ((x+2) * (x+2))); Grid copy = new Grid(x, currentRemoved, points, gridCopy); return copy; }
public MoveNode solveTree(Grid board) { MoveNode result = null; Stack<MoveNode> s = new Stack<MoveNode>(); MoveNode[] expnd = expandNodes(board, null, 1); for(int i=0; i<expnd.Length; i++) { s.Push(expnd[i]); } MoveNode current = s.Pop(); while(!(current.Board.checkWin()) && (s.Count > 0)) { expnd = expandNodes(current.Board, current, current.Ply+1); for(int j=0; j<expnd.Length; j++) { s.Push(expnd[j]); } current = s.Pop(); } if(current.Board.checkWin()) result = current; return result; }
public static void newAIGame(Grid game) { bool GameLoop = true; bool list = false; AI blue = new AI(50); Stack<MoveNode> moves = new Stack<MoveNode>(); if((game.Colors <= 5 && game.X <7) || (game.Colors < 4 && game.X <= 15)) { MoveNode move = blue.solveTree(game); if(move != null) { list = true; while(move != null) { moves.Push(move); move = move.PreviousMove; } } } while (GameLoop) { int[] nextMove; game.displayGrid(); if(list) { nextMove = new int[2]; MoveNode cur = moves.Pop(); nextMove[0] = cur.Group.X; nextMove[1] = cur.Group.Y; } else { nextMove = blue.makeNextMove(game); } Console.WriteLine("AI makes the move (" + nextMove[0] + ", " + nextMove[1] + ")"); game.removeGroup(nextMove[0], nextMove[1]); game.Logger.addLog("MOVE: (" + nextMove[0] + "," + nextMove[1] + ")"); game.Logger.addLog(" --Points: " + game.calculatePoints()); game.compressGrid(); if (game.checkWin()) { GameLoop = false; Console.WriteLine("You Win!"); game.Logger.addLog("GAME WIN: All bubbles eliminated"); game.Logger.addLog("FINAL POINTS: " + game.Points); game.Logger.close(); Console.ReadLine(); } else if (game.checkLocked()) { GameLoop = false; //display final game board showing locked game game.displayGrid(); Console.WriteLine("You Lose!"); game.Logger.addLog("GAME LOSS: Gameboard locked!"); game.Logger.addLog("FINAL POINTS: " + game.Points); game.Logger.close(); Console.ReadLine(); } } }