From f3beee8e452d1955175f0f35fe3a9a5125cf8806 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 19:04:11 +0100 Subject: [PATCH] add getEverySecondField in ai-logic --- .../java/de/tims/fleetstorm/ai/Logic.java | 13 ++++++++ .../java/de/tims/fleetstorm/ai/LogicTest.java | 33 +++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index 3fefa1b..ddbc555 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -22,4 +22,17 @@ public class Logic { return possibleFields.get(randy.nextInt(possibleFields.size())); } + public ArrayList getEverySecondField(Matchfield matchfield) { + ArrayList out = new ArrayList(); + for (int x = 0; x < Math.sqrt(matchfield.getSize()); x++) { + for (int y = 0; y < Math.sqrt(matchfield.getSize()); y++) { + if ((x % 2 == 0 && y % 2 == 0) || (x % 2 == 1 && y % 2 == 1)) { + out.add(matchfield.getField(x, y)); + } + } + } + + return out; + } + } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index 9a201d8..96d8eae 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -1,8 +1,11 @@ package de.tims.fleetstorm.ai; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.util.ArrayList; + import org.junit.jupiter.api.Test; import de.tims.fleetstorm.matchfield.Coordinate; @@ -31,13 +34,39 @@ class LogicTest { for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { - // matchfield.setState(x, y, Matchfield.SHOT); + matchfield.setState(x, y, Coordinate.SHOT); } } - // matchfield.setState(0, 0, Matchfield.EMPTY); + matchfield.setState(2, 2, Coordinate.EMPTY); Coordinate choosenField = logic.chooseField(matchfield); int calcState = matchfield.getState(choosenField.getX(), choosenField.getY()); assertNotEquals(calcState, Coordinate.SHOT); } + + @Test + void testGetEverySecondField() { + Logic logic = new Logic(); + Matchfield matchfield; + int size = 4; + matchfield = new Matchfield(size); + matchfield.createMatchfield(); + + ArrayList everySecondField = logic.getEverySecondField(matchfield); + ArrayList expectedResult = new ArrayList(); + + expectedResult.add(new Coordinate(0, 0)); + expectedResult.add(new Coordinate(0, 2)); + expectedResult.add(new Coordinate(1, 1)); + expectedResult.add(new Coordinate(1, 3)); + expectedResult.add(new Coordinate(2, 0)); + expectedResult.add(new Coordinate(2, 2)); + expectedResult.add(new Coordinate(3, 1)); + expectedResult.add(new Coordinate(3, 3)); + + for (int i = 0; i < expectedResult.size(); i++) { + assertEquals(everySecondField.get(i).getX(), expectedResult.get(i).getX()); + assertEquals(everySecondField.get(i).getY(), expectedResult.get(i).getY()); + } + } }