OthelloState extends TwoDimensionalBoardGameState { @SuppressWarnings("unchecked") @Override public <T extends BoardGameState> Set<T> possibleBoardGameStates(T currentState) { Set<T> possible = new HashSet<T>(); List<Point> chipMoves = new ArrayList<Point>(); for(int i = 0; i < BOARD_WIDTH; i++){ for(int j = 0; j < BOARD_WIDTH; j++){ if(boardState[i][j] == nextPlayer){ chipMoves.add(new Point(i, j)); } } } for(Point chip : chipMoves){ int chipX = (int) chip.getX(); int chipY = (int) chip.getY(); for(int dX = -1; dX <= 1; dX++){ for(int dY = -1; dY <= 1; dY++){ if(dX != 0 || dY != 0){ int x = chipX; int y = chipY; do{ x += dX; y += dY; }while((x >= 0 && x < BOARD_WIDTH) && (y >= 0 && y < BOARD_WIDTH) && boardState[x][y] == (nextPlayer + 1) % 2); if((x >= 0 && x < BOARD_WIDTH) && (y >= 0 && y < BOARD_WIDTH)){ if(boardState[x][y] == EMPTY){ OthelloState temp = (OthelloState) currentState.copy(); if(temp.moveSinglePoint(new Point(x, y))){ possible.add((T) temp); } } } } } } } if(possible.isEmpty()){ possible.add((T) this.pass()); } return possible; } OthelloState();  OthelloState(OthelloState state);  OthelloState(int[][] board, int nextPlay, List<Integer> win); int getNumPlayers(); @Override boolean endState(); @Override boolean moveSinglePoint(Point goTo); @SuppressWarnings("unchecked") @Override Set<T> possibleBoardGameStates(T currentState); @SuppressWarnings("unchecked") @Override OthelloState copy(); @Override List<Integer> getWinners(); @Override void setupStartingBoard(); @Override int getBoardWidth(); @Override int getBoardHeight(); @Override char[] getPlayerSymbols(); @Override Color[] getPlayerColors(); @Override boolean moveDoublePoint(Point goTo, Point moveTo); @Override boolean moveOnePiece(); String toString(); static final int BLACK_CHIP; static final int WHITE_CHIP; }
@Test public void testPossibleBoardGameStates() { Set<OthelloState> listStart = start.possibleBoardGameStates(start); Set<OthelloState> list1 = test1.possibleBoardGameStates(test1); Set<BoardGameState> listTestStart = new HashSet<BoardGameState>(); listTestStart.add(new OthelloState(new int[][]{{E,E,E,E,E,E,E,E}, {E,E,E,E,E,E,E,E}, {E,E,E,E,E,E,E,E}, {E,E,E,B,B,B,E,E}, {E,E,E,W,B,E,E,E}, {E,E,E,E,E,E,E,E}, {E,E,E,E,E,E,E,E}, {E,E,E,E,E,E,E,E}}, W, new ArrayList<Integer>())); listTestStart.add(new OthelloState(new int[][]{{E,E,E,E,E,E,E,E}, {E,E,E,E,E,E,E,E}, {E,E,E,E,B,E,E,E}, {E,E,E,B,B,E,E,E}, {E,E,E,W,B,E,E,E}, {E,E,E,E,E,E,E,E}, {E,E,E,E,E,E,E,E}, {E,E,E,E,E,E,E,E}}, W, new ArrayList<Integer>())); listTestStart.add(new OthelloState(new int[][]{{E,E,E,E,E,E,E,E}, {E,E,E,E,E,E,E,E}, {E,E,E,E,E,E,E,E}, {E,E,E,B,W,E,E,E}, {E,E,E,B,B,E,E,E}, {E,E,E,B,E,E,E,E}, {E,E,E,E,E,E,E,E}, {E,E,E,E,E,E,E,E}}, W, new ArrayList<Integer>())); listTestStart.add(new OthelloState(new int[][]{{E,E,E,E,E,E,E,E}, {E,E,E,E,E,E,E,E}, {E,E,E,E,E,E,E,E}, {E,E,E,B,W,E,E,E}, {E,E,B,B,B,E,E,E}, {E,E,E,E,E,E,E,E}, {E,E,E,E,E,E,E,E}, {E,E,E,E,E,E,E,E}}, W, new ArrayList<Integer>())); Set<BoardGameState> listTest1 = new HashSet<BoardGameState>(); OthelloState testBoard1 = (OthelloState) test1.copy(); testBoard1.moveSinglePoint(new Point(6,4)); listTest1.add(testBoard1); OthelloState testBoard2 = (OthelloState) test1.copy(); testBoard2.moveSinglePoint(new Point(0,4)); listTest1.add(testBoard2); OthelloState testBoard3 = (OthelloState) test1.copy(); testBoard3.moveSinglePoint(new Point(3,1)); listTest1.add(testBoard3); OthelloState testBoard4 = (OthelloState) test1.copy(); testBoard4.moveSinglePoint(new Point(3,7)); listTest1.add(testBoard4); OthelloState testBoard5 = (OthelloState) test1.copy(); testBoard5.moveSinglePoint(new Point(6,1)); listTest1.add(testBoard5); OthelloState testBoard6 = (OthelloState) test1.copy(); testBoard6.moveSinglePoint(new Point(6,7)); listTest1.add(testBoard6); OthelloState testBoard7 = (OthelloState) test1.copy(); testBoard7.moveSinglePoint(new Point(0,1)); listTest1.add(testBoard7); OthelloState testBoard8 = (OthelloState) test1.copy(); testBoard8.moveSinglePoint(new Point(0,7)); listTest1.add(testBoard8); for(BoardGameState othello: listStart){ assertTrue(listTestStart.contains(othello)); } for(BoardGameState othello: list1){ assertTrue(listTest1.contains(othello)); } Set<OthelloState> list2 = test2.possibleBoardGameStates(test2); Set<OthelloState> listTest2 = new HashSet<OthelloState>(); listTest2.add(test2.pass()); OthelloState temp = null; for(OthelloState othello: list2){ assertTrue(listTest2.contains(othello)); assertFalse(othello.endState()); temp = othello; } list2 = temp.possibleBoardGameStates(temp); assertTrue(list2.size() > 1); }