public Node(Move move, Node parent, State state, Deck deck) { this.state = state; this.generatingMove = move; this.parent = parent; this.results = 0; this.visits = 0; this.children = new List<Node>(); this.untriedMoves = state.GetMoves(deck); }
private State ApplyUp() { for (int j = GameEngine.ROWS - 2; j >= 0; j--) { for (int i = 0; i < GameEngine.COLUMNS; i++) { if (grid[i][j] != 0) { if (grid[i][j + 1] == 0) { // next cell is empty, just move the tile MoveTile(i, j, i, j + 1); this.columnsOrRowsWithMovedTiles.Add(i); } else if ((grid[i][j] == 1 && grid[i][j + 1] == 2) || (grid[i][j] == 2 && grid[i][j + 1] == 1) || (grid[i][j] > 2 && grid[i][j] == grid[i][j + 1])) { // tiles are mergeable MergeTiles(i, j, i, j + 1); this.columnsOrRowsWithMovedTiles.Add(i); } } } } this.generatingMove = new PlayerMove(DIRECTION.UP); return this; }
private State ApplyRight() { for (int i = GameEngine.COLUMNS - 2; i >= 0; i--) { for (int j = 0; j < GameEngine.ROWS; j++) { if (grid[i][j] != 0) { if (grid[i + 1][j] == 0) { // next cell is empty, just move the tile MoveTile(i, j, i + 1, j); this.columnsOrRowsWithMovedTiles.Add(j); } else if ((grid[i][j] == 1 && grid[i + 1][j] == 2) || (grid[i][j] == 2 && grid[i + 1][j] == 1) || (grid[i][j] > 2 && grid[i][j] == grid[i + 1][j])) { // tiles are mergeable MergeTiles(i, j, i + 1, j); this.columnsOrRowsWithMovedTiles.Add(j); } } } } this.generatingMove = new PlayerMove(DIRECTION.RIGHT); return this; }
// Applies the move and returns the result state internal State ApplyMove(Move move) { if (move is PlayerMove) { State result = new State(BoardHelper.CloneGrid(this.grid), GameEngine.COMPUTER); if (((PlayerMove)move).Direction == DIRECTION.LEFT) { return result.ApplyLeft(); } else if (((PlayerMove)move).Direction == DIRECTION.RIGHT) { return result.ApplyRight(); } else if (((PlayerMove)move).Direction == DIRECTION.UP) { return result.ApplyUp(); } else if (((PlayerMove)move).Direction == DIRECTION.DOWN) { return result.ApplyDown(); } return result; } else if (move is ComputerMove) { State result = new State(BoardHelper.CloneGrid(this.grid), GameEngine.PLAYER); result.grid[((ComputerMove)move).Position.Item1][((ComputerMove)move).Position.Item2] = ((ComputerMove)move).Card; result.generatingMove = (ComputerMove)move; return result; } else { throw new Exception(); } }
// adds a child node to the list of children // after exploring a move - removes the move from untried public Node AddChild(Move move, State state, Deck deck) { Node child = new Node(move, this, state, deck); this.untriedMoves.Remove(move); this.children.Add(child); return child; }