diff --git a/.gitignore b/.gitignore index a1c2a23..62f3499 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,21 @@ +#GRADLE +.gradle +/build/ + +# Ignore Gradle GUI config +gradle-app.setting + +# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) +!gradle-wrapper.jar + +# Cache of project +.gradletasknamecache + +# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 +# gradle/wrapper/gradle-wrapper.properties + +#JAVA + # Compiled class file *.class @@ -21,3 +39,20 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + + +#MAVEN + +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +# https://github.com/takari/maven-wrapper#usage-without-binary-jar +.mvn/wrapper/maven-wrapper.jar + + diff --git a/.idea/compiler.xml b/.idea/compiler.xml index feffff8..daab56f 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -9,8 +9,8 @@ - - + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apache_commons_commons_lang3_3_9.xml b/.idea/libraries/Maven__org_apache_commons_commons_lang3_3_9.xml new file mode 100644 index 0000000..9050e00 --- /dev/null +++ b/.idea/libraries/Maven__org_apache_commons_commons_lang3_3_9.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 4f62f2e..98e58fa 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -7,7 +7,7 @@ - + \ No newline at end of file diff --git a/project/battleShipAi.iml b/project/battleShipAi.iml index e9c46ff..fad47e1 100644 --- a/project/battleShipAi.iml +++ b/project/battleShipAi.iml @@ -1,6 +1,6 @@ - + @@ -9,7 +9,8 @@ - + + \ No newline at end of file diff --git a/project/pom.xml b/project/pom.xml index 1325ccb..74f07b7 100644 --- a/project/pom.xml +++ b/project/pom.xml @@ -3,6 +3,18 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 + + + org.apache.commons + commons-lang3 + 3.9 + + + + 1.8 + 1.8 + + org.example battleShipAiTest diff --git a/project/src/main/java/com/battleShipAi/BattleShip.java b/project/src/main/java/com/battleShipAi/BattleShip.java index 45ea5e5..de48886 100644 --- a/project/src/main/java/com/battleShipAi/BattleShip.java +++ b/project/src/main/java/com/battleShipAi/BattleShip.java @@ -1,7 +1,7 @@ package com.battleShipAi; -import jdk.internal.net.http.common.Pair; +import org.apache.commons.lang3.tuple.Pair; public class BattleShip { int[][] field; @@ -33,11 +33,11 @@ public class BattleShip { for (int xi = 0; xi < width; xi++) { for (int yi = 0; yi < height; yi++) { if (returnVal == null) { - returnVal = new Pair(xi, yi); + returnVal = Pair.of(xi, yi);//new Pair(xi, yi); bestScore = scoredField[xi][yi]; } else { if (bestScore < scoredField[xi][yi]) { - returnVal = new Pair(xi, yi); + returnVal = Pair.of(xi,yi);//new Pair(xi, yi); bestScore = scoredField[xi][yi]; } } diff --git a/project/src/main/java/com/battleShipAi/Game.java b/project/src/main/java/com/battleShipAi/Game.java index 27623e4..fb5487f 100644 --- a/project/src/main/java/com/battleShipAi/Game.java +++ b/project/src/main/java/com/battleShipAi/Game.java @@ -4,85 +4,87 @@ package com.battleShipAi; import java.util.Scanner; public class Game { - //todo vars aanmaken voor bijhouden speler stats. - + static int width; + static int height; + static String[][] field; public static void main(String[] args) { + Scanner in = new Scanner(System.in); System.out.println("How wide should the playfield be?"); - int width = in.nextInt(); + width = in.nextInt(); System.out.println("How high should the playfield be?"); - int height = in.nextInt(); + height = in.nextInt(); System.out.println("How many ships do you want to play with?"); int loop = in.nextInt(); int[] ships = new int[loop]; - String[][] field = new String[width][height]; - for(String[] row : field) { - for (String cel: row) { - cell = "O"; + field = new String[width][height]; + for (int x = 0; x < width; x++) { + for(int y = 0; y < height; y++) { + field[x][y] = "O"; } } + for(int i = 0; i < loop; i++) { System.out.println(String.format("What do you want the size of ship %d to be?", i+1)); ships[i] = in.nextInt(); while(true) { System.out.println("\nDo you want to set the ship vertical or horizontal? (v/h)"); String type = in.next(); - if (type.equals("v")){ - System.out.println("Enter the column position of the top of the ship"); - int col = in.nextInt(); - System.out.println("Enter the row position of the top of the ship"); - int row = in.nextInt(); - boolean success = true; - try { - for (int ij = row; ij < ships[i]; ij++) { - if (field[col][ij].equals("S")) { - success = false; - break; - } else { - field[col][ij] = "S"; - } - } - } catch (Exception e) { - success = false; - } - if (success){ - System.out.println("Board currently looks like this:") - printBoard(); - break; - } - } else if (type.equals("h")) { - //todo refactor top part so we can reuse it here. - } - - - - System.out.println(String.format("What do you want the tail position of the ship to be? \nX:")); - int xpos1 = in.nextInt(); - System.out.println(String.format("Y:")); - int ypos1 = in.nextInt(); - - - System.out.println("Something was invalid, try again.\nFor ship with width " + ships[i]); - if (xpos > width || xpos < 0 || ypos > height || ypos < 0) System.out.println("Positions invalid, try again. \n"); - else { - + if (placeShip(in, ships[i], type.equals("v"))){ + break; + } else { + continue; } } } - for(int i = 0; i < loop; i++) { + } + + + + + + + + private static boolean placeShip(Scanner in, int ship, boolean vert) { + System.out.println("Enter the column position of the top of the ship"); + int col = in.nextInt(); + System.out.println("Enter the row position of the top of the ship"); + int row = in.nextInt(); + int turn = (vert) ? row : col; + + boolean success = (vert && ship + row < height) || (!vert && ship + col < width); + for (int ij = turn; ij < turn+ship; ij++) { + if (!vert) { + success = !(field[ij][row].equals("S")); + field[ij][row] = "S"; + } else { + success = !(field[col][ij].equals("S")); + field[col][ij] = "S"; + } + } + if (success) { + System.out.println("Board currently looks like this:"); + printBoard(); + return true; + } else { + System.out.println("Something went wrong try again"); + return false; } - - - - BattleShip opponent = new BattleShip() } - private static void printBoard() { //todo print functie die het bord toont //todo bord aanmaken + String printable = ""; + for(String[] row : field) { + for (String cell : row) { + printable += cell + " "; + } + printable += "\n"; + } + System.out.println(printable); } }