@Test public void testGetPossibleMoves() { double[] features = new double[16]; state = new State2048(features); List<Action2048> possibleMoves = state.getPossibleMoves(); Assert.assertTrue(possibleMoves.isEmpty()); features[0] = 1; state = new State2048(features); possibleMoves = state.getPossibleMoves(); Assert.assertTrue(possibleMoves.contains(Action2048.DOWN)); Assert.assertTrue(possibleMoves.contains(Action2048.RIGHT)); Assert.assertFalse(possibleMoves.contains(Action2048.LEFT)); Assert.assertFalse(possibleMoves.contains(Action2048.UP)); features[1] = 2; features[2] = 3; features[3] = 4; state = new State2048(features); possibleMoves = state.getPossibleMoves(); Assert.assertTrue(possibleMoves.contains(Action2048.DOWN)); Assert.assertFalse(possibleMoves.contains(Action2048.RIGHT)); Assert.assertFalse(possibleMoves.contains(Action2048.LEFT)); Assert.assertFalse(possibleMoves.contains(Action2048.UP)); features[1] = 1; state = new State2048(features); possibleMoves = state.getPossibleMoves(); Assert.assertTrue(possibleMoves.contains(Action2048.RIGHT)); }
public List<Action2048> getPossibleMoves() { boolean[] set = new boolean[4]; ArrayList<Action2048> moves = new ArrayList<>(4); for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE; col++) { if (board[row][col] > 0) { continue; } if (!set[Action2048.RIGHT.id()]) { for (int col2 = 0; col2 < col; col2++) { if (board[row][col2] > 0) { set[Action2048.RIGHT.id()] = true; moves.add(Action2048.RIGHT); break; } } } if (!set[Action2048.LEFT.id()]) { for (int col2 = col + 1; col2 < SIZE; col2++) { if (board[row][col2] > 0) { set[Action2048.LEFT.id()] = true; moves.add(Action2048.LEFT); break; } } } if (!set[Action2048.DOWN.id()]) { for (int row2 = 0; row2 < row; row2++) { if (board[row2][col] > 0) { set[Action2048.DOWN.id()] = true; moves.add(Action2048.DOWN); break; } } } if (!set[Action2048.UP.id()]) { for (int row2 = row + 1; row2 < SIZE; row2++) { if (board[row2][col] > 0) { set[Action2048.UP.id()] = true; moves.add(Action2048.UP); break; } } } if (moves.size() == 4) { return new ArrayList<>(moves); } } } if (!set[Action2048.RIGHT.id()] || !set[Action2048.LEFT.id()]) { for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE - 1; col++) { if (board[row][col] > 0 && board[row][col] == board[row][col + 1]) { if (!set[Action2048.LEFT.id()]) { set[Action2048.LEFT.id()] = true; moves.add(Action2048.LEFT); } if (!set[Action2048.RIGHT.id()]) { set[Action2048.RIGHT.id()] = true; moves.add(Action2048.RIGHT); } break; } } } } if (!set[Action2048.DOWN.id()] || !set[Action2048.UP.id()]) { for (int col = 0; col < SIZE; col++) { for (int row = 0; row < SIZE - 1; row++) { if (board[row][col] > 0 && board[row][col] == board[row + 1][col]) { if (!set[Action2048.UP.id()]) { set[Action2048.UP.id()] = true; moves.add(Action2048.UP); } if (!set[Action2048.DOWN.id()]) { set[Action2048.DOWN.id()] = true; moves.add(Action2048.DOWN); } break; } } } } return moves; }
State2048 implements BoardState { public List<Action2048> getPossibleMoves() { boolean[] set = new boolean[4]; ArrayList<Action2048> moves = new ArrayList<>(4); for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE; col++) { if (board[row][col] > 0) { continue; } if (!set[Action2048.RIGHT.id()]) { for (int col2 = 0; col2 < col; col2++) { if (board[row][col2] > 0) { set[Action2048.RIGHT.id()] = true; moves.add(Action2048.RIGHT); break; } } } if (!set[Action2048.LEFT.id()]) { for (int col2 = col + 1; col2 < SIZE; col2++) { if (board[row][col2] > 0) { set[Action2048.LEFT.id()] = true; moves.add(Action2048.LEFT); break; } } } if (!set[Action2048.DOWN.id()]) { for (int row2 = 0; row2 < row; row2++) { if (board[row2][col] > 0) { set[Action2048.DOWN.id()] = true; moves.add(Action2048.DOWN); break; } } } if (!set[Action2048.UP.id()]) { for (int row2 = row + 1; row2 < SIZE; row2++) { if (board[row2][col] > 0) { set[Action2048.UP.id()] = true; moves.add(Action2048.UP); break; } } } if (moves.size() == 4) { return new ArrayList<>(moves); } } } if (!set[Action2048.RIGHT.id()] || !set[Action2048.LEFT.id()]) { for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE - 1; col++) { if (board[row][col] > 0 && board[row][col] == board[row][col + 1]) { if (!set[Action2048.LEFT.id()]) { set[Action2048.LEFT.id()] = true; moves.add(Action2048.LEFT); } if (!set[Action2048.RIGHT.id()]) { set[Action2048.RIGHT.id()] = true; moves.add(Action2048.RIGHT); } break; } } } } if (!set[Action2048.DOWN.id()] || !set[Action2048.UP.id()]) { for (int col = 0; col < SIZE; col++) { for (int row = 0; row < SIZE - 1; row++) { if (board[row][col] > 0 && board[row][col] == board[row + 1][col]) { if (!set[Action2048.UP.id()]) { set[Action2048.UP.id()] = true; moves.add(Action2048.UP); } if (!set[Action2048.DOWN.id()]) { set[Action2048.DOWN.id()] = true; moves.add(Action2048.DOWN); } break; } } } } return moves; } }
State2048 implements BoardState { public List<Action2048> getPossibleMoves() { boolean[] set = new boolean[4]; ArrayList<Action2048> moves = new ArrayList<>(4); for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE; col++) { if (board[row][col] > 0) { continue; } if (!set[Action2048.RIGHT.id()]) { for (int col2 = 0; col2 < col; col2++) { if (board[row][col2] > 0) { set[Action2048.RIGHT.id()] = true; moves.add(Action2048.RIGHT); break; } } } if (!set[Action2048.LEFT.id()]) { for (int col2 = col + 1; col2 < SIZE; col2++) { if (board[row][col2] > 0) { set[Action2048.LEFT.id()] = true; moves.add(Action2048.LEFT); break; } } } if (!set[Action2048.DOWN.id()]) { for (int row2 = 0; row2 < row; row2++) { if (board[row2][col] > 0) { set[Action2048.DOWN.id()] = true; moves.add(Action2048.DOWN); break; } } } if (!set[Action2048.UP.id()]) { for (int row2 = row + 1; row2 < SIZE; row2++) { if (board[row2][col] > 0) { set[Action2048.UP.id()] = true; moves.add(Action2048.UP); break; } } } if (moves.size() == 4) { return new ArrayList<>(moves); } } } if (!set[Action2048.RIGHT.id()] || !set[Action2048.LEFT.id()]) { for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE - 1; col++) { if (board[row][col] > 0 && board[row][col] == board[row][col + 1]) { if (!set[Action2048.LEFT.id()]) { set[Action2048.LEFT.id()] = true; moves.add(Action2048.LEFT); } if (!set[Action2048.RIGHT.id()]) { set[Action2048.RIGHT.id()] = true; moves.add(Action2048.RIGHT); } break; } } } } if (!set[Action2048.DOWN.id()] || !set[Action2048.UP.id()]) { for (int col = 0; col < SIZE; col++) { for (int row = 0; row < SIZE - 1; row++) { if (board[row][col] > 0 && board[row][col] == board[row + 1][col]) { if (!set[Action2048.UP.id()]) { set[Action2048.UP.id()] = true; moves.add(Action2048.UP); } if (!set[Action2048.DOWN.id()]) { set[Action2048.DOWN.id()] = true; moves.add(Action2048.DOWN); } break; } } } } return moves; } private  State2048();  State2048(State2048 state);  State2048(double[] features); }
State2048 implements BoardState { public List<Action2048> getPossibleMoves() { boolean[] set = new boolean[4]; ArrayList<Action2048> moves = new ArrayList<>(4); for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE; col++) { if (board[row][col] > 0) { continue; } if (!set[Action2048.RIGHT.id()]) { for (int col2 = 0; col2 < col; col2++) { if (board[row][col2] > 0) { set[Action2048.RIGHT.id()] = true; moves.add(Action2048.RIGHT); break; } } } if (!set[Action2048.LEFT.id()]) { for (int col2 = col + 1; col2 < SIZE; col2++) { if (board[row][col2] > 0) { set[Action2048.LEFT.id()] = true; moves.add(Action2048.LEFT); break; } } } if (!set[Action2048.DOWN.id()]) { for (int row2 = 0; row2 < row; row2++) { if (board[row2][col] > 0) { set[Action2048.DOWN.id()] = true; moves.add(Action2048.DOWN); break; } } } if (!set[Action2048.UP.id()]) { for (int row2 = row + 1; row2 < SIZE; row2++) { if (board[row2][col] > 0) { set[Action2048.UP.id()] = true; moves.add(Action2048.UP); break; } } } if (moves.size() == 4) { return new ArrayList<>(moves); } } } if (!set[Action2048.RIGHT.id()] || !set[Action2048.LEFT.id()]) { for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE - 1; col++) { if (board[row][col] > 0 && board[row][col] == board[row][col + 1]) { if (!set[Action2048.LEFT.id()]) { set[Action2048.LEFT.id()] = true; moves.add(Action2048.LEFT); } if (!set[Action2048.RIGHT.id()]) { set[Action2048.RIGHT.id()] = true; moves.add(Action2048.RIGHT); } break; } } } } if (!set[Action2048.DOWN.id()] || !set[Action2048.UP.id()]) { for (int col = 0; col < SIZE; col++) { for (int row = 0; row < SIZE - 1; row++) { if (board[row][col] > 0 && board[row][col] == board[row + 1][col]) { if (!set[Action2048.UP.id()]) { set[Action2048.UP.id()] = true; moves.add(Action2048.UP); } if (!set[Action2048.DOWN.id()]) { set[Action2048.DOWN.id()] = true; moves.add(Action2048.DOWN); } break; } } } } return moves; } private  State2048();  State2048(State2048 state);  State2048(double[] features); static int getNumValues(); int[][] getPowerGrid(); @Override double[] getFeatures(); int getValue(int flatLocation); List<Pair<Double, EnvTransition<State2048>>> getPossibleEnvironmentTransitions(); void addRandomTile(RandomDataGenerator random); List<Integer> getEmptyLocations(); int moveUp(); void rotateBoard(); int makeMove(Action2048 action); List<Action2048> getPossibleMoves(); boolean isTerminal(); int getMaxTile(); void printHumanReadable(); static State2048 getInitialState(int numLocations, RandomDataGenerator random); static State2048 getInitialState(RandomDataGenerator random); @Override Game2048Board getBoard(); @Override String toString(); boolean hasValue(int[] values, int value); int[] getValues(); int getStage(int num_segments, int max_segment); }
State2048 implements BoardState { public List<Action2048> getPossibleMoves() { boolean[] set = new boolean[4]; ArrayList<Action2048> moves = new ArrayList<>(4); for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE; col++) { if (board[row][col] > 0) { continue; } if (!set[Action2048.RIGHT.id()]) { for (int col2 = 0; col2 < col; col2++) { if (board[row][col2] > 0) { set[Action2048.RIGHT.id()] = true; moves.add(Action2048.RIGHT); break; } } } if (!set[Action2048.LEFT.id()]) { for (int col2 = col + 1; col2 < SIZE; col2++) { if (board[row][col2] > 0) { set[Action2048.LEFT.id()] = true; moves.add(Action2048.LEFT); break; } } } if (!set[Action2048.DOWN.id()]) { for (int row2 = 0; row2 < row; row2++) { if (board[row2][col] > 0) { set[Action2048.DOWN.id()] = true; moves.add(Action2048.DOWN); break; } } } if (!set[Action2048.UP.id()]) { for (int row2 = row + 1; row2 < SIZE; row2++) { if (board[row2][col] > 0) { set[Action2048.UP.id()] = true; moves.add(Action2048.UP); break; } } } if (moves.size() == 4) { return new ArrayList<>(moves); } } } if (!set[Action2048.RIGHT.id()] || !set[Action2048.LEFT.id()]) { for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE - 1; col++) { if (board[row][col] > 0 && board[row][col] == board[row][col + 1]) { if (!set[Action2048.LEFT.id()]) { set[Action2048.LEFT.id()] = true; moves.add(Action2048.LEFT); } if (!set[Action2048.RIGHT.id()]) { set[Action2048.RIGHT.id()] = true; moves.add(Action2048.RIGHT); } break; } } } } if (!set[Action2048.DOWN.id()] || !set[Action2048.UP.id()]) { for (int col = 0; col < SIZE; col++) { for (int row = 0; row < SIZE - 1; row++) { if (board[row][col] > 0 && board[row][col] == board[row + 1][col]) { if (!set[Action2048.UP.id()]) { set[Action2048.UP.id()] = true; moves.add(Action2048.UP); } if (!set[Action2048.DOWN.id()]) { set[Action2048.DOWN.id()] = true; moves.add(Action2048.DOWN); } break; } } } } return moves; } private  State2048();  State2048(State2048 state);  State2048(double[] features); static int getNumValues(); int[][] getPowerGrid(); @Override double[] getFeatures(); int getValue(int flatLocation); List<Pair<Double, EnvTransition<State2048>>> getPossibleEnvironmentTransitions(); void addRandomTile(RandomDataGenerator random); List<Integer> getEmptyLocations(); int moveUp(); void rotateBoard(); int makeMove(Action2048 action); List<Action2048> getPossibleMoves(); boolean isTerminal(); int getMaxTile(); void printHumanReadable(); static State2048 getInitialState(int numLocations, RandomDataGenerator random); static State2048 getInitialState(RandomDataGenerator random); @Override Game2048Board getBoard(); @Override String toString(); boolean hasValue(int[] values, int value); int[] getValues(); int getStage(int num_segments, int max_segment); static final int SIZE; static final RectSize BOARD_SIZE; static final int NUM_INITIAL_LOCATIONS; static final double RANDOM_FOUR_PROB; static final int REWARDS; }