Question: Implement public void printBoard(String direction) Call public int[][] getGrid() to save a copy of the 2D array. Make sure you understand how this method works

Implement

public void printBoard(String direction)
  1. Call public int[][] getGrid() to save a copy of the 2D array. Make sure you understand how this method works and what it does.
  2. Make a function call to public boolean move(String direction) by passing the direction from the parameter. This will perform one step of action which makes changes to the current grid.
  3. Print out the private member variable grid like method 1. (You can even do this by calling the method printBoard()).
  4. Call public void setGrid(int [][] gridIn) and pass in the saved 2D array from step 1 to reset the board to its original state. The methods: public void setGrid(int[][] newGrid) { for (int r = 0; r < this.grid.length; r++) { for (int c = 0; c < this.grid[r].length; c++) { this.grid[r][c] = newGrid[r][c]; } } } public int[][] getGrid() { int[][] gridCopy = new int[this.GRID_SIZE][this.GRID_SIZE]; for (int r = 0; r < this.grid.length; r++) { for (int c = 0; c < this.grid[r].length; c++) { gridCopy[r][c] = this.grid[r][c]; } } return gridCopy; }

    public boolean move(String direction) { /* if canMove is false, exit and don't move tiles */ if (!this.canMove(direction)) return false;

    /* move in relationship to the direction passed in */ if (direction.equals(this.UP)) { this.moveUp(); } else if (direction.equals(this.RIGHT)) { this.moveRight(); } else if (direction.equals(this.DOWN)) { this.moveDown(); } else if (direction.equals(this.LEFT)) { this.moveLeft(); } else { return false; }

    return true; } public void printBoard() { for(int row=0;row

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!