private void gameUpdate() { for (Square square : squares) { Point pos = positionPuller.getSquarePosition(square); square.setPosition(pos); } }
public static void main(String[] args) { // square Square square = new Square(5.6); System.out.println("area: " + square.getArea()); System.out.println("perimeter: " + square.getPerimeter()); // rectangle Rectangle rectangle = new Rectangle(1.2, 3.4); System.out.println("area: " + rectangle.getArea()); System.out.println("perimeter: " + rectangle.getPerimeter()); // circle Circle circle = new Circle(1.2); System.out.println("area: " + circle.getArea()); System.out.println("perimeter: " + circle.getPerimeter()); // triangle Triangle triangle = new Triangle(1.2, 1.2, 1.2); System.out.println("area: " + triangle.getArea()); System.out.println("perimeter: " + triangle.getPerimeter()); // shape Shape shape = new Circle(1); System.out.println("area: " + shape.getArea()); System.out.println("perimeter: " + shape.getPerimeter()); }
/** * Returns a collection of potentially legal moves determined by whether the move has the move * vector for the given piece. * * @param endSquare The end square of the potential moves. * @return A HashSet of the resulting moves. */ public HashSet<Move> possibleMovesWhichEndAt(Square endSquare) { final HashSet<Move> moves = new HashSet<>(); int x2 = endSquare.getX(); int y2 = endSquare.getY(); for (int x1 = 0; x1 < Chess.BOARD_SIZE; x1++) { for (int y1 = 0; y1 < Chess.BOARD_SIZE; y1++) { if (!this.isMovablePieceAtSquare(new Square(x1, y1))) { continue; } MoveVector moveVector = new MoveVector(Math.abs(x2 - x1), Math.abs(y2 - y1)); if (Chess.vectorMask(moveVector)) { Square square1 = new Square(x1, y1); moves.add(new Move(square1, endSquare)); if (board[x1][y1].getType().equals(PieceType.PAWN)) { moves.add(new Move(square1, endSquare, PieceType.QUEEN)); moves.add(new Move(square1, endSquare, PieceType.KNIGHT)); moves.add(new Move(square1, endSquare, PieceType.ROOK)); moves.add(new Move(square1, endSquare, PieceType.BISHOP)); } } } } return moves; }
private void movePiece(Piece p, Square destinationSquare, Square originSquare) { p.setVisible(false); originSquare.removeAll(); destinationSquare.add(p); p.setSquare(destinationSquare); p.setVisible(true); }
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 ""; }
@Test public void testSquareOrderVsNull() { Square mySquare = new Square(10); Shape nullShape = new NewShapeNull(); assertTrue(mySquare.compareTo(null) == 1); assertTrue(mySquare.compareTo(nullShape) == 1); }
public Board(int[][] boardValues) { this.squares = new Square[ROWS * COLUMNS]; byte piecesOnBoard = 0; byte whitePiecesOnBoard = 0; byte blackPiecesOnBoard = 0; for (byte x = 0; x < COLUMNS; x++) { for (byte y = 0; y < ROWS; y++) { byte squareValue = (byte) boardValues[x][y]; byte index = index(x, y); Square sq = this.squares[index] = new Square(squareValue); if (!(sq.isEmpty())) { piecesOnBoard++; if (sq.getPiece().getColor() == ChessPieceColor.WHITE) { whitePiecesOnBoard++; } else { blackPiecesOnBoard++; } } } } this.piecesOnBoard = piecesOnBoard; this.whitePiecesOnBoard = whitePiecesOnBoard; this.blackPiecesOnBoard = blackPiecesOnBoard; }
/** * Returns a collection of potentially legal moves determined by whether the move has the move * vector for the given piece. * * @param startSquare The start square of the potential moves. * @return A HashSet of the resulting moves. */ public HashSet<Move> potentialMovesWhichStartAt(Square startSquare) { final ArrayList<PieceLogic> pieceLogics = new ArrayList<>(); final HashSet<Move> moves = new HashSet<>(); final PieceType pieceType = this.getPieceAt(startSquare).getType(); pieceLogics.add(new PawnAdvanceLogic()); // TODO: Refactor pieceLogics.add(new PawnCaptureLogic()); pieceLogics.add(new PawnEnPassantLogic()); pieceLogics.add(new KnightLogic()); pieceLogics.add(new BishopLogic()); pieceLogics.add(new RookLogic()); pieceLogics.add(new QueenLogic()); pieceLogics.add(new KingLogic()); pieceLogics.add(new KingCastlingLogic()); for (PieceLogic pieceLogic : pieceLogics) { if (pieceLogic.relevantPiece().equals(pieceType)) { for (MoveVector moveVector : pieceLogic.getPossibleVectors()) { final Square square2 = startSquare.addVector(moveVector); if (square2.isOnBoard()) { moves.add(new Move(startSquare, square2)); } } } } return moves; }
/** * drawing a frame drawing happens if world.start than it is assumed that game is loaded and * running therefore is ** scene.draw (GL10 gl) is called until world.start it is assumed that we * are still loading therefore ** scene.loading (GL10 gl) is called --- world starts in ** * scene.loader() (a load method without gl parameter) if you do not call world.start() in * scene.loader() than scene.load(GL10 gl) will be called on every frame draw --- scene.loading * (GL10 gl) is called every time the frame is going to draw scene.loader is called once --- while * loading you can draw a loader or progressbar * * @param gl */ @Override public void onDrawFrame(GL10 gl) { // debug ("image: bitmaps size "+bitmaps.values().size()); if (bitmaps.size() > 0) { if (!active) { active = true; } for (int resource : bitmaps.keySet()) { debug("image: entering binder for resource " + resource); // here comes decode(R.drawable.something) from decode method for binding image(gl, resource); // @TODO try bind (gl, resource) instead of image(gl,resource) // because it seems that why decoding twice same image ? // if load thread is running it only binds it // assuming bitmap is already decoded } } else { // debug ("image: bitmaps not populated"); } // // clear Screen and Depth Buffer gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glMatrixMode(GL10.GL_MODELVIEW); // // Reset the Modelview Matrix gl.glLoadIdentity(); if (world.start) { draw(gl); // System.out.println ("world entity count "+world.subjects.size ()); } else { loading(gl); } if (config.display == Config.FIT && shift > 0) { square.draw(gl, 0, null, 0, -shift, display.width, shift, 0); square.draw(gl, 0, null, 0, display.height, display.width, shift, 0); } }
@Test public void twentyFor4x5RectangleFromSquare() throws Exception { final Rectangle rectangle = new Rectangle(); rectangle.setWidth(5); rectangle.setHeight(4); final Square square = new Square(); square.setSideLength(3); List<Shape> shapes = new ArrayList<Shape>() { { add(rectangle); add(square); } }; List<Integer> areas = new ArrayList<Integer>(); for (Shape shape : shapes) { areas.add(shape.area()); } assertEquals(20, areas.get(0).intValue()); assertEquals(9, areas.get(1).intValue()); }
@Test public void nineFor3x3Square() throws Exception { Square square = new Square(); square.setSideLength(3); assertEquals(9, square.area()); }
// keeps track of time, and calls methods that need to be called every drop public static void timeKeeping() { // random place stuff int rantime = 0; // the startTime helps keep track of how much time has passed since the loop started, which // keeps the computer's performance from effecting game speed long startTime = System.currentTimeMillis(); Main.display.repaint(); sleep(startTime); // a loop that calls methods after every drop while (Main.gameOn) { startTime = System.currentTimeMillis(); Physics.clearLineCheck(); Physics.dropCall(); Square.neighborFindCall(); Main.display.repaint(); sleep(startTime); // random place stuff rantime++; if (rantime > 3) { Square.randomPlace(); rantime = 0; } } }
public void pulseOccurred(Square source) { int r = source.getRow(); int c = source.getColumn(); int pulsePower = (int) (source.getPower() + source.getPulse()); pulsePower = (int) (pulsePower - Math.ceil(pulsePower * PULSE_DECAY)); // System.out.println("Pulse Occured at ("+c+", "+r+") with Power "+pulsePower); if (pulsePower > 0) { // Top Left Corner if (r - 1 >= 0 && !board[r - 1][c].isPulsing()) board[r - 1][c].dieWithPulse(pulsePower); if (c - 1 >= 0 && !board[r][c - 1].isPulsing()) board[r][c - 1].dieWithPulse(pulsePower); if (r - 1 >= 0 && c - 1 >= 0 && !board[r - 1][c - 1].isPulsing()) board[r - 1][c - 1].dieWithPulse(pulsePower); // Bottom Right Corner if (r + 1 < NUM_OF_SQUARES && !board[r + 1][c].isPulsing()) board[r + 1][c].dieWithPulse(pulsePower); if (c + 1 < NUM_OF_SQUARES && !board[r][c + 1].isPulsing()) board[r][c + 1].dieWithPulse(pulsePower); if (r + 1 < NUM_OF_SQUARES && c + 1 < NUM_OF_SQUARES && !board[r + 1][c + 1].isPulsing()) board[r + 1][c + 1].dieWithPulse(pulsePower); if (r + 1 < NUM_OF_SQUARES && c - 1 >= 0 && !board[r + 1][c - 1].isPulsing()) board[r + 1][c - 1].dieWithPulse(pulsePower); if (r - 1 >= 0 && c + 1 < NUM_OF_SQUARES && !board[r - 1][c + 1].isPulsing()) board[r - 1][c + 1].dieWithPulse(pulsePower); } }
public void putStone(Square.Status color, int x, int y) { color.assertToBeColor(); Square target = board[x][y]; target.setStatus(color); reverseOpponentsInAllDirections(target); }
public static void loadCache() { Square square = new Square(); shapes.put(square.getName(), square); Circle circle = new Circle(); shapes.put(circle.getName(), circle); }
@Test public void testReveal() { // TODO: test for setRevealed() return value? assertFalse("Square should not be revealed by default", s.isRevealed()); s.setRevealed(true); assertTrue("Square should be revealed after calling setRevealed", s.isRevealed()); }
@Override public boolean validMoveTo(Square toSquare) { if (toSquare.getClass() == RoomSquare.class) { return hasDoor; // if room square, can only move there if there is a door here } else { // check if square is a valid destination otherwise return toSquare.validDestinationSquare(this); } }
public void setUserSquare(int squareId) { for (Square square : squares) { if (square.getId() == squareId) { this.userSquare = square; return; } } }
@Override public boolean requestCapture(String square, Ghost ghost, Player player) { Square s = Game.getCurrent().getBoard().getSquare(square); return player.hasGhost(ghost) && s != null && s.getGhost() != null && !player.hasGhost(s.getGhost()); }
public Movement(String movString) { int sourceFile = movString.charAt(0) - 'a'; int sourceRank = movString.charAt(1) - '1'; int destinationFile = movString.charAt(2) - 'a'; int destinationRank = movString.charAt(3) - '1'; this.setSource(Square.fromRankAndFile(sourceRank, sourceFile)); this.setDestination(Square.fromRankAndFile(destinationRank, destinationFile)); }
@Test public void testObservable() { TestObserver observer = new TestObserver(); s.addObserver(observer); s.setRevealed(true); assertTrue("Square should update observers after revealing", observer.updated); }
public ArrayList<Byte> validMoves(int index) { Square square = this.get(index); if (square.isEmpty()) { return new ArrayList<Byte>(); } ChessPiece pc = square.getPiece(); ChessPieceColor opponentColor = (pc.getColor() == ChessPieceColor.WHITE) ? ChessPieceColor.BLACK : ChessPieceColor.WHITE; ArrayList<Byte> validMoves = new ArrayList<Byte>(24); switch (pc.getType()) { case KNIGHT: addValidMoves(ChessPieceMoves.KNIGHT_MOVES[index], pc, opponentColor, validMoves, false); break; case KING: addValidMoves(ChessPieceMoves.KING_MOVES[index], pc, opponentColor, validMoves, false); break; case PAWN: if (pc.getColor() == ChessPieceColor.WHITE) { addValidMoves( ChessPieceMoves.WHITE_PAWN_MOVES[index], pc, opponentColor, validMoves, false); } else { addValidMoves( ChessPieceMoves.BLACK_PAWN_MOVES[index], pc, opponentColor, validMoves, false); } break; case ROOK: addValidMoves(ChessPieceMoves.ROOK_MOVES_DOWN[index], pc, opponentColor, validMoves, true); addValidMoves(ChessPieceMoves.ROOK_MOVES_LEFT[index], pc, opponentColor, validMoves, true); addValidMoves(ChessPieceMoves.ROOK_MOVES_RIGHT[index], pc, opponentColor, validMoves, true); addValidMoves(ChessPieceMoves.ROOK_MOVES_UP[index], pc, opponentColor, validMoves, true); break; case QUEEN: addValidMoves(ChessPieceMoves.QUEEN_MOVES_DOWN[index], pc, opponentColor, validMoves, true); addValidMoves(ChessPieceMoves.QUEEN_MOVES_LEFT[index], pc, opponentColor, validMoves, true); addValidMoves( ChessPieceMoves.QUEEN_MOVES_RIGHT[index], pc, opponentColor, validMoves, true); addValidMoves(ChessPieceMoves.QUEEN_MOVES_UP[index], pc, opponentColor, validMoves, true); addValidMoves( ChessPieceMoves.QUEEN_MOVES_DOWN_LEFT[index], pc, opponentColor, validMoves, true); addValidMoves( ChessPieceMoves.QUEEN_MOVES_DOWN_RIGHT[index], pc, opponentColor, validMoves, true); addValidMoves( ChessPieceMoves.QUEEN_MOVES_UP_LEFT[index], pc, opponentColor, validMoves, true); addValidMoves( ChessPieceMoves.QUEEN_MOVES_UP_RIGHT[index], pc, opponentColor, validMoves, true); break; } return validMoves; }
@Test public void greenStartHasFourStones() { Ludo game = new Ludo(); Square red = game.getStartSquare(Color.GREEN); assertEquals(4, Length.of(red.occupants())); for (Stone each : red.occupants()) { assertEquals(Color.GREEN, each.color); assertEquals(true, each.atStart()); } }
Square rotate(int count) { if (count == 0) return this; else { Square result = new Square(n); for (int r = 0; r < n; r++) for (int c = 0; c < n; c++) result.s[r][c] = s[n - 1 - c][r]; return result.rotate(count - 1); } }
@Test public void amoebeShouldDie() { Amoebe amoebe = AmoebeFactory.get(this.compassProvider, Game.Color.RED); int[] position = {0, 0}; Square square = SquareFactory.get(position); amoebe.setSquare(square); square.enterSquare(amoebe); assertTrue(square.getAmoebesList().size() == 1); amoebe.die(); assertTrue(square.getAmoebesList().size() == 0); }
@Test public void amoebeShouldExcrement() { Amoebe amoebe = AmoebeFactory.get(this.compassProvider, Game.Color.RED); int[] position = {0, 0}; Square square = SquareFactory.get(position); amoebe.setSquare(square); square.enterSquare(amoebe); assertTrue(square.getFoodcubes().size() == 6); amoebe.excrement(); assertTrue(square.getFoodcubes().size() == 8); }
public void takeTurn(Die die1, Die die2) { die1.roll(); die2.roll(); steps = die1.getFaceValue() + die2.getFaceValue(); for (int i = 0; i < steps; i++) { location = location.getNextSquare(); location.passOverBy(this); } location.landOnBy(this); }
public static void main(String args[]) { Circle c = new Circle(30, 40, 2.3); Rectangle r = new Rectangle(50, 60, 3.5, 4.5); Square s = new Square(70, 80, 5.5); System.out.println("Circle c: \n" + c); System.out.println("\nRectangle r: \n" + r); System.out.println("\nSquare s: \n" + s); System.out.println("\n\nArea of Square s = " + s.area()); }
/** * If the a move created by the given starting square and ending square in the given position is a * promotion, returns an array containing a king, pawn, knight, bishop, rook queen of the color of * the promoted pawn. Otherwise returns <code>null</code>. */ public Piece[] getPromotionTargets(Position pos, Square startingSquare, Square endingSquare) { checkPosition(pos); ChessPiece movingPiece = (ChessPiece) pos.getPieceAt(startingSquare); if ((endingSquare.getRank() == 7) && (movingPiece == ChessPiece.WHITE_PAWN)) return (Piece[]) whitePromotionTargets.clone(); if ((endingSquare.getRank() == 0) && (movingPiece == ChessPiece.BLACK_PAWN)) return (Piece[]) blackPromotionTargets.clone(); return null; }
private void reverseOpponents(Square target, Direction direction) { Square.Status color = target.getStatus(); Cursor cursor = target.createCursor(); while (true) { cursor.moveIn(direction); if (statusOn(cursor).isOpponent(color)) { continue; } else if (statusOn(cursor).isSameColor(color)) { doReverseOpponentsInBetweenExclusive(cursor.getOrigin(), cursor, direction); } break; } }