/** 2|2|- */ @Test public void emptyLastCell() { Board board = new Board(3); List<Cell> cells = getCells(2, 2, null); assertFalse(board.areCellsMovable(cells)); }
/** * Tests updateScore and getScore methods. First, the score should be 0. Then the score is updated * with 20 (2 Gems). Then the score should be 20. */ @Test public final void scoreTest() { board = new Board(2, 0, 0, false); assertEquals(board.getScore(), 0); board.updateScore(2); assertEquals(board.getScore(), 20); }
public Move(Board board, int[] oldPositions, int[] newPositions, int stepNumber) { this.board = board; this.stepNumber = stepNumber; int robotNumber = 0, oldPosition = 0, newPosition = 0; for (int robo = 0; robo < oldPositions.length; ++robo) { if (oldPositions[robo] != newPositions[robo]) { robotNumber = robo; oldPosition = oldPositions[robo]; newPosition = newPositions[robo]; break; } } this.robotNumber = robotNumber; this.oldPosition = oldPosition; this.newPosition = newPosition; this.pathMap = new HashMap<Integer, Integer>(); final int diffPos = newPosition - oldPosition; this.direction = board.getDirection(diffPos); final int pathStart = 1 << this.direction; final int pathEnd = 1 << board.getDirection(-diffPos); final int posIncr = board.directionIncrement[this.direction]; int i = oldPosition; this.pathMap.put(Integer.valueOf(i), Integer.valueOf(pathStart)); for (i += posIncr; i != newPosition; i += posIncr) { this.pathMap.put(Integer.valueOf(i), Integer.valueOf(pathStart + pathEnd)); } this.pathMap.put(Integer.valueOf(i), Integer.valueOf(pathEnd)); }
@Test public void testPrintsBoardToConsole() { Board board = new Board(); System.setOut(new PrintStream(outContent)); board.display(); assertEquals("0 | 1 | 2\n3 | 4 | 5\n6 | 7 | 8", outContent.toString()); }
/** * Somewhat multi-threaded depth-first search. Performs a DFS of the subtrees from the current * node in parallel. * * @param s The tile sequence * @param b The board to search * @param pool The thread pool in which to submit jobs to. * @return The board with the highest evaluation or null if no board can continue. */ private Board solve_pdfs(Board b, ExecutorService pool) { List<Future<Board>> rets = new ArrayList<>(BOARD_WIDTH); Board best = null; int best_score = -1; for (Direction d : directions) { Board n = new Board(b); if (n.move(tileSequence, d)) { rets.add(pool.submit(new ParallelDFS(n))); } } for (Future<Board> ret : rets) { try { Board c = ret.get(); if (c != null) { int score = evaluate(c); if (score > best_score) { best = c; best_score = score; } } } catch (InterruptedException | ExecutionException e) { System.err.println("Error: " + e.getMessage()); } } return best; }
public boolean queensideClear() { boolean[] flags = ChessGameDemo.getFlags(); int row = square.getRow(); int column = square.getColumn(); if ((row == 1) && (flags[0] || flags[4])) { return false; } if ((row == 8) && (flags[2] || flags[5])) { return false; } if (((row == 1) && (column == 5)) || ((row == 8) && (column == 5))) { for (int i = 1; i < 4; i++) { int r = row; int c = column - i; int cell = square.getCellFromCoord(r, c); Square sq = (Square) board.getComponent(cell); if ((sq.getComponentCount() == 1) || (r < 1)) { return false; } else if (color == 1) { if ((board.getBlackCheckList()).contains(sq.getPosition())) { return false; } } else { if ((board.getWhiteCheckList()).contains(sq.getPosition())) { return false; } } } return true; } return false; }
public static void main(String[] args) { // create initial board from file In in = new In(args[0]); int N = in.readInt(); int[][] tiles = new int[N][N]; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { tiles[i][j] = in.readInt(); } Board initial = new Board(tiles); /* System.out.println(initial.isSolvable()); System.out.println(initial.toString()); Iterable<Board> newbrdstack = new Stack<Board>(); newbrdstack = initial.neighbors(); System.out.println(newbrdstack.toString()); */ // check if puzzle is solvable; if so, solve it and output solution if (initial.isSolvable()) { Solver solver = new Solver(initial); StdOut.println("Minimum number of moves = " + solver.moves()); for (Board board : solver.solution()) StdOut.println(board); } // if not, report unsolvable else { StdOut.println("Unsolvable puzzle"); } }
/** Test of legalMoves method, of class Board. */ @Test public void testLegalMoves() { System.out.println("legalMoves"); Board instance = new Board(); Iterator<Move> result = instance.legalMoves(); assertEquals(Move.create("c1-a3"), result.next()); }
/** * Paints the graphic component * * @param g Graphic component */ public void paint(Graphics g) { if (environment != null) { Sudoku env = (Sudoku) environment; Board board = env.getBoard(); int n = SudokuLanguage.DIGITS; int sqrt_n = (int) (Math.sqrt(n) + 0.1); g.setColor(Color.lightGray); Font font = g.getFont(); g.setFont(new Font(font.getName(), font.getStyle(), 20)); for (int i = 0; i < n; i++) { int ci = getCanvasValue(i); if (i % sqrt_n == 0) { g.drawLine(ci, DRAW_AREA_SIZE + MARGIN, ci, MARGIN); g.drawLine(DRAW_AREA_SIZE + MARGIN, ci, MARGIN, ci); } for (int j = 0; j < n; j++) { int cj = getCanvasValue(j); int value = board.get(i, j); if (value > 0) { g.setColor(Color.black); g.drawString("" + value, cj + CELL_SIZE / 5, ci + CELL_SIZE); g.setColor(Color.lightGray); } } } g.drawLine(DRAW_AREA_SIZE + MARGIN, DRAW_AREA_SIZE + MARGIN, DRAW_AREA_SIZE + MARGIN, MARGIN); g.drawLine(DRAW_AREA_SIZE + MARGIN, DRAW_AREA_SIZE + MARGIN, MARGIN, DRAW_AREA_SIZE + MARGIN); } }
/** * Creates all possible moves from the current board and their corresponding Node object with the * current position as the parent * * @param piece specifies player that can make the move * @throws Exception */ public void createChildren(char color) { ArrayList<Piece> moveable = current.allJumps(color); if (moveable.size() != 0) // if it's a jump, take it. find the final states and return it. { for (Piece move : moveable) { ArrayList<Board> temp = childrenHelper(current, move); for (Board t : temp) { children.add(new Node(this, t)); } } } else // else, make the moves and return it. { moveable = current.allMoves(color); for (Piece move : moveable) { ArrayList<Piece> move_list = current.validMoves(move); for (Piece move_to : move_list) { Board temp = new Board(current); try { temp.makeMove(move, move_to); children.add(new Node(this, temp)); } catch (Exception e) { System.out.println("Move not made: " + move + "-->" + move_to); } } } } }
/** * This specifically gets the jumps, and recursive jumps. * * @param move is the piece to move * @return the final jump states * @throws Exception */ private ArrayList<Board> childrenHelper(Board b, Piece move) { ArrayList<Board> result = new ArrayList<Board>(); ArrayList<Piece> move_list = b.validJumps(move); for (Piece move_to : move_list) { Board temp = new Board(b); boolean remain; try { remain = temp.makeMove(move, move_to); if (remain == true) { ArrayList<Board> future_list = childrenHelper( temp, temp.getPiece(move_to.getPosition()[0], move_to.getPosition()[1])); for (Board future : future_list) { result.add(future); } if (future_list.size() == 0) result.add(temp); } else { result.add(temp); } } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } } return result; }
public static void main(String[] args) { Board b = new Board(8); b.explore(b, 1); b.solveQueens(b); }
public NodeIterator(Node start, int n) { for (int i = -2; i <= 2; i++) { for (int j = -2; j <= 2; j++) { Board settingClone = new BoardImpl((BoardImpl) setting); Player moverClone = new PlayerImpl(owner); settingClone.getPawn(moverClone, settingClone).setSquare(square); MovePawn tentative = new MovePawnImpl( square.getCol() + j, square.getRow() + i, moverClone, settingClone); if (tentative.isValid()) { nextList.add( new Node( new SquareImpl(square.getCol() + j, square.getRow() + i), n + 1, moverClone, start)); } } } backer = nextList.iterator(); }
/** 2|-|2 */ @Test public void emptyMiddleCell() { Board board = new Board(3); List<Cell> cells = getCells(2, null, 2); assertTrue(board.areCellsMovable(cells)); }
@Override public void removeAt(PS ps) { if (ps == null) return; Board board = this.get(ps.getWorld()); if (board == null) return; board.removeAt(ps); }
@Override public boolean isBorderPs(PS ps) { if (ps == null) return false; Board board = this.get(ps.getWorld()); if (board == null) return false; return board.isBorderPs(ps); }
@Test public void testMoveNextToSurroundedByRobots() { Board board = facade.createBoard(16, 26); Robot robot1 = facade.createRobot(0, 6000); Robot robot2 = facade.createRobot(0, 3000); facade.putRobot(board, 7, 15, robot1); facade.putRobot(board, 12, 23, robot2); Set<Position> robotsRoundRobot2Positions = new HashSet<Position>(); for (long x = 10; x < 15; x++) { robotsRoundRobot2Positions.add(Position.newPosition(x, 21, board)); robotsRoundRobot2Positions.add(Position.newPosition(x, 26, board)); } for (long y = 22; y < 26; y++) { robotsRoundRobot2Positions.add(Position.newPosition(10, y, board)); robotsRoundRobot2Positions.add(Position.newPosition(14, y, board)); } for (Position robotPosition : robotsRoundRobot2Positions) board.putElement(robotPosition, new Robot(new Energy(1000, unitOfPower.Ws), Orientation.UP)); facade.moveNextTo(robot1, robot2); assertTrue(robot1.getPosition().equals(Position.newPosition(12, 20, board))); assertTrue(robot2.getPosition().equals(Position.newPosition(12, 22, board))); assertEquals(800, robot1.getAmountOfEnergy(), epsilon); assertEquals(2500, robot2.getAmountOfEnergy(), epsilon); }
@Override public boolean isConnectedPs(PS ps, Faction faction) { if (ps == null) return false; Board board = this.get(ps.getWorld()); if (board == null) return false; return board.isConnectedPs(ps, faction); }
public String castleQueenside(Square candidateSquare) { if (piece == 0) { int row; int column; if (color == 1) { row = 1; column = 1; } else { row = 8; column = 1; } int cell = square.getCellFromCoord(row, column); Square sq = (Square) board.getComponent(cell); Piece pc = (Piece) sq.getComponent(0); int newCell; if (pc.getColor() == 1) { newCell = square.getCellFromCoord(1, 4); } else { newCell = square.getCellFromCoord(8, 4); } Square newSquare = (Square) board.getComponent(newCell); // move rook movePiece(pc, newSquare, sq); return ("0-0-0"); } return ""; }
@Override public ArrayList<String> getMap(RelationParticipator observer, PS centerPs, double inDegrees) { if (centerPs == null) return null; Board board = this.get(centerPs.getWorld()); if (board == null) return null; return board.getMap(observer, centerPs, inDegrees); }
@Override public String toString() { String rs = role == null ? "-no role-" : role.toString(); return String.format( "%d %d xy=[%d,%d] bs=[%d,%d,%d] %s", turn, id, x, y, board.sizeX(), board.sizeY(), board.knownCells, rs); }
@Override public TerritoryAccess getTerritoryAccessAt(PS ps) { if (ps == null) return null; Board board = this.get(ps.getWorld()); if (board == null) return null; return board.getTerritoryAccessAt(ps); }
private Board solve_dfs(Board b, int depthLimit, int depth) { if (depth >= depthLimit) { // Cutoff test return b; } Board best = null; int best_score = -1; for (int i = 0; i < BOARD_WIDTH; i++) { Board next = new Board(b); if (next.move(tileSequence, directions[i])) { Board candidate = solve_dfs(next, depthLimit, depth + 1); if (candidate == null && next.finished()) { candidate = next; } if (candidate != null) { if (candidate.finished()) { updateBest(candidate); } else { int score = evaluate(candidate); if (score > best_score) { best_score = score; best = candidate; } } } } } return best; }
@Override public Faction getFactionAt(PS ps) { if (ps == null) return null; Board board = this.get(ps.getWorld()); if (board == null) return null; return board.getFactionAt(ps); }
/** * Tests the deleteVertical method, by making a nice board with vertical combinations of 5,4 and 3 * Gems and checking if the method finds those combinations. */ @Test public final void testDeleteVertical() { board = new Board(5, 0, 0, false); gems = board.getGems(); for (int col = 0; col < 5; col++) { for (int row = 0; row < 5 - col; row++) { gems[row][col].setType(1); } } gems[4][1].setType(2); gems[3][2].setType(2); gems[4][2].setType(2); ArrayList<Gem> array1 = new ArrayList<Gem>(); array1.add(gems[1][0]); array1.add(gems[2][0]); array1.add(gems[3][0]); array1.add(gems[4][0]); ArrayList<Gem> array2 = board.deleteVertical(gems[0][0]); assertEquals(array1, array2); ArrayList<Gem> array3 = new ArrayList<Gem>(); array3.add(gems[1][1]); array3.add(gems[0][1]); array3.add(gems[3][1]); ArrayList<Gem> array4 = board.deleteVertical(gems[2][1]); assertEquals(array3, array4); ArrayList<Gem> array5 = new ArrayList<Gem>(); array5.add(gems[0][2]); array5.add(gems[2][2]); ArrayList<Gem> array6 = board.deleteVertical(gems[1][2]); assertEquals(array5, array6); }
@Override public void setTerritoryAccessAt(PS ps, TerritoryAccess territoryAccess) { if (ps == null) return; Board board = this.get(ps.getWorld()); if (board == null) return; board.setTerritoryAccessAt(ps, territoryAccess); }
/** * Tests the setGems and getGems methods, by first setting Gems[][] testgems, and then checking * whether it was set with the getGems method. */ @Test public final void setGemTest() { board = new Board(2, 0, 0, false); Gem[][] testgems = new Gem[2][2]; board.setGems(testgems); assertArrayEquals(testgems, board.getGems()); }
@Override public void setFactionAt(PS ps, Faction faction) { if (ps == null) return; Board board = this.get(ps.getWorld()); if (board == null) return; board.setFactionAt(ps, faction); }
private boolean checkGameStatus(Position pos) { Piece[][] grid = board.getCurrentBoard(); int row = 0, column = 0, diagonal = 0, antiDiagonal = 0; int size = board.getSize(); int x = pos.getX(); int y = pos.getY(); for (int i = 0; i < size; i++) { if (!board.isEmptyPosition(new Position(x, i)) && board.getPiece(new Position(x, i)).getType() == turn) column++; if (!board.isEmptyPosition(new Position(i, y)) && board.getPiece(new Position(i, y)).getType() == turn) row++; if (!board.isEmptyPosition(new Position(i, i)) && board.getPiece(new Position(i, i)).getType() == turn) diagonal++; if (!board.isEmptyPosition(new Position(i, size - 1 - i)) && board.getPiece(new Position(i, size - 1 - i)).getType() == turn) antiDiagonal++; } System.out.print(row + " " + column + " " + diagonal + " " + antiDiagonal + "\n"); if (column == size || row == size || diagonal == size || antiDiagonal == size) { if (turn == X) { System.out.println("Player X has won!"); } else if (turn == O) { System.out.println("Player O has won!"); } return false; } return true; }
/** -|2|- */ @Test public void emptyFirstAndLastCell() { Board board = new Board(3); List<Cell> cells = getCells(null, 2, null); assertTrue(board.areCellsMovable(cells)); }