Last active
August 29, 2015 14:07
-
-
Save jeffschulthies/bfe6715d736916e67b43 to your computer and use it in GitHub Desktop.
Combine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Combine2 { | |
public static void main(String[] argv) { | |
Scanner scnr = new Scanner(System.in); // talk to Gabe Monday to better | |
// understand scanner | |
// String row = scnr.next(); | |
// row = moveLeft(row); // remember to call methods | |
// row = combineLeft(row); | |
// System.out.print(row); | |
System.out.println(combineLeft("_44")); | |
// asserts | |
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("_2_") : "_2_ should be valid"; | |
assert validateRow("22_") : "22_ should be valid"; | |
assert validateRow("242") : "242 should be valid"; | |
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 "422".equals(combineLeft("44_")) : "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 doens't work!"; | |
assert "___".equals(combineLeft("---")) : "___should be invalid!"; | |
} | |
public static boolean validateChar(char ch) { | |
if (ch == '2' || ch == '4' || ch == '_' || ch == '8') { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public static boolean validateRow(String row) { | |
if (((row.charAt(0) == '_' || row.charAt(0) == '2' || row.charAt(0) == '4')) && ((row.charAt(1) == '_' || row.charAt(1) == '2' || row.charAt(1) == '4')) && ((row.charAt(2) == '_' || row.charAt(2) == '2' || row.charAt(2) == '4'))) { | |
if(row.length() !=3) { | |
return false; | |
} | |
else if(row == "___") { | |
return false; | |
} | |
else{ | |
return true; | |
} | |
} | |
else { | |
return false; | |
} | |
} | |
public static String moveLeft(String row) { | |
if (validateRow(row) == true) { | |
row = row.replace("__2", "2__"); | |
row = row.replace("_2_", "2__"); | |
row = row.replace("__4", "4__"); | |
row = row.replace("_4_", "4__"); | |
row = row.replace("_44", "44_"); | |
} | |
return row; | |
} | |
public static String combineLeft(String row) { | |
String row2 = moveLeft(row); | |
if (validateRow(row2) == true) { | |
row2 = row2.replace("22_", "4__"); | |
row2 = row2.replace("_22", "4__"); | |
row2 = row2.replace("2_2", "4__"); | |
row2 = row2.replace("222", "42_"); | |
row2 = row2.replace("44_", "8__"); | |
row2 = row2.replace("_44", "8__"); | |
row2 = row2.replace("4_4", "8__"); | |
row2 = row2.replace("444", "84_"); | |
row2 = row2.replace("422", "8__"); | |
row2 = row2.replace("442", "82_"); | |
row2 = row2.replace("224", "44_"); | |
row2 = row2.replace("244", "28_"); | |
} | |
return row2; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment