Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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)
- Call public int[][] getGrid() to save a copy of the 2D array. Make sure you understand how this method works and what it does.
- 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.
- Print out the private member variable grid like method 1. (You can even do this by calling the method printBoard()).
- 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started