Skip to content

Homework 3

 

Homework 3

First, download this hw3_templates zip file and extract the contents into a folder. The zip file contains the files OneDimensional.java (interface), and OneDimensional2048.java(implementing class). You will use these to guide you in completing this assignment.


One Dimensional 2048

Your own 2048 is edging closer to reality! Now we'll implement much of the functionality you already have, with arrays of integers instead of strings of digits. This will enable us to, among other things, go up to numbers greater than 8! You will implement a class called OneDimensional2048 in a OneDimensional2048.java file. You must implement multiple methods from the interface OneDimentional:

  • public boolean validateValue(int value, int maxPowerOfTwo)
    We must make sure that an integer value is a power of two between 2 and maxPowerOfTwo (e.g. 2048), or is set to 0. The number 0 denotes an empty square in the 2048 game.
  • public boolean validateRow(int[] row)
    Validate that a row includes only valid values. You can assume here that the maximum value we will represent is 2048. Use your validateValue method to test each integer in the row array.
  • public boolean moveLeft(int[] row)
    All non-zero integers are shifted all the way to the left of the row array. Remember that arrays are different from single int or double parameters of methods in that when we pass an array variable as an argument of a method (for example by calling moveLeft ( callingRow ) ), the array parameter it is passed into contains a reference to the array itself. So if you modify any value in the array (example: row[2] = 8), the calling function will see those changes in the calling variable(callingRow[2] is now 8). For this reason, we do not need to return a new array, but can instead simply modify the row passed in as an argument. We return a boolean: false if the row is not valid, true otherwise. You must invoke validateRow to determine the input parameter's validity.
  • public boolean combineLeft(int[] row)
    As in the previous implementations, we are not only moving all values left (no 0s are left of any non-zero values), but we are also combining like-values in values of double the value. Any value that is the result of such a combination will not be combined with another in this same invocation of this method. It is highly recommended that you use moveLeft to implement this as it should simplify your implementation greatly; however, it is not a requirement. You will be modifying the array that is passed into this method, not returning a new array.You must invoke the validateRow method to test if the input array of integers is valid. If it is not, return false. True otherwise.

Some assertions you can use to test your code. Note that for these assertions to work, you'll need to implement a method called: identicalRows, (boolean identicalRows(int[] testRow, row))) that compares the passed in row against the first argument. If the contents are the same, it returns true. Otherwise, false.

Also, note that the notation:
row = new int[]{0,1024,512,256,128,64,32,16,8,4,2,0,2,0};
is another valid way of initializing a one dimensional an array.


OneDimensional2048_solution d =new OneDimensional2048_solution();
assert(!d.validateValue(8, 4));
assert(d.validateValue(8, 8));
assert(d.validateValue(8, 16));
assert(d.validateValue(0, 16));
assert(d.validateValue(2, 2048));
assert(!d.validateValue(7, 2048));
assert(!d.validateValue(1023, 2048));
assert(!d.validateValue(1025, 2048));

assert(d.validateRow(new int[]{2, 2, 2, 2}));
assert(d.validateRow(new int[]{0, 2, 4, 0, 32}));
assert(!d.validateRow(new int[]{2, 2, 0, 3, 4, 0}));
assert(d.validateRow(new int[]{}));
assert(!d.validateRow(new int[]{8, 2, 64, 32, 30}));

row = new int[]{0,0,4,0,0};
assert(d.moveLeft(row) && d.identicalRows(new int[]{4,0,0,0,0}, row));
row = new int[]{0,0,4,0,0};
assert(d.moveLeft(row) && !d.identicalRows(new int[]{4,0,0,0,0,0}, row));
row = new int[]{2,0,4,0,0,16};
assert(d.moveLeft(row) && d.identicalRows(new int[]{2,4,16,0,0,0}, row));
row = new int[]{0,0,0};
assert(d.moveLeft(row) && d.identicalRows(new int[]{0,0,0}, row));
assert(!d.moveLeft(new int[]{2,0,31}));
row = new int[]{4,16,2048};
assert(d.moveLeft(row) && d.identicalRows(new int[]{4,16,2048}, row));

row = new int[]{8,8,16,16,32,32};
assert(d.combineLeft(row) && d.identicalRows(new int[]{16,32,64,0,0,0}, row));
row = new int[]{2,0,2,8,0,8,64,0,64,0};
assert(d.combineLeft(row) && d.identicalRows(new int[]{4,16,128,0,0,0,0,0,0,0}, row));
row = new int[]{2,0,8,2,0,64,4,0,64,0};
assert(d.combineLeft(row) && d.identicalRows(new int[]{2,8,2,64,4,64,0,0,0,0}, row));
row = new int[]{2,0,8,2,0,64,4,0,64,0};
assert(d.combineLeft(row) && d.identicalRows(new int[]{2,8,2,64,4,64,0,0,0,0}, row));
row = new int[]{0,0,2,2,128,64,0,64};
assert(d.combineLeft(row) && d.identicalRows(new int[]{4,128,128,0,0,0,0,0}, row));
row = new int[]{0,0,2,2,128,1234,64,0,64};
assert(!d.combineLeft(row));
row = new int[]{};
assert(d.combineLeft(row) && d.identicalRows(new int[]{}, row));

row = new int[]{0,1024,512,256,128,64,32,16,8,4,2,0,2,0};
assert(d.combineLeft(row) && d.combineLeft(row) && d.combineLeft(row) && d.combineLeft(row) &&
        d.combineLeft(row) && d.combineLeft(row) && d.combineLeft(row) && d.combineLeft(row) &&
        d.combineLeft(row) && d.combineLeft(row) && d.identicalRows(new int[]{2048,0,0,0,0,0,0,0,0,0,0,0,0,0}, row));
        

Notes and grading: Your implementations must compile to get any credit. You must include all of the required methods, and they must compile, even if they don't work. Please develop these in small chunks. Don't program everything, and hope it will work. Write each method separately, and test it separately. Your implementation will get credit proportional to how many assertions they pass. The methods they implement must follow the provided interfaces to get any credit. Make sure to submit your .java file, and that it has the correct class name. Your main method will not be invoked, so do not assume that it executes.

If you simply implement your methods in such a way that they are customized to the specific test cases we provide (i.e. by returning the desired string, but not having any of the logic required by the assignment), you'll receive no credit.

Submission: Please submit the .java file(s) via blackboard in a zipped folder.