Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given this prompt: You'll implement the missing methods of the TwoDimensional2048 class in a comparably named file. You are required to Implement the methods defined

Given this prompt:

image text in transcribed
You'll implement the missing methods of the TwoDimensional2048 class in a comparably named file. You are required to Implement the methods defined in the interface named TwoDimensional.java. It will include the following methods: * public static boolean validate Board(into b) I strongly recommend that you make this method invoke your validateRow method in your last assignment. You can do this using the following syntax. Your OneDimensional2048.java file must be in the current directory. OneDimensional2048 oneDimension = new OneDimensional2048(); //Assuming row is the 10 array of the ith row of the copied board oneDimension. combineLeft(now): Additionally, this method checks that each row is of the same length. Note that It does allow boards that are not square. public static Int blankBoard[int x, Inty) Return a board with the specified dimensions, .e. with x number of rows, and y number of columns. All values should be initialized to zero. public static boolean IdenticalBoard(Int[ a, Intoll b) Check if the two boards that are passed in have identical values in all of their squares, Return true or false accordingly. public static int numb occupied(int[ b) Return the number of O cells on the board. This is quite useful when finding a bound on the random number you'll need to generate (see next section) to determine here a new itemn needs to be placed. public static into randCoord(intO b. int offset) Given an offset (the index of an available space, currently occupied by a O), return the xy coordinate (as an array of length ?) in the board where a new random value should be placed. In the picture below, we have three examples. Notice that the board size is 3 rows by 5 columns or 15 spaces but the available spaces are only 9. We could index each of these available spaces as if they were on an array by themselves. We call that index the offset For an offset of 0, we want to find the Oth available position on the board and return its board coordinates. The red circle highlights that location. The coordinates of the Oth empty square are row = 0 and calumn = 1. This method would, therefore, return the array [0.1] for an offset of 0. In the second example, the offeet is 4, which means we want the zero that is in the fifth available position (offset 4 spaces from the first available position). The method would return the array [1,3], which is the position in the board for that 4 offset zero (highlighted in green). In the third example, the offset is 8 (the maximum possible]. The method would return the array [2,4], which is the position in the board for that B-offset zero (highlighted in blue). public static bool aluc(into0 bj This method Is supposed to perform the function in 2048 of adding a new random value Into the board. We will do so in a very controlled manner. First, you must figure out where to put the new value. Of course it can only be in one of the spots with a zero. This method will 1. Invoke And2048.randValue( to get the value to be Inserted (a 2 or a 4). 2. Invoke offset - And2048.rand Num(numUnoccupied(b]) to figure out in which of the zeros fie. the offset of the zero square) to place the value we just generated. 3. Invoke rand Coord(b. offset) to get the [MY] coordinates in the board we'll insert the value into. This takes the randomly generated offset into the zeros from the previous step. A, Add the new value at the corresponding coordinates. This method is straight-forward, but it is important as it connects many other functionalities. . public static into0 combineLeft[inton b) Returns a copy of the board where rows have been combined left. It is Important that you first copy the Input board (using the copyBoard method) and then apply combineLeft on each row of the copied board. This is because we wish to keep the original board unchanged for the case when we want to compare before and after board states (this will be used later). You can use your previous solution to combineLeft that only works on a 10 array. If you call this for each row in your copy of board b, it should have the desired effect. You can call the combineLeft method in your previous OneDimensional2040 Implementation as such; // let's say that you've copied board b into a new board n // and that row is the ith row of the copy-board n (row = s[i]) OneDimons Lono12048. combineLeft(row): public static intO0 rotateLeft[into[ b) Take a board as input, and return a new board that is the same contents, but rotated 90 degrees to the left (counter. clockwise). This method, and combineLeft will be used together to implement up, down, and right. For Instance, to move the squares to the right. we can rotate left twice, then combineLeft, then rotate left twice. This means that we only need to Implement the logic of combine once! public static Into0 lefttinton b) public static into0 right(Intoo b) public static intOO up(into[ bj public static into0 down[into0 b] Each of these move all blocks, and combine them, in the direction specified. They will just use a combination of rotateLeft and combineLeft, and they should be pretty short (e.g. 5 lines each]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Programming questions