public MITrisBoard addPiece(Piece piece, int rot, int x, int y) { if (piece == null) return this; MITrisBoard ret = new MITrisBoard(this); rot %= piece.getMaxRotation(); for (int i = 0; i < piece.getNumComponents(); i++) { int px = x, py = y; switch (rot) { case 0: px += piece.getX(i); py += piece.getY(i); break; case 1: px += piece.getY(i); py -= piece.getX(i); break; case 2: px -= piece.getX(i); py -= piece.getY(i); break; case 3: px -= piece.getY(i); py += piece.getX(i); break; default: throw new RuntimeException("invalid rotation"); } if (py < 0 || px < 0 || px >= width) // out of bounds return null; else if (py < height && ret.data[px][py] != null) // overlap return null; else if (py < height) ret.data[px][py] = piece; } return ret; }
public void moveRight() { Piece imaginary = new Piece(currentPiece.getX(), currentPiece.getY() + 1, currentPiece.getPiece_bits()); boolean[][] tempResult = MatrixBoolean2DUtil.alignedNand( coordinates, currentPiece.getPiece_bits(), currentPiece.getX(), currentPiece.getY()); boolean[][] tempCoordinates = MatrixBoolean2DUtil.and(coordinates, tempResult); if (roomExist(tempCoordinates, imaginary)) { coordinates = tempCoordinates; spawn(imaginary); } }
public Snake createSnake() { Piece head = new Piece(sizeOfArena / 2, sizeOfArena / 2, 1); // middle of the arena Piece part2 = new Piece((sizeOfArena / 2) - 1, sizeOfArena / 2, 1); // secondPart Piece part3 = new Piece((sizeOfArena / 2) - 2, sizeOfArena / 2, 1); // third part board[head.getX()][head.getY()] = head; // save the snake to the arena board[part2.getX()][part2.getY()] = part2; board[part3.getX()][part3.getY()] = part3; Piece[] parts = new Piece[3]; // pass it to the snake parts[0] = head; parts[1] = part2; parts[2] = part3; return new Snake(parts); }
public void fall() { Piece imaginary = new Piece(currentPiece.getX() + 1, currentPiece.getY(), currentPiece.getPiece_bits()); boolean[][] tempResult = MatrixBoolean2DUtil.alignedNand( coordinates, currentPiece.getPiece_bits(), currentPiece.getX(), currentPiece.getY()); boolean[][] tempCoordinates = MatrixBoolean2DUtil.and(coordinates, tempResult); if (roomExist(tempCoordinates, imaginary)) { coordinates = tempCoordinates; spawn(imaginary); } else { currentPiece.hitsBottom(); checkForPoints(); currentPiece = null; } }