public AppearNode(GameBoard board, Direction moveDirection, PositionEvaluator evaluator) { instances++; this.board = board.move(moveDirection); this.lastDirection = moveDirection; this.evaluator = evaluator; }
public double score(GameBoard board) { double score = 100; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (board.squares[i, j] > 0) score -= i + j; } } return score; }
public double score(GameBoard board) { double score = 1; double last = 10000000; int i; for (i = 0; i < 16; i++) { var square = snakeOrder.ElementAt(i); double current = board.squares[square.Item1, square.Item2]; if (current <= last && current > 0) { score += current; if (i == 4 && current == last) { i++; break; } last = current; } else if (i == 0 || current == 0) { break; } else //we are blocking the snake. If there's another way to increase the tail, we should award points for that { var tailSquare = snakeOrder.ElementAt(i - 1); for (int j = i; j < 16; j++) { var trySquare = snakeOrder.ElementAt(j); if (Math.Abs(trySquare.Item1 - tailSquare.Item1) + Math.Abs(trySquare.Item2 - tailSquare.Item2) == 1) { int adjValue = board.squares[trySquare.Item1, trySquare.Item2]; if (adjValue > 0 && adjValue <= last) { score += adjValue/2; } } } break; } } int badScore = 0; for (; i < 16; i++) { var square = snakeOrder.ElementAt(i); badScore += board.squares[square.Item1, square.Item2] * 8; } score -= Math.Min(badScore, score / 8); return score; }
public Direction getDirection(GameBoard board) { double bestScore = -1; Direction bestDirection = Direction.None; foreach (Direction tryDirection in (Direction[])Enum.GetValues(typeof(Direction))) { if (tryDirection == Direction.None) continue; GameBoard newBoard = board.move(tryDirection); if (newBoard.Equals(board)) continue; double newScore = evaluator.score(newBoard); if (newScore > bestScore) { bestScore = newScore; bestDirection = tryDirection; } } return bestDirection; }
public double score(GameBoard board) { double score = board.score(); double last = 10000000; for (int i = 0; i < 16; i++) { var square = snakeOrder.ElementAt(i); double current = board.squares[square.Item2, square.Item1]; if (current > last) { score -= current * Math.Log(current - last) / Math.Log(2); } if (current == 0 && last > 0 && i > 0) { score -= last * Math.Log(last) / Math.Log(2) / 2; } last = current; } return score; }
public void compressRight() { GameBoard board = new GameBoard(new int[,]{ {2, 2, 2, 0}, {0, 2, 2 , 0}, {0, 0, 4 , 8}, {0, 0, 4 , 4} } ); GameBoard actual = board.move(Direction.Right); GameBoard expected = new GameBoard(new int[,]{ {0, 0, 2, 4}, {0, 0, 0, 4}, {0, 0, 4 , 8}, {0, 0, 0 , 8} } ); Assert.IsTrue(expected.Equals(actual)); }
public void compressLeft() { GameBoard board = new GameBoard(new int[,]{ {2, 2, 0, 0}, {0, 2, 2 , 2}, {0, 0, 4 , 8}, {0, 0, 4 , 4} } ); GameBoard actual = board.move(Direction.Left); GameBoard expected = new GameBoard(new int[,]{ {4, 0, 0, 0}, {4, 2, 0 , 0}, {4, 8, 0 , 0}, {8, 0, 0 , 0} } ); Assert.IsTrue(expected.Equals(actual)); }
public Direction getDirection(GameBoard board) { Stopwatch timer = Stopwatch.StartNew(); int level = 0; rootNode = new MoveNode(board, evaluator); List<GameNode> bottomNodes = new List<GameNode>(); bottomNodes.Add(rootNode); while (timer.ElapsedMilliseconds < moveTimeInMs) { level++; List<GameNode> newNodes = new List<GameNode>(); foreach (GameNode node in bottomNodes) { node.calculateChildren(); newNodes.AddRange(node.getChildren()); } bottomNodes = newNodes; } Console.WriteLine(level); return rootNode.bestDirection(); }
public void compressDown() { GameBoard board = new GameBoard(new int[,]{ {2, 2, 0, 0}, {0, 2, 2 , 0}, {0, 2, 4 , 8}, {0, 0, 4 , 4} } ); GameBoard actual = board.move(Direction.Down); board.print(); GameBoard expected = new GameBoard(new int[,]{ {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 2, 2, 8}, {2, 4, 8, 4} } ); Assert.IsTrue(expected.Equals(actual)); }
public double score(GameBoard board) { int moves = 0; for (int i = 0; i < rollouts; i++) { MockController controller = new MockController(board); while (true) { moves++; var moveBoard = controller.getBoard(); Direction bestDirection = strategy.getDirection(moveBoard); if (bestDirection != Direction.None) { controller.move(bestDirection); } else { break; } } } return (double)moves / (double)rollouts; }
public Direction getDirection(GameBoard board) { if (!board.Equals(board.move(Direction.Up))) { return Direction.Up; } else if (!board.Equals(board.move(Direction.Left))) { return Direction.Left; } else if (!board.Equals(board.move(Direction.Right))) { return Direction.Right; } else if (!board.Equals(board.move(Direction.Down))) { return Direction.Down; } else { return Direction.None; } }
public MoveNode(GameBoard board, PositionEvaluator evaluator) { instances++; this.board = board; this.evaluator = evaluator; }
public MoveNode(GameBoard board, Square square, PositionEvaluator evaluator) { instances++; this.board = board.withNewSquare(square); this.evaluator = evaluator; }
public void append(GameBoard board) { gameLog.Add(board); }
public MockController() { board = new GameBoard(new int[4, 4]); board = board.randomNewTile(); board = board.randomNewTile(); }
public void move(Direction direction) { board = board.move(direction); board = board.randomNewTile(); }
public MockController(GameBoard inputBoard) { board = inputBoard; }