/** Updates the matrix with current board and current pentomino */ public void updateMatrix() { int[][] tmpBoard = new int[gameBoard.getBoard().length][gameBoard.getBoard()[0].length]; // Copy the old board to the tmp for (int i = 0; i < gameBoard.getBoard().length; i++) { System.arraycopy(gameBoard.getBoard()[i], 0, tmpBoard[i], 0, gameBoard.getBoard()[i].length); } // Add the pentomino to the tmpBoard for (Point p : activePentomino.getLocation()) { int newX = (int) (pentominoLocation.getX() + p.getX()); int newY = (int) (pentominoLocation.getY() + p.getY()); if (newY >= 0) tmpBoard[newX][newY] = activePentomino.getID(); } matrix = tmpBoard; }
/** Sets the location for predicted pentomino */ public void predictDrop() { predictedLocation = (Point) pentominoLocation.clone(); while (nextDropLegal(activePentomino, gameBoard.getBoard(), predictedLocation)) { predictedLocation.setLocation(predictedLocation.getX(), predictedLocation.getY() + 1); } }
public void parseBoard() { File file = new File("data.txt"); try { Scanner scanner = new Scanner(file); Integer size = Integer.parseInt(scanner.nextLine()); Integer rows = Integer.parseInt(scanner.nextLine()); Integer columns = Integer.parseInt(scanner.nextLine()); // Initialize all the squares based on column and row Square[][] squares = new Square[size][size]; Integer[] boxCoords = {rows, columns}; System.out.println("Size: "+size+" Rows: "+rows); int rowCount = 0; while (scanner.hasNextLine()) { String[] row = scanner.nextLine().split(""); for (int i = 1; i < row.length; i++) { System.out.println(row[i]); squares[rowCount][i-1] = new Square(row[i]); } rowCount++; } System.out.println("Size: "+size+" Rows: "+rows+" Columns: "+squares); Board b = new Board(size, boxCoords, squares); b.solve(); } catch(FileNotFoundException e) { } }
/** * <b>Method: </b>convertFromProtocol</br> <b>Usage: </b>{@code convertFromProtocol(string)}</br> * -------------------------------</br> Splits the String protocol received as an array. </br> * part[0] indicates the type of Move received. </br> part[1] indicates the location of the source * as RowCol without any marker in between. </br> part[2] indicates the locaiton of the * destination as RowCol without any maker in between. </br> ex) "Move 34 54" represents a normal * Move with piece at (3,4) to be moved to (5,4) on the board. * * @param protocol defined in a network Chess game * @return Move represented by protocol */ private Move convertFromProtocol(String protocol) { Board board = getBoard(); String[] part = protocol.split(" "); String type = part[0]; Piece target = board.get( new Location( Integer.parseInt(part[1].substring(0, 1)), Integer.parseInt(part[1].substring(1, 2)))); Location destination = new Location( Integer.parseInt(part[2].substring(0, 1)), Integer.parseInt(part[2].substring(1, 2))); Move move = null; if (type.equals("move")) move = new Move(target, destination); else if (type.equals("castle")) move = new Castle(target, board.get(destination)); else if (type.equals("promotion")) move = new Promotion(target, destination); return move; }
public void update(Observable o, Object arg) { String gameStatus = model.getStatus(); if (gameStatus.equals("finished")) { statusbar.setText("Game over. You won."); } else if (gameStatus.equals("paused")) { statusbar.setText("Paused"); } else if (gameStatus.equals("game over")) { statusbar.setText("Game over. You lost."); } else { statusbar.setText( "Your score: " + new Integer(model.getScore()).toString() + ", level " + new Integer(model.getLevel()).toString()); board.repaint(); } }
/** * Checks if the current game is finished * * @return True if game is finished */ public boolean gameFinished() { return suddenDeath || gameBoard.isTheGameLost(); }
/** * Handles the events for keypresses * * @param e the pressed key */ public void keyPressed(KeyEvent e) { if (activePentomino != null) { // Keys for moveing the pentomino sideways if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_RIGHT) { // Initialize direction to 0 to make sure we don't get null pointer exception // or do something weird with the movement int direction = 0; if (e.getKeyCode() == KeyEvent.VK_LEFT) direction = -1; if (e.getKeyCode() == KeyEvent.VK_RIGHT) direction = 1; // +1 right, -1 left // Check if we are allowed to actually move the pentomino to the new position boolean legalMove = true; for (Point p : activePentomino.getLocation()) { int newX = (int) (p.getX() + pentominoLocation.getX() + direction); int newY = (int) (p.getY() + pentominoLocation.getY()); // Check only if the pentomino is not above the board if (newY >= 0) { // Check that we are horizontally within the board and not trying to overlap another // piece if (newX < 0 || newX >= gameBoard.getBoard().length || gameBoard.getBoard()[newX][newY] != -1) legalMove = false; } } // Check passed so we do the actual movement of the pentomino if (legalMove) { pentominoLocation.setLocation( (pentominoLocation.getX() + direction), pentominoLocation.getY()); predictDrop(); updateMatrix(); } } // Key for rotating the pentomino if (e.getKeyCode() == KeyEvent.VK_UP) { // Create a temporary pentomino to check if the rotated position free Pentomino tmpPentomino = activePentomino.copy(); tmpPentomino.rotate(); boolean legalMove = true; // For reach block of the position check that it's free and within the grid // ignoring the check if we're above the grid for (Point p : tmpPentomino.getLocation()) { int newX = (int) (p.getX() + pentominoLocation.getX()); int newY = (int) (p.getY() + pentominoLocation.getY()); if (newY > 0) { if (newY >= gameBoard.getBoard()[0].length || newX < 0 || newX >= gameBoard.getBoard().length || gameBoard.getBoard()[newX][newY] != -1) { legalMove = false; } } } // If the check passed then simply do the rotation if (legalMove) { activePentomino.rotate(); predictDrop(); } // Update the matrix so we don't have lag in display updateMatrix(); } // Key for speeding up the fall if (e.getKeyCode() == KeyEvent.VK_DOWN) { // If next position is available, put the current pentomino to it if (nextDropLegal(activePentomino, gameBoard.getBoard(), pentominoLocation)) { pentominoLocation.setLocation(pentominoLocation.getX(), pentominoLocation.getY() + 1); } // Update to avoid lag updateMatrix(); } // Key for entirely dropping the pentomino if (e.getKeyCode() == KeyEvent.VK_ENTER) { // We make the pentomino fall as many times as it can while (nextDropLegal(activePentomino, gameBoard.getBoard(), pentominoLocation)) { pentominoLocation.setLocation(pentominoLocation.getX(), pentominoLocation.getY() + 1); } // Update to avoid lag updateMatrix(); } // Key for flipping the pentomino if (e.getKeyCode() == KeyEvent.VK_SPACE) { // Create a temporary pentomino for checking the flipped position Pentomino tmpPentomino = activePentomino.copy(); tmpPentomino.reflect(); // Check that each position in the temporary pentomino is available and within the matrix boolean legalMove = true; for (Point p : tmpPentomino.getLocation()) { int newX = (int) (p.getX() + pentominoLocation.getX()); int newY = (int) (p.getY() + pentominoLocation.getY()); if (newY < 0 || newY >= gameBoard.getBoard()[0].length || newX < 0 || newX >= gameBoard.getBoard().length || gameBoard.getBoard()[newX][newY] != -1) { legalMove = false; } } // Flip the pentomino if check is passed if (legalMove) { activePentomino.reflect(); predictDrop(); } // Update to avoid lag updateMatrix(); } // Key for storing the currently falling pentomino if (e.getKeyCode() == KeyEvent.VK_SHIFT) { // If we haven't stored after fixing the pentomino to the grid if (storageable) { // Change the active and stored pentomino and set the location to the top of the screen pentominoLocation = new Point(gameBoard.getWidth() / 2, 0); Pentomino tmp = activePentomino; activePentomino = storagedPentomino; storagedPentomino = tmp; storageable = false; // Get new prediction predictDrop(); // Update to avoid lag updateMatrix(); } } // Key for a chect for testing purposes if (e.getKeyCode() == KeyEvent.VK_X) { // If pressed enough, add score cheat++; if (cheat > 10) { cheat = 0; addScore(12); } } } }
public static void main(String[] argv) { Board board = new Board(); board.print(); }