Question: Now modify the Board2048 object so that it will hold a 2-dimensional grid of tiles, which can be shifted in any direction. 1) Add instance
Now modify the Board2048 object so that it will hold a 2-dimensional grid of tiles, which can be shifted in any direction.
1) Add instance variables to the Board2048 class so that a Board2048 object will contain a square grid of int values of any size. (The number of rows must always be equal to the number of columns.)
2) Add public static final int constants UP, DOWN, LEFT, and RIGHT to represent the four possible directions. It doesnt matter what values you use to represent them.
3) Provide a constructor with one int parameter that specifies the desired size of the grid. (The normal game is played on a 4x4 grid but your program should handle any size.) The grid will always be square. The grid should initially be blank (all 0s).
4) Provide a second constructor with one int[][] parameter that specifies a particular grid of values to be used. You may assume that the supplied array will be square, but it may be any size. The values in the grid may be any numbers, not only powers of 2.
5) Write a standard toString method which will return a multi-line String representation of the grid. Use tab characters (\t) between the columns and newline ( ) characters between the rows. Use a '-' for a blank space. ASSIGNMENT 2: Multidimensional Arrays 2048 Puzzle COMP 1020 Winter 2018 Page 3 of 5 6) Write a method public int[] extractLine(int i, boolean vertical, boolean reverse) which will return an int[] array containing one particular row or column of the grid. If vertical is true it should return column i. If vertical is false it should return row i. If reverse is false it should return the values in the usual order (left to right or top to bottom), but if reverse is true it should return them in the reverse order. The array that is returned should be a newly-created object. For example: > Board2048 test = new Board2048(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
> test 1 2 3 4 5 6 7 8 9
> test.extractLine(1,false,false) //row 1, normal order { 4, 5, 6 }
> test.extractLine(1,false,true) //row 1, reverse order { 6, 5, 4 }
> test.extractLine(1,true,false) //column 2, normal order { 2, 5, 8 }
> test.extractLine(2,true,false) //column 2, normal order { 3, 6, 9 }
> test.extractLine(2,true,true) //column 2, normal order { 9, 6, 3 } 7) Write a method public void insertLine(int[] line, int i, boolean vertical, boolean reverse) which will work exactly like extractLine, but in reverse. The given int[] array will be copied into the specified row or column of the grid, in normal or reverse order. You may assume that the provided array is the correct size. For example: > Board2048 test = new Board2048(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
> test 1 2 3 4 5 6 7 8 9
> test.insertLine(new int[]{10,11,12},1,false,false); //Change row 1 to 10,11,12
> test 1 2 3 10 11 12 7 8 9
> test.insertLine(new int[]{13,14,15},2,true,true); //Change column 2 to 15,14,13
> test 1 2 15 10 11 14 7 8 13 8) Write a method public Board2048 shift(int direction) which will shift the entire grid in the indicated direction, producing a new Board2048 object. The existing grid in the existing Board2048 object should not be affected. This method must use extractLine, alterOneLine, and insertLine to do all of the work. If the shift direction is LEFT or RIGHT, all rows should be shifted to the left or to the right, and if the shift direction is UP or DOWN, all of the columns should be shifted up or down. For example: > Board2048 test = new Board2048(new int[][]{{2,0,2,4},{0,0,0,4},{2,2,2,2},{0,0,0,0}});
> test 2 - 2 4 - - - 4 2 2 2 2 - - - -
> Board2048 test2 = test.shift(Board2048.LEFT);
> test2 4 4 - - 4 - - - 4 4 - - - - - -
> test //should be unchanged 2 - 2 4 - - - 4 2 2 2 2 - - - -
> Board2048 test3 = test2.shift(Board2048.UP);
> test3 8 8 - -
4 - - -
- - - -
- - - -
Below is my board class.
import java.util.Arrays;
public class Board2048 { public static boolean alterOneLine(int[]board){ boolean move1 = moveleft(board); boolean move2 = false; for(int i = 0;i < board.length-1; i++){ if(board[i] != 0 && board[i+1] != 0 && board[i] == board[i+1]){ board[i] += board[i+1]; board[i+1] = 0; move2 = true; } } if(move2){ moveleft(board); } return move1 || move2 ; }
private static boolean moveleft(int[] board) { // TODO Auto-generated method stub boolean move = false; int end = -1; for(int first = 0; first< board.length; first++){ if(board[first] == 0 && end == -1){ end = first; } if(board[first] != 0 && end != -1){ int temp = board[first]; board[first] = board[end]; board[end] = temp; move = true; } System.out.println(Arrays.toString(board)); } return move; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
