Kumite (ko͞omiˌtā) is the practice of taking techniques learned from Kata and applying them through the act of freestyle sparring.
You can create a new kumite by providing some initial code and optionally some test cases. From there other warriors can spar with you, by enhancing, refactoring and translating your code. There is no limit to how many warriors you can spar with.
A great use for kumite is to begin an idea for a kata as one. You can collaborate with other code warriors until you have it right, then you can convert it to a kata.
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test void SampleTest() { assertTrue(DrivingTestEvaluator.evaluate("SL30_4 TURNR SL35_8 STOP1", "28 27 30 29> 36 38 37 0 0 0 0 0")); assertFalse(DrivingTestEvaluator.evaluate("SL30_4 TURNR SL35_8 STOP1", "28 27 30 29> 36 38 37 0 0 0 50 30")); assertTrue(DrivingTestEvaluator.evaluate("SL50_2 SL30_4 TURNR TURNL", "48 51 21> 28 30< 22")); assertFalse(DrivingTestEvaluator.evaluate("SL30_4 TURNR SL35_7 YIELD2", "28 27 30 32> 36 38 0 0 0 0 60")); assertTrue(DrivingTestEvaluator.evaluate("SL40_3 YIELD SL25_5 TURNR", "39 40 41 24 25 23 25> 22")); } @Test void ComplexesTest() { assertTrue(DrivingTestEvaluator.evaluate("SL25_4 TURNL SL30_3 SL20_4 TURNR SL40_8 STOP1", "25 24 25 26> 30 29 30 22 18 20 24> 40 39 40 0 0 0 0 0")); assertTrue(DrivingTestEvaluator.evaluate("SL30_5 TURNL SL40_6 SL25_4 TURNR SL50_8 SL35_3 STOP1", "30 29 30 31 30> 40 39 38 40 39 40> 25 24 23 25> 50 49 50 48 50 49 50 50> 35 34 35 0 0 0 0 0")); assertFalse(DrivingTestEvaluator.evaluate("SL20_4 TURNR SL30_5 SL45_6 TURNL SL50_7 SL40_4 STOP1", "20 19 20 21> 30 29 30 31 32> 45 44 46 45 47 48> 50 49 50 51 50 52 53> 40 39 40 10 5 5 5 5")); assertFalse(DrivingTestEvaluator.evaluate("SL25_4 STOP1 SL20_4", "30 29 30 31 0 0 0 0 0 < 13 15 18 15")); assertTrue(DrivingTestEvaluator.evaluate("SL25_4 TURNL SL30_3 SL20_4 TURNR SL40_8 STOP1", "25 24 25 26> 30 29 30 22 18 20 24> 40 39 40 0 0 0 0 0")); assertFalse(DrivingTestEvaluator.evaluate("SL30_5 TURNL SL40_6 SL25_4 TURNR SL50_8 SL35_3 STOP1", "30 29 30 31 30> 40 39 38 40 39 40> 25 24 23 25> 50 49 50 48 50 49 50 50> 35 34 35 0 0 0 0 0")); assertTrue(DrivingTestEvaluator.evaluate("SL50_10 TURNL R", "50 49 50 48 50 49 50 50 > 0 0 0 0 0")); assertFalse(DrivingTestEvaluator.evaluate("SL30_5 TURNL SL40_6 YIELD SL25_4 TURNR SL50_8 SL35_3 STOP1", "30 29 30 31 30> 40 39 38 40 39 40 0 0 25 24 23 25> 50 49 50 48 50 49 50 50> 35 34 35 0 0 0 0 0")); assertTrue(DrivingTestEvaluator.evaluate("SL25_4 TURNL SL30_3 YIELD SL20_4 TURNR SL40_8 STOP1", "25 24 25 26> 30 29 30 22 18 20 24> 40 39 40 0 0 0 0 0")); assertTrue(DrivingTestEvaluator.evaluate("SL25_4 TURNL SL30_3 YIELD2 SL20_4 TURNR SL40_8 STOP1", "25 24 25 26> 30 29 30 0 0 0 0 22 18 20 24> 40 39 40 0 0 0 0 0")); } @Test void StopTest() { assertTrue(DrivingTestEvaluator.evaluate("STOP", "0 0")); assertTrue(DrivingTestEvaluator.evaluate("STOP3", "0 0 0 0 0 0 0 0")); assertTrue(DrivingTestEvaluator.evaluate("STOP1", "0 0 0 0")); assertFalse(DrivingTestEvaluator.evaluate("STOP3", "0 0 0 0 2 2 2")); assertFalse(DrivingTestEvaluator.evaluate("STOP", "0 ")); assertFalse(DrivingTestEvaluator.evaluate("STOP2", "0 0 ")); } @Test void TurnTest() { assertTrue(DrivingTestEvaluator.evaluate("TURNR SL25_1 ", "> 60")); assertTrue(DrivingTestEvaluator.evaluate("TURNR SL25_1 TURNR SL25_1 TURNL SL25_1 ", "> 20 > 25 < 15 ")); assertTrue(DrivingTestEvaluator.evaluate( "TURNR SL25_1 SL25_1 TURNR SL25_1 TURNL SL25_1 TURNR SL25_1 TURNR SL25_1 ", "> 10 > 20 < 25 > 15 > 20")); assertFalse(DrivingTestEvaluator.evaluate("TURNL SL25_1 TURNR SL25_1 TURNL SL25_1 TURNL SL25_1 TURNR SL25_1 ", "> 50 > 60 < 40 < 30 > 70 ")); assertFalse(DrivingTestEvaluator.evaluate("TURNL SL25_1 TURNR SL25_1 TURNR SL25_1 TURNL SL25_1 TURNR SL25_1 ", "> 50 > 60 > 70 < 40 > 30")); assertFalse(DrivingTestEvaluator.evaluate("TURNL SL25_1 TURNR SL25_1 TURNL SL25_1 TURNL SL25_1 TURNR SL25_1 ", "> 50 < 30 < 40 > 60 > 70 ")); assertFalse(DrivingTestEvaluator.evaluate("TURNR SL25_1 ", "< 40 ")); assertFalse(DrivingTestEvaluator.evaluate("TURNL SL25_1 TURNR SL25_1 TURNR SL25_1 TURNL SL25_1 TURNR SL25_1 ", "> 50 > 60 > 70 < 40 > 30")); } @Test void SpeedLimitTest() { } @Test void YieldTest() { } public static String generateIndications() { ArrayList<String> types = new ArrayList<>(Arrays.asList("SL", "TURNL", "TURNR", "YIELD", "STOP", "R")); StringBuilder sb = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); Random random = new Random(); sb2.append(types.get(0) + random.nextInt(20, 70) + "_" + random.nextInt(1, 10)); sb.append(sb2 + " "); for (int i = 0; i < 8; i++) { sb2.setLength(0); Collections.shuffle(types); if (types.get(0) == "YIELD" || types.get(0) == "STOP" || types.get(0) == "R") { int aleatorio = random.nextInt(0, 5); if (aleatorio == 0) { sb2.append(types.get(0)); } else { sb2.append(types.get(0) + aleatorio); } } else if (types.get(0) == "SL") { sb2.append(types.get(0) + random.nextInt(20, 70) + "_" + random.nextInt(1, 10)); } else { sb2.append(types.get(0)); } sb.append(sb2 + " "); } return sb.toString().trim(); } public static String generateGoodActions(String indications) { String[] trafficSigns = indications.split("\\s+"); List<String> actions = new ArrayList<>(); Random random = new Random(); Matcher matcher; for (String sing : trafficSigns) { if ((matcher = Pattern.compile("SL(\\d+)_(\\d+)").matcher(sing)).matches()) { int limit = Integer.parseInt(matcher.group(1)); int duration = Integer.parseInt(matcher.group(2)); for (int i = 0; i < duration; i++) { actions.add(String.valueOf(random.nextInt(limit + 1))); } } else if ((matcher = Pattern.compile("YIELD(\\d*)").matcher(sing)).matches()) { int cars = matcher.group(1).isEmpty() ? 1 : Integer.parseInt(matcher.group(1)); for (int i = 0; i < cars * 2; i++) { actions.add("0"); } } else if ((matcher = Pattern.compile("STOP(\\d*)").matcher(sing)).matches()) { int cars = matcher.group(1).isEmpty() ? 0 : Integer.parseInt(matcher.group(1)); for (int i = 0; i < 3 + 2 * cars; i++) { actions.add("0"); } } else if ((matcher = Pattern.compile("R(\\d+)").matcher(sing)).matches()) { int secs = Integer.parseInt(matcher.group(1)); for (int i = 0; i < secs; i++) { actions.add("0"); } } else if ("TURNL".equals(sing) && !actions.isEmpty()) { int last = actions.size() - 1; actions.set(last, actions.get(last) + "<"); } else if ("TURNR".equals(sing) && !actions.isEmpty()) { int last = actions.size() - 1; actions.set(last, actions.get(last) + ">"); } } return String.join(" ", actions); } @Test void RandomTest() { String indications = generateIndications(); String actions = generateGoodActions(indications); System.out.println(indications); System.out.println(actions); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
@Testvoid testSomething() {// assertEquals("expected", "actual");}- @Test
- void SampleTest() {
- assertTrue(DrivingTestEvaluator.evaluate("SL30_4 TURNR SL35_8 STOP1", "28 27 30 29> 36 38 37 0 0 0 0 0"));
- assertFalse(DrivingTestEvaluator.evaluate("SL30_4 TURNR SL35_8 STOP1", "28 27 30 29> 36 38 37 0 0 0 50 30"));
- assertTrue(DrivingTestEvaluator.evaluate("SL50_2 SL30_4 TURNR TURNL", "48 51 21> 28 30< 22"));
- assertFalse(DrivingTestEvaluator.evaluate("SL30_4 TURNR SL35_7 YIELD2", "28 27 30 32> 36 38 0 0 0 0 60"));
- assertTrue(DrivingTestEvaluator.evaluate("SL40_3 YIELD SL25_5 TURNR", "39 40 41 24 25 23 25> 22"));
- }
- @Test
- void ComplexesTest() {
- assertTrue(DrivingTestEvaluator.evaluate("SL25_4 TURNL SL30_3 SL20_4 TURNR SL40_8 STOP1",
- "25 24 25 26> 30 29 30 22 18 20 24> 40 39 40 0 0 0 0 0"));
- assertTrue(DrivingTestEvaluator.evaluate("SL30_5 TURNL SL40_6 SL25_4 TURNR SL50_8 SL35_3 STOP1",
- "30 29 30 31 30> 40 39 38 40 39 40> 25 24 23 25> 50 49 50 48 50 49 50 50> 35 34 35 0 0 0 0 0"));
- assertFalse(DrivingTestEvaluator.evaluate("SL20_4 TURNR SL30_5 SL45_6 TURNL SL50_7 SL40_4 STOP1",
- "20 19 20 21> 30 29 30 31 32> 45 44 46 45 47 48> 50 49 50 51 50 52 53> 40 39 40 10 5 5 5 5"));
- assertFalse(DrivingTestEvaluator.evaluate("SL25_4 STOP1 SL20_4", "30 29 30 31 0 0 0 0 0 < 13 15 18 15"));
- assertTrue(DrivingTestEvaluator.evaluate("SL25_4 TURNL SL30_3 SL20_4 TURNR SL40_8 STOP1",
- "25 24 25 26> 30 29 30 22 18 20 24> 40 39 40 0 0 0 0 0"));
- assertFalse(DrivingTestEvaluator.evaluate("SL30_5 TURNL SL40_6 SL25_4 TURNR SL50_8 SL35_3 STOP1",
- "30 29 30 31 30> 40 39 38 40 39 40> 25 24 23 25> 50 49 50 48 50 49 50 50> 35 34 35 0 0 0 0 0"));
- assertTrue(DrivingTestEvaluator.evaluate("SL50_10 TURNL R", "50 49 50 48 50 49 50 50 > 0 0 0 0 0"));
- assertFalse(DrivingTestEvaluator.evaluate("SL30_5 TURNL SL40_6 YIELD SL25_4 TURNR SL50_8 SL35_3 STOP1",
- "30 29 30 31 30> 40 39 38 40 39 40 0 0 25 24 23 25> 50 49 50 48 50 49 50 50> 35 34 35 0 0 0 0 0"));
- assertTrue(DrivingTestEvaluator.evaluate("SL25_4 TURNL SL30_3 YIELD SL20_4 TURNR SL40_8 STOP1",
- "25 24 25 26> 30 29 30 22 18 20 24> 40 39 40 0 0 0 0 0"));
- assertTrue(DrivingTestEvaluator.evaluate("SL25_4 TURNL SL30_3 YIELD2 SL20_4 TURNR SL40_8 STOP1",
- "25 24 25 26> 30 29 30 0 0 0 0 22 18 20 24> 40 39 40 0 0 0 0 0"));
- }
- @Test
- void StopTest() {
- assertTrue(DrivingTestEvaluator.evaluate("STOP", "0 0"));
- assertTrue(DrivingTestEvaluator.evaluate("STOP3", "0 0 0 0 0 0 0 0"));
- assertTrue(DrivingTestEvaluator.evaluate("STOP1", "0 0 0 0"));
- assertFalse(DrivingTestEvaluator.evaluate("STOP3", "0 0 0 0 2 2 2"));
- assertFalse(DrivingTestEvaluator.evaluate("STOP", "0 "));
- assertFalse(DrivingTestEvaluator.evaluate("STOP2", "0 0 "));
- }
- @Test
- void TurnTest() {
- assertTrue(DrivingTestEvaluator.evaluate("TURNR SL25_1 ", "> 60"));
- assertTrue(DrivingTestEvaluator.evaluate("TURNR SL25_1 TURNR SL25_1 TURNL SL25_1 ", "> 20 > 25 < 15 "));
- assertTrue(DrivingTestEvaluator.evaluate(
- "TURNR SL25_1 SL25_1 TURNR SL25_1 TURNL SL25_1 TURNR SL25_1 TURNR SL25_1 ",
- "> 10 > 20 < 25 > 15 > 20"));
- assertFalse(DrivingTestEvaluator.evaluate("TURNL SL25_1 TURNR SL25_1 TURNL SL25_1 TURNL SL25_1 TURNR SL25_1 ",
- "> 50 > 60 < 40 < 30 > 70 "));
- assertFalse(DrivingTestEvaluator.evaluate("TURNL SL25_1 TURNR SL25_1 TURNR SL25_1 TURNL SL25_1 TURNR SL25_1 ",
- "> 50 > 60 > 70 < 40 > 30"));
- assertFalse(DrivingTestEvaluator.evaluate("TURNL SL25_1 TURNR SL25_1 TURNL SL25_1 TURNL SL25_1 TURNR SL25_1 ",
- "> 50 < 30 < 40 > 60 > 70 "));
- assertFalse(DrivingTestEvaluator.evaluate("TURNR SL25_1 ", "< 40 "));
- assertFalse(DrivingTestEvaluator.evaluate("TURNL SL25_1 TURNR SL25_1 TURNR SL25_1 TURNL SL25_1 TURNR SL25_1 ",
- "> 50 > 60 > 70 < 40 > 30"));
- }
- @Test
- void SpeedLimitTest() {
- }
- @Test
- void YieldTest() {
- }
- public static String generateIndications() {
- ArrayList<String> types = new ArrayList<>(Arrays.asList("SL", "TURNL", "TURNR", "YIELD", "STOP", "R"));
- StringBuilder sb = new StringBuilder();
- StringBuilder sb2 = new StringBuilder();
- Random random = new Random();
- sb2.append(types.get(0) + random.nextInt(20, 70) + "_" + random.nextInt(1, 10));
- sb.append(sb2 + " ");
- for (int i = 0; i < 8; i++) {
- sb2.setLength(0);
- Collections.shuffle(types);
- if (types.get(0) == "YIELD" || types.get(0) == "STOP" || types.get(0) == "R") {
- int aleatorio = random.nextInt(0, 5);
- if (aleatorio == 0) {
- sb2.append(types.get(0));
- } else {
- sb2.append(types.get(0) + aleatorio);
- }
- } else if (types.get(0) == "SL") {
- sb2.append(types.get(0) + random.nextInt(20, 70) + "_" + random.nextInt(1, 10));
- } else {
- sb2.append(types.get(0));
- }
- sb.append(sb2 + " ");
- }
- return sb.toString().trim();
- }
- public static String generateGoodActions(String indications) {
- String[] trafficSigns = indications.split("\\s+");
- List<String> actions = new ArrayList<>();
- Random random = new Random();
- Matcher matcher;
- for (String sing : trafficSigns) {
- if ((matcher = Pattern.compile("SL(\\d+)_(\\d+)").matcher(sing)).matches()) {
- int limit = Integer.parseInt(matcher.group(1));
- int duration = Integer.parseInt(matcher.group(2));
- for (int i = 0; i < duration; i++) {
- actions.add(String.valueOf(random.nextInt(limit + 1)));
- }
- } else if ((matcher = Pattern.compile("YIELD(\\d*)").matcher(sing)).matches()) {
- int cars = matcher.group(1).isEmpty() ? 1 : Integer.parseInt(matcher.group(1));
- for (int i = 0; i < cars * 2; i++) {
- actions.add("0");
- }
- } else if ((matcher = Pattern.compile("STOP(\\d*)").matcher(sing)).matches()) {
- int cars = matcher.group(1).isEmpty() ? 0 : Integer.parseInt(matcher.group(1));
- for (int i = 0; i < 3 + 2 * cars; i++) {
- actions.add("0");
- }
- } else if ((matcher = Pattern.compile("R(\\d+)").matcher(sing)).matches()) {
- int secs = Integer.parseInt(matcher.group(1));
- for (int i = 0; i < secs; i++) {
- actions.add("0");
- }
- } else if ("TURNL".equals(sing) && !actions.isEmpty()) {
- int last = actions.size() - 1;
- actions.set(last, actions.get(last) + "<");
- } else if ("TURNR".equals(sing) && !actions.isEmpty()) {
- int last = actions.size() - 1;
- actions.set(last, actions.get(last) + ">");
- }
- }
- return String.join(" ", actions);
- }
- @Test
- void RandomTest() {
- String indications = generateIndications();
- String actions = generateGoodActions(indications);
- System.out.println(indications);
- System.out.println(actions);
- }
- }
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import java.util.Arrays; import java.util.Random; import org.junit.jupiter.api.Test; class SolutionTest { @Test void TestSample() { assertArrayEquals(new String[][]{ {"2", "+", "3", "=", "5"}, {"+", " ", "+", " ", "+"}, {"4", "+", "1", "=", "5"}, {"+", " ", "+", " ", "+"}, {"6", "+", "2", "=", "8"} }, CrossMath.solve(new String[][]{ {"2", "+", "3", "=", "C"}, {"+", " ", "+", " ", "+"}, {"A", "+", "1", "=", "5"}, {"+", " ", "+", " ", "+"}, {"6", "+", "B", "=", "8"} })); assertArrayEquals(new String[][]{ {"9", "-", "3", "=", "6"}, {"-", " ", "-", " ", "-"}, {"7", "-", "2", "=", "5"}, {"=", " ", "=", " ", "="}, {"2", "-", "1", "=", "1"} }, CrossMath.solve(new String[][]{ {"9", "-", "3", "=", "6"}, {"-", " ", "-", " ", "-"}, {"A", "-", "2", "=", "5"}, {"=", " ", "=", " ", "="}, {"B", "-", "1", "=", "C"} })); assertArrayEquals(new String[][]{ {"1", "+", "3", "=", "4"}, {"+", " ", "-", " ", "+"}, {"3", "-", "3", "=", "0"}, {"=", " ", "=", " ", "="}, {"4", "+", "0", "=", "8"} }, CrossMath.solve(new String[][]{ {"1", "+", "3", "=", "4"}, {"+", " ", "-", " ", "+"}, {"A", "-", "3", "=", "0"}, {"=", " ", "=", " ", "="}, {"B", "+", "C", "=", "8"} })); assertArrayEquals(new String[][]{ {"2", "*", "3", "=", "6"}, {"*", " ", "*", " ", "*"}, {"4", "*", "2", "=", "8"}, {"=", " ", "=", " ", "="}, {"8", "*", "6", "=", "48"} }, CrossMath.solve(new String[][]{ {"2", "*", "3", "=", "C"}, {"*", " ", "*", " ", "*"}, {"A", "*", "2", "=", "8"}, {"=", " ", "=", " ", "="}, {"8", "*", "B", "=", "48"} })); assertArrayEquals(new String[][]{ {"48", "/", "2", "=", "24"}, {"/", " ", "/", " ", "/"}, {"24", "/", "2", "=", "12"}, {"=", " ", "=", " ", "="}, {"12", "/", "2", "=", "6"} }, CrossMath.solve(new String[][]{ {"48", "/", "C", "=", "24"}, {"/", " ", "/", " ", "/"}, {"24", "/", "B", "=", "12"}, {"=", " ", "=", " ", "="}, {"A", "/", "2", "=", "6"} })); } @Test void TestBasic() { assertArrayEquals(new String[][]{ {"24", "/", "3", "=", "8"}, {"-", " ", "*", " ", "*"}, {"16", "-", "4", "=", "12"}, {"=", " ", "=", " ", "="}, {"8", "*", "12", "=", "96"} }, CrossMath.solve(new String[][]{ {"24", "/", "3", "=", "8"}, {"-", " ", "*", " ", "*"}, {"A", "-", "4", "=", "C"}, {"=", " ", "=", " ", "="}, {"8", "*", "B", "=", "96"} })); assertArrayEquals(new String[][]{ {"1", "/", "1", "=", "1"}, {"/", " ", "/", " ", "/"}, {"1", "/", "1", "=", "1"}, {"=", " ", "=", " ", "="}, {"1", "/", "1", "=", "1"} }, CrossMath.solve(new String[][]{ {"1", "/", "1", "=", "C"}, {"/", " ", "/", " ", "/"}, {"1", "/", "A", "=", "1"}, {"=", " ", "=", " ", "="}, {"B", "/", "1", "=", "1"} })); assertArrayEquals(new String[][]{ {"1", "+", "5", "=", "6"}, {"+", " ", "-", " ", "+"}, {"7", "-", "3", "=", "4"}, {"=", " ", "=", " ", "="}, {"8", "+", "2", "=", "10"} }, CrossMath.solve(new String[][]{ {"1", "+", "A", "=", "6"}, {"+", " ", "-", " ", "+"}, {"7", "-", "3", "=", "C"}, {"=", " ", "=", " ", "="}, {"B", "+", "2", "=", "10"} })); assertArrayEquals(new String[][]{ {"1", "+", "3", "=", "4"}, {"+", " ", "-", " ", "+"}, {"6", "-", "3", "=", "3"}, {"=", " ", "=", " ", "="}, {"7", "+", "0", "=", "7"} }, CrossMath.solve(new String[][]{ {"1", "+", "A", "=", "4"}, {"+", " ", "-", " ", "+"}, {"6", "-", "B", "=", "3"}, {"=", " ", "=", " ", "="}, {"7", "+", "C", "=", "7"} })); } @Test void cerosTest() { String[][] unsolved = new String[][] { {"0","+","0","=","B"}, {"*"," ","-"," ","+"}, {"A","-","0","=","D"}, {"="," ","="," ","="}, {"0","+","C","=","0"} }; String[][] expected = new String[][] { {"0","+","0","=","0"}, {"*"," ","-"," ","+"}, {"0","-","0","=","0"}, {"="," ","="," ","="}, {"0","+","0","=","0"} }; assertArrayEquals(expected, CrossMath.solve(unsolved)); } @Test void oneTest() { String[][] unsolved = new String[][] { {"1","+","A","=","1"}, {"*"," ","+"," ","*"}, {"1","-","1","=","0"}, {"="," ","="," ","="}, {"1","-","B","=","0"} }; String[][] expected = new String[][] { {"1","+","0","=","1"}, {"*"," ","+"," ","*"}, {"1","-","1","=","0"}, {"="," ","="," ","="}, {"1","-","1","=","0"} }; assertArrayEquals(expected, CrossMath.solve(unsolved)); } @Test void TestRandomA() { for(int z = 0; z < 100; z++) { String[][] board = generatePlusMinusMultiDiv(); String[][] end = generatePlusMinusMultiDiv(); for(int i = 0; i < end.length; i++) { end[i] = board[i].clone(); } for(int i = 0; i < board.length; i++) { for(int k = 0; k < board[i].length; k++) { end[i][k] = board[i][k]; } } board = incognitaCreator(board); System.out.println(Arrays.deepToString(board)); System.out.println(Arrays.deepToString(end)); assertArrayEquals(end, CrossMath.solve(board)); } } @Test void TestRandomB(){ for(int i = 0; i < 100; i++){ String[][] expected = generateSol(); String[][] input = incognitaCreator(expected); assertArrayEquals(expected,input); } } public static String[][] generateSol(){ int num1 = new Random().nextInt(1,100); int num2 = new Random().nextInt(1,100); int corners = num1 * num2; String[][] solution = new String[5][5]; for(int y = 0; y < solution.length; y++) for(int x = 0; x < solution[y].length; x++) solution[y][x] = " "; for(int i = 0; i < solution.length; i +=2) { solution[i][solution.length-2] = "="; solution[solution.length-2][i] = "="; } if (new Random().nextBoolean()) { solution[0][0] = String.valueOf(num1); solution[solution.length-1][solution.length-1]= String.valueOf(num2); solution[0][solution.length-1] = String.valueOf(num1*num2); solution[solution.length-1][0] = String.valueOf(num1*num2); } else { solution[0][solution.length-1] = String.valueOf(num1); solution[solution.length-1][0] = String.valueOf(num2); solution[0][0] = String.valueOf(num1*num2); solution[solution.length-1][solution.length-1] = String.valueOf(num1*num2); } if (Integer.parseInt(solution[0][0]) > Integer.parseInt(solution[0][solution.length-1])) { solution[0][1] = "/"; solution[0][2] = String.valueOf(Integer.parseInt(solution[0][0])/Integer.parseInt(solution[0][solution.length-1])); } else { solution[0][1] = "*"; solution[0][2] = String.valueOf(Integer.parseInt(solution[0][solution.length-1])/Integer.parseInt(solution[0][0])); } if (Integer.parseInt(solution[solution.length-1][0]) > Integer.parseInt(solution[solution.length-1][solution.length-1])) { solution[solution.length-1][1] = "/"; solution[solution.length-1][2] = String.valueOf(Integer.parseInt(solution[solution.length-1][0])/Integer.parseInt(solution[solution.length-1][solution.length-1])); } else { solution[solution.length-1][1] = "*"; solution[solution.length-1][2] = String.valueOf(Integer.parseInt(solution[solution.length-1][solution.length-1])/Integer.parseInt(solution[solution.length-1][0])); } if (Integer.parseInt(solution[0][0]) > Integer.parseInt(solution[solution.length-1][0])) { solution[1][0] = "-"; solution[2][0] = String.valueOf(Integer.parseInt(solution[0][0])-Integer.parseInt(solution[solution.length-1][0])); } else { solution[1][0] = "+"; solution[2][0] = String.valueOf(Integer.parseInt(solution[solution.length-1][0])-Integer.parseInt(solution[0][0])); } if (Integer.parseInt(solution[0][solution.length-1]) > Integer.parseInt(solution[solution.length-1][solution.length-1])) { solution[1][solution.length-1] = "-"; solution[2][solution.length-1] = String.valueOf(Integer.parseInt(solution[0][solution.length-1])-Integer.parseInt(solution[solution.length-1][solution.length-1])); } else { solution[1][solution.length-1] = "+"; solution[2][solution.length-1] = String.valueOf(Integer.parseInt(solution[solution.length-1][solution.length-1])-Integer.parseInt(solution[0][solution.length-1])); } if (Integer.parseInt(solution[0][2]) > Integer.parseInt(solution[solution.length-1][2])) { solution[1][2] = "-"; solution[2][2] = String.valueOf(Integer.parseInt(solution[0][2])-Integer.parseInt(solution[solution.length-1][2])); } else { solution[1][2] = "+"; solution[2][2] = String.valueOf(Integer.parseInt(solution[solution.length-1][2])-Integer.parseInt(solution[0][2])); } if (Integer.parseInt(solution[2][0]) > Integer.parseInt(solution[2][solution.length-1])) { solution[2][1] = "-"; } else { solution[2][1] = "+"; } return solution; } public static String[][] generatePlusMinusMultiDiv() { Random r = new Random(); String[][] moss = new String[5][5]; if(r.nextInt(2) == 1) { int mult = r.nextInt(5)+1; int base = r.nextInt(5)+5; int num = base*mult; if(base > mult) { moss[1][2] = "+"; moss[2][1] = "+"; }else { moss[1][2] = "-"; moss[2][1] = "-"; } moss[0][0] = String.valueOf(base); moss[0][1] = "+"; moss[0][2] = String.valueOf(num-base); moss[0][3] = "="; moss[0][4] = String.valueOf(num); moss[1][0] = "*"; moss[1][1] = " "; moss[1][3] = " "; moss[1][4] = "/"; moss[2][0] = String.valueOf(mult); moss[2][2] = String.valueOf(Math.abs(base-mult)); moss[2][3] = "="; moss[2][4] = String.valueOf(base); moss[3][0] = "="; moss[3][1] = " "; moss[3][2] = "="; moss[3][3] = " "; moss[3][4] = "="; moss[4][0] = String.valueOf(num); moss[4][1] = "-"; moss[4][2] = String.valueOf(num-mult); moss[4][3] = "="; moss[4][4] = String.valueOf(mult); }else { int mult = r.nextInt(9)+1; int base = r.nextInt(9)+1; int num = base*mult; if(base < mult) { moss[1][2] = "-"; moss[2][1] = "+"; }else { moss[1][2] = "+"; moss[2][1] = "-"; } moss[0][0] = String.valueOf(num); moss[0][1] = "-"; moss[0][2] = String.valueOf(num-base); moss[0][3] = "="; moss[0][4] = String.valueOf(base); moss[1][0] = "/"; moss[1][1] = " "; moss[1][3] = " "; moss[1][4] = "*"; moss[2][0] = String.valueOf(base); moss[2][2] = String.valueOf(Math.abs(base-mult)); moss[2][3] = "="; moss[2][4] = String.valueOf(mult); moss[3][0] = "="; moss[3][1] = " "; moss[3][2] = "="; moss[3][3] = " "; moss[3][4] = "="; moss[4][0] = String.valueOf(mult); moss[4][1] = "+"; moss[4][2] = String.valueOf(num-mult); moss[4][3] = "="; moss[4][4] = String.valueOf(num); } return moss; } public static String[][] incognitaCreator(String[][] moss) { String[] arr = {"A","B","C"}; Random r = new Random(); for(String a : arr) { moss[r.nextInt(3)*2][r.nextInt(3)*2] = a; } return moss; } }
- import static org.junit.jupiter.api.Assertions.assertArrayEquals;
- import java.util.Arrays;
- import java.util.Random;
- import org.junit.jupiter.api.Test;
- class SolutionTest {
- @Test
void TestSample() {assertArrayEquals(new String[][]{{"2", "+", "3", "=", "5"},{"+", " ", "+", " ", "+"},{"4", "+", "1", "=", "5"},{"+", " ", "+", " ", "+"},{"6", "+", "2", "=", "8"}}, CrossMath.solve(new String[][]{{"2", "+", "3", "=", "C"},{"+", " ", "+", " ", "+"},{"A", "+", "1", "=", "5"},{"+", " ", "+", " ", "+"},{"6", "+", "B", "=", "8"}}));assertArrayEquals(new String[][]{{"9", "-", "3", "=", "6"},{"-", " ", "-", " ", "-"},{"7", "-", "2", "=", "5"},{"=", " ", "=", " ", "="},{"2", "-", "1", "=", "1"}}, CrossMath.solve(new String[][]{{"9", "-", "3", "=", "6"},{"-", " ", "-", " ", "-"},{"A", "-", "2", "=", "5"},{"=", " ", "=", " ", "="},{"B", "-", "1", "=", "C"}}));assertArrayEquals(new String[][]{{"1", "+", "3", "=", "4"},{"+", " ", "-", " ", "+"},{"3", "-", "3", "=", "0"},{"=", " ", "=", " ", "="},{"4", "+", "0", "=", "8"}}, CrossMath.solve(new String[][]{{"1", "+", "3", "=", "4"},{"+", " ", "-", " ", "+"},{"A", "-", "3", "=", "0"},{"=", " ", "=", " ", "="},{"B", "+", "C", "=", "8"}}));- void TestSample() {
- assertArrayEquals(new String[][]{
{"2", "*", "3", "=", "6"},{"*", " ", "*", " ", "*"},{"4", "*", "2", "=", "8"},{"=", " ", "=", " ", "="},{"8", "*", "6", "=", "48"}}, CrossMath.solve(new String[][]{{"2", "*", "3", "=", "C"},{"*", " ", "*", " ", "*"},{"A", "*", "2", "=", "8"},{"=", " ", "=", " ", "="},{"8", "*", "B", "=", "48"}}));assertArrayEquals(new String[][]{{"48", "/", "2", "=", "24"},{"/", " ", "/", " ", "/"},{"24", "/", "2", "=", "12"},{"=", " ", "=", " ", "="},{"12", "/", "2", "=", "6"}}, CrossMath.solve(new String[][]{{"48", "/", "C", "=", "24"},{"/", " ", "/", " ", "/"},{"24", "/", "B", "=", "12"},{"=", " ", "=", " ", "="},{"A", "/", "2", "=", "6"}}));}- {"2", "+", "3", "=", "5"},
- {"+", " ", "+", " ", "+"},
- {"4", "+", "1", "=", "5"},
- {"+", " ", "+", " ", "+"},
- {"6", "+", "2", "=", "8"}
- }, CrossMath.solve(new String[][]{
- {"2", "+", "3", "=", "C"},
- {"+", " ", "+", " ", "+"},
- {"A", "+", "1", "=", "5"},
- {"+", " ", "+", " ", "+"},
- {"6", "+", "B", "=", "8"}
- }));
@Testvoid TestBasic() {assertArrayEquals(new String[][]{{"24", "/", "3", "=", "8"},{"-", " ", "*", " ", "*"},{"16", "-", "4", "=", "12"},{"=", " ", "=", " ", "="},{"8", "*", "12", "=", "96"}}, CrossMath.solve(new String[][]{{"24", "/", "3", "=", "8"},{"-", " ", "*", " ", "*"},{"A", "-", "4", "=", "C"},{"=", " ", "=", " ", "="},{"8", "*", "B", "=", "96"}}));- assertArrayEquals(new String[][]{
- {"9", "-", "3", "=", "6"},
- {"-", " ", "-", " ", "-"},
- {"7", "-", "2", "=", "5"},
- {"=", " ", "=", " ", "="},
- {"2", "-", "1", "=", "1"}
- }, CrossMath.solve(new String[][]{
- {"9", "-", "3", "=", "6"},
- {"-", " ", "-", " ", "-"},
- {"A", "-", "2", "=", "5"},
- {"=", " ", "=", " ", "="},
- {"B", "-", "1", "=", "C"}
- }));
assertArrayEquals(new String[][]{{"1", "/", "1", "=", "1"},{"/", " ", "/", " ", "/"},{"1", "/", "1", "=", "1"},{"=", " ", "=", " ", "="},{"1", "/", "1", "=", "1"}}, CrossMath.solve(new String[][]{{"1", "/", "1", "=", "C"},{"/", " ", "/", " ", "/"},{"1", "/", "A", "=", "1"},{"=", " ", "=", " ", "="},{"B", "/", "1", "=", "1"}}));- assertArrayEquals(new String[][]{
- {"1", "+", "3", "=", "4"},
- {"+", " ", "-", " ", "+"},
- {"3", "-", "3", "=", "0"},
- {"=", " ", "=", " ", "="},
- {"4", "+", "0", "=", "8"}
- }, CrossMath.solve(new String[][]{
- {"1", "+", "3", "=", "4"},
- {"+", " ", "-", " ", "+"},
- {"A", "-", "3", "=", "0"},
- {"=", " ", "=", " ", "="},
- {"B", "+", "C", "=", "8"}
- }));
assertArrayEquals(new String[][]{{"1", "+", "5", "=", "6"},{"+", " ", "-", " ", "+"},{"7", "-", "3", "=", "4"},{"=", " ", "=", " ", "="},{"8", "+", "2", "=", "10"}}, CrossMath.solve(new String[][]{{"1", "+", "A", "=", "6"},{"+", " ", "-", " ", "+"},{"7", "-", "3", "=", "C"},{"=", " ", "=", " ", "="},{"B", "+", "2", "=", "10"}}));assertArrayEquals(new String[][]{{"1", "+", "3", "=", "4"},{"+", " ", "-", " ", "+"},{"6", "-", "3", "=", "3"},{"=", " ", "=", " ", "="},{"7", "+", "0", "=", "7"}}, CrossMath.solve(new String[][]{{"1", "+", "A", "=", "4"},{"+", " ", "-", " ", "+"},{"6", "-", "B", "=", "3"},{"=", " ", "=", " ", "="},{"7", "+", "C", "=", "7"}}));}- assertArrayEquals(new String[][]{
- {"2", "*", "3", "=", "6"},
- {"*", " ", "*", " ", "*"},
- {"4", "*", "2", "=", "8"},
- {"=", " ", "=", " ", "="},
- {"8", "*", "6", "=", "48"}
- }, CrossMath.solve(new String[][]{
- {"2", "*", "3", "=", "C"},
- {"*", " ", "*", " ", "*"},
- {"A", "*", "2", "=", "8"},
- {"=", " ", "=", " ", "="},
- {"8", "*", "B", "=", "48"}
- }));
- assertArrayEquals(new String[][]{
- {"48", "/", "2", "=", "24"},
- {"/", " ", "/", " ", "/"},
- {"24", "/", "2", "=", "12"},
- {"=", " ", "=", " ", "="},
- {"12", "/", "2", "=", "6"}
- }, CrossMath.solve(new String[][]{
- {"48", "/", "C", "=", "24"},
- {"/", " ", "/", " ", "/"},
- {"24", "/", "B", "=", "12"},
- {"=", " ", "=", " ", "="},
- {"A", "/", "2", "=", "6"}
- }));
- }
- @Test
- void TestBasic() {
- assertArrayEquals(new String[][]{
- {"24", "/", "3", "=", "8"},
- {"-", " ", "*", " ", "*"},
- {"16", "-", "4", "=", "12"},
- {"=", " ", "=", " ", "="},
- {"8", "*", "12", "=", "96"}
- }, CrossMath.solve(new String[][]{
- {"24", "/", "3", "=", "8"},
- {"-", " ", "*", " ", "*"},
- {"A", "-", "4", "=", "C"},
- {"=", " ", "=", " ", "="},
- {"8", "*", "B", "=", "96"}
- }));
- assertArrayEquals(new String[][]{
- {"1", "/", "1", "=", "1"},
- {"/", " ", "/", " ", "/"},
- {"1", "/", "1", "=", "1"},
- {"=", " ", "=", " ", "="},
- {"1", "/", "1", "=", "1"}
- }, CrossMath.solve(new String[][]{
- {"1", "/", "1", "=", "C"},
- {"/", " ", "/", " ", "/"},
- {"1", "/", "A", "=", "1"},
- {"=", " ", "=", " ", "="},
- {"B", "/", "1", "=", "1"}
- }));
- assertArrayEquals(new String[][]{
- {"1", "+", "5", "=", "6"},
- {"+", " ", "-", " ", "+"},
- {"7", "-", "3", "=", "4"},
- {"=", " ", "=", " ", "="},
- {"8", "+", "2", "=", "10"}
- }, CrossMath.solve(new String[][]{
- {"1", "+", "A", "=", "6"},
- {"+", " ", "-", " ", "+"},
- {"7", "-", "3", "=", "C"},
- {"=", " ", "=", " ", "="},
- {"B", "+", "2", "=", "10"}
- }));
- assertArrayEquals(new String[][]{
- {"1", "+", "3", "=", "4"},
- {"+", " ", "-", " ", "+"},
- {"6", "-", "3", "=", "3"},
- {"=", " ", "=", " ", "="},
- {"7", "+", "0", "=", "7"}
- }, CrossMath.solve(new String[][]{
- {"1", "+", "A", "=", "4"},
- {"+", " ", "-", " ", "+"},
- {"6", "-", "B", "=", "3"},
- {"=", " ", "=", " ", "="},
- {"7", "+", "C", "=", "7"}
- }));
- }
- @Test
- void cerosTest() {
- String[][] unsolved = new String[][] {
- {"0","+","0","=","B"},
- {"*"," ","-"," ","+"},
- {"A","-","0","=","D"},
- {"="," ","="," ","="},
- {"0","+","C","=","0"}
- };
- String[][] expected = new String[][] {
- {"0","+","0","=","0"},
- {"*"," ","-"," ","+"},
- {"0","-","0","=","0"},
- {"="," ","="," ","="},
- {"0","+","0","=","0"}
- };
- assertArrayEquals(expected, CrossMath.solve(unsolved));
- }
- @Test
- void oneTest() {
- String[][] unsolved = new String[][] {
- {"1","+","A","=","1"},
- {"*"," ","+"," ","*"},
- {"1","-","1","=","0"},
- {"="," ","="," ","="},
- {"1","-","B","=","0"}
- };
- String[][] expected = new String[][] {
- {"1","+","0","=","1"},
- {"*"," ","+"," ","*"},
- {"1","-","1","=","0"},
- {"="," ","="," ","="},
- {"1","-","1","=","0"}
- };
- assertArrayEquals(expected, CrossMath.solve(unsolved));
- }
- @Test
- void TestRandomA() {
- for(int z = 0; z < 100; z++) {
- String[][] board = generatePlusMinusMultiDiv();
- String[][] end = generatePlusMinusMultiDiv();
- for(int i = 0; i < end.length; i++) {
- end[i] = board[i].clone();
- }
- for(int i = 0; i < board.length; i++) {
- for(int k = 0; k < board[i].length; k++) {
- end[i][k] = board[i][k];
- }
- }
- board = incognitaCreator(board);
- System.out.println(Arrays.deepToString(board));
- System.out.println(Arrays.deepToString(end));
- assertArrayEquals(end, CrossMath.solve(board));
- }
- }
- @Test
- void TestRandomB(){
- for(int i = 0; i < 100; i++){
- String[][] expected = generateSol();
- String[][] input = incognitaCreator(expected);
- assertArrayEquals(expected,input);
- }
- }
- public static String[][] generateSol(){
- int num1 = new Random().nextInt(1,100);
- int num2 = new Random().nextInt(1,100);
- int corners = num1 * num2;
- String[][] solution = new String[5][5];
- for(int y = 0; y < solution.length; y++)
- for(int x = 0; x < solution[y].length; x++)
- solution[y][x] = " ";
- for(int i = 0; i < solution.length; i +=2) {
- solution[i][solution.length-2] = "=";
- solution[solution.length-2][i] = "=";
- }
- if (new Random().nextBoolean()) {
- solution[0][0] = String.valueOf(num1);
- solution[solution.length-1][solution.length-1]= String.valueOf(num2);
- solution[0][solution.length-1] = String.valueOf(num1*num2);
- solution[solution.length-1][0] = String.valueOf(num1*num2);
- } else {
- solution[0][solution.length-1] = String.valueOf(num1);
- solution[solution.length-1][0] = String.valueOf(num2);
- solution[0][0] = String.valueOf(num1*num2);
- solution[solution.length-1][solution.length-1] = String.valueOf(num1*num2);
- }
- if (Integer.parseInt(solution[0][0]) > Integer.parseInt(solution[0][solution.length-1])) {
- solution[0][1] = "/";
- solution[0][2] = String.valueOf(Integer.parseInt(solution[0][0])/Integer.parseInt(solution[0][solution.length-1]));
- } else {
- solution[0][1] = "*";
- solution[0][2] = String.valueOf(Integer.parseInt(solution[0][solution.length-1])/Integer.parseInt(solution[0][0]));
- }
- if (Integer.parseInt(solution[solution.length-1][0]) > Integer.parseInt(solution[solution.length-1][solution.length-1])) {
- solution[solution.length-1][1] = "/";
- solution[solution.length-1][2] = String.valueOf(Integer.parseInt(solution[solution.length-1][0])/Integer.parseInt(solution[solution.length-1][solution.length-1]));
- } else {
- solution[solution.length-1][1] = "*";
- solution[solution.length-1][2] = String.valueOf(Integer.parseInt(solution[solution.length-1][solution.length-1])/Integer.parseInt(solution[solution.length-1][0]));
- }
- if (Integer.parseInt(solution[0][0]) > Integer.parseInt(solution[solution.length-1][0])) {
- solution[1][0] = "-";
- solution[2][0] = String.valueOf(Integer.parseInt(solution[0][0])-Integer.parseInt(solution[solution.length-1][0]));
- } else {
- solution[1][0] = "+";
- solution[2][0] = String.valueOf(Integer.parseInt(solution[solution.length-1][0])-Integer.parseInt(solution[0][0]));
- }
- if (Integer.parseInt(solution[0][solution.length-1]) > Integer.parseInt(solution[solution.length-1][solution.length-1])) {
- solution[1][solution.length-1] = "-";
- solution[2][solution.length-1] = String.valueOf(Integer.parseInt(solution[0][solution.length-1])-Integer.parseInt(solution[solution.length-1][solution.length-1]));
- } else {
- solution[1][solution.length-1] = "+";
- solution[2][solution.length-1] = String.valueOf(Integer.parseInt(solution[solution.length-1][solution.length-1])-Integer.parseInt(solution[0][solution.length-1]));
- }
- if (Integer.parseInt(solution[0][2]) > Integer.parseInt(solution[solution.length-1][2])) {
- solution[1][2] = "-";
- solution[2][2] = String.valueOf(Integer.parseInt(solution[0][2])-Integer.parseInt(solution[solution.length-1][2]));
- } else {
- solution[1][2] = "+";
- solution[2][2] = String.valueOf(Integer.parseInt(solution[solution.length-1][2])-Integer.parseInt(solution[0][2]));
- }
- if (Integer.parseInt(solution[2][0]) > Integer.parseInt(solution[2][solution.length-1])) {
- solution[2][1] = "-";
- } else {
- solution[2][1] = "+";
- }
- return solution;
- }
- public static String[][] generatePlusMinusMultiDiv() {
- Random r = new Random();
- String[][] moss = new String[5][5];
- if(r.nextInt(2) == 1) {
- int mult = r.nextInt(5)+1;
- int base = r.nextInt(5)+5;
- int num = base*mult;
- if(base > mult) {
- moss[1][2] = "+";
- moss[2][1] = "+";
- }else {
- moss[1][2] = "-";
- moss[2][1] = "-";
- }
- moss[0][0] = String.valueOf(base);
- moss[0][1] = "+";
- moss[0][2] = String.valueOf(num-base);
- moss[0][3] = "=";
- moss[0][4] = String.valueOf(num);
- moss[1][0] = "*";
- moss[1][1] = " ";
- moss[1][3] = " ";
- moss[1][4] = "/";
- moss[2][0] = String.valueOf(mult);
- moss[2][2] = String.valueOf(Math.abs(base-mult));
- moss[2][3] = "=";
- moss[2][4] = String.valueOf(base);
- moss[3][0] = "=";
- moss[3][1] = " ";
- moss[3][2] = "=";
- moss[3][3] = " ";
- moss[3][4] = "=";
- moss[4][0] = String.valueOf(num);
- moss[4][1] = "-";
- moss[4][2] = String.valueOf(num-mult);
- moss[4][3] = "=";
- moss[4][4] = String.valueOf(mult);
- }else {
- int mult = r.nextInt(9)+1;
- int base = r.nextInt(9)+1;
- int num = base*mult;
- if(base < mult) {
- moss[1][2] = "-";
- moss[2][1] = "+";
- }else {
- moss[1][2] = "+";
- moss[2][1] = "-";
- }
- moss[0][0] = String.valueOf(num);
- moss[0][1] = "-";
- moss[0][2] = String.valueOf(num-base);
- moss[0][3] = "=";
- moss[0][4] = String.valueOf(base);
- moss[1][0] = "/";
- moss[1][1] = " ";
- moss[1][3] = " ";
- moss[1][4] = "*";
- moss[2][0] = String.valueOf(base);
- moss[2][2] = String.valueOf(Math.abs(base-mult));
- moss[2][3] = "=";
- moss[2][4] = String.valueOf(mult);
- moss[3][0] = "=";
- moss[3][1] = " ";
- moss[3][2] = "=";
- moss[3][3] = " ";
- moss[3][4] = "=";
- moss[4][0] = String.valueOf(mult);
- moss[4][1] = "+";
- moss[4][2] = String.valueOf(num-mult);
- moss[4][3] = "=";
- moss[4][4] = String.valueOf(num);
- }
- return moss;
- }
- public static String[][] incognitaCreator(String[][] moss) {
- String[] arr = {"A","B","C"};
- Random r = new Random();
- for(String a : arr) {
- moss[r.nextInt(3)*2][r.nextInt(3)*2] = a;
- }
- return moss;
- }
- }
Original seems to do unnecessary computation before ultimately returning True