Question
Simplified 2048 Left Shift Second, you'll complete a class that emulates some of the logic in 2048. You will complete the Combine class (in the
Simplified 2048 Left Shift
Second, you'll complete a class that emulates some of the logic in 2048. You will complete the Combine class (in the Combine.java file) that implements the Combinable interface with a number of methods specified below. First, have a look at and play a few games of 2048 to get an idea of what we're eventually going to be shooting for. 2048 can be a little like a drug addiction, so be sure to pry yourself away, and back to this homework. We aren't at the level yet where all of it can be implemented, but you'll implement the shuffling left of blocks with the collapsing of two blocks of like values together into a single block of double the value.
We will represent a single row of the 2048 square that includes only three blocks with a 3-length string. Each character in the string is either a '4', a '2', or a '_' which designates a "blank" space. Example rows are "42_", "4_4", and "242". Your program will execute a "shift blocks left" operation which will include combining like blocks into one of double the value. Note that newly created (double valued) blocks are not combined with other blocks. Examples of proper shift left operations include:
- shifted original
- "4__" "__4"
- "8__" "_44"
- "42_" "222"
- "4__" "2_2"
- "242" "242"
- "44_" "422"
Note that for this, you cannot use loops. We haven't learned them yet, and you should be able to complete the assignment with only concepts we've learned so far.
Error Checking: Your code must handle quite a bit of error checking. The strings being passed in must:
- include only the characters '4', '2' and '_',
- have exactly a length of three.
If any input string is not formatted according to this, you must return the string, unmodified.
You will implement four methods:
- public boolean validateChar(char ch)
This method takes a character, and validates that it is one of the allowed characters in the input string. It returns true if it is, and false otherwise.
- public boolean validateRow(String row)
This method takes a string as an argument, and checks that it adheres to the requirements for the input string above in "Error Checking". Return true if it the string is valid, and false otherwise. It must invoke the validateChar method for each character.
- public String moveLeft(String row)
If the row is not valid (you must invoke the validateRow method to determine this), return the string unmodified. Otherwise, return a new string with all of the non-blank spaces (i.e. '2's and '4's) shifted all the way to the left. This method does not combine blocks.
- public String combineLeft(String row)
This method first calls validateRow to determine that the input string is formatted correctly. If it isn't, then it returns the input unchanged. Otherwise, it calls moveLeft to move all blocks to the far left (this simplifies the problem). Then it must combine any blocks of comparable value starting from the left of the string. When two blocks are combined into a block of twice the value, any other blocks will need to be shifted left. Return the new string with blocks shifted left, and combined appropriately.
Example Assertions:
assert validateChar('2') : "2 should be a valid char"; assert validateChar('4') : "4 should be a valid char"; assert validateChar('_') : "_ should be a valid char"; assert !validateChar(' ') : "space should not be a valid char"; assert !validateChar('3') : "3 should not be a valid char"; assert !validateRow("2222") : "2222 should not be valid"; assert !validateRow("__") : "__ should not be valid"; assert !validateRow("aaa") : "aaa should not be valid"; assert !validateRow("333") : "333 should not be valid"; assert validateRow("22_") : "22_ should be valid"; assert validateRow("_2_") : "_2_ should be valid"; assert validateRow("242") : "242 should be valid"; assert "4__".equals(moveLeft("__4")) : "__4 doesn't change to 4__"; assert "4__".equals(moveLeft("_4_")) : "_4_ doesn't change to 4__"; assert "42_".equals(moveLeft("4_2")) : "4_2 doesn't change to 42_"; assert "4__".equals(combineLeft("__4")) : "__4 doesn't change to 4__"; assert "8__".equals(combineLeft("_44")) : "_44 doesn't change to 8__"; assert "4__".equals(combineLeft("2_2")) : "2_2 doesn't change to 4__"; assert "4__".equals(combineLeft("22_")) : "22_ doesn't change to 4__"; assert "42_".equals(combineLeft("222")) : "222 doesn't change to 42_"; assert "44_".equals(combineLeft("422")) : "422 doesn't change to 44_"; assert "242".equals(combineLeft("242")) : "242 doesn't change to 242"; assert "8__".equals(combineLeft(combineLeft("422"))) : "Double invocation doesn't work!"; // You should be using your validate methods to check for erroneous inputs! assert "_222".equals(combineLeft("_222")) : "2222 should be invalid!"; assert "333".equals(combineLeft("333")) : "333 should be invalid!"; assert "__".equals(combineLeft("__")) : "__ should be invalid!";
Using:
import java.lang.*; public interface Combinable { // Validate each Char public boolean validateChar(char ch); // Validate each Char in row with size 3 public boolean validateRow(String row); // Moving the Row to the left "_24" > "24_" or "__2" > "2__" public String moveLeft(String row); // This Method validates the row and then takes the String row // and combines pairs of 2's or 4's and shifts to the left public String combineLeft(String row); }
For:
class Combine implements combinable {
//ANSWER HERE
The main is used to test your code public static void main(String[] args) { The following are asserts used to test your code Combine A = new Combine(); assert A.validateChar('2') : "2 should be a valid char"; assert A.validateChar('4') : "4 should be a valid char"; assert A.validateRow("242") : "242 should be valid"; assert !A.validateRow("246") : "246 should NOT be valid"; assert "4__".equals(A.moveLeft("__4")) : "__4 doesn't change to 4__"; assert "24_".equals(A.moveLeft("2_4")) : "2_4 doesn't change to 24_"; assert "242".equals(A.combineLeft("242")) : "242 doesn't change to 242"; assert "4__".equals(A.combineLeft("22_")) : "22_ doesn't change to 4__"; assert "8__".equals(A.combineLeft(A.combineLeft("422"))) : "Double invocation doesn't work!"; assert "84_".equals(A.combineLeft(A.combineLeft("444"))) : "Double invocation doesn't work!"; You should be using your own validation asserts to check for erroneous inputs! assert "__".equals(A.combineLeft("__")) : "__ should be invalid!"; System.out.println("All tests passed. VICTORY!"); }
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