mirror of
https://github.com/bvanroll/battleShipAi.git
synced 2025-08-29 03:52:41 +00:00
begonnen development game onderdeel.
This commit is contained in:
35
.gitignore
vendored
35
.gitignore
vendored
@@ -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
|
# Compiled class file
|
||||||
*.class
|
*.class
|
||||||
|
|
||||||
@@ -21,3 +39,20 @@
|
|||||||
|
|
||||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||||
hs_err_pid*
|
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
|
||||||
|
|
||||||
|
|
||||||
|
4
.idea/compiler.xml
generated
4
.idea/compiler.xml
generated
@@ -9,8 +9,8 @@
|
|||||||
<module name="battleShipAi" />
|
<module name="battleShipAi" />
|
||||||
</profile>
|
</profile>
|
||||||
</annotationProcessing>
|
</annotationProcessing>
|
||||||
<bytecodeTargetLevel>
|
<bytecodeTargetLevel target="8">
|
||||||
<module name="battleShipAi" target="1.5" />
|
<module name="battleShipAi" target="1.8" />
|
||||||
</bytecodeTargetLevel>
|
</bytecodeTargetLevel>
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
13
.idea/libraries/Maven__org_apache_commons_commons_lang3_3_9.xml
generated
Normal file
13
.idea/libraries/Maven__org_apache_commons_commons_lang3_3_9.xml
generated
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<component name="libraryTable">
|
||||||
|
<library name="Maven: org.apache.commons:commons-lang3:3.9">
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC>
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/apache/commons/commons-lang3/3.9/commons-lang3-3.9-javadoc.jar!/" />
|
||||||
|
</JAVADOC>
|
||||||
|
<SOURCES>
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/apache/commons/commons-lang3/3.9/commons-lang3-3.9-sources.jar!/" />
|
||||||
|
</SOURCES>
|
||||||
|
</library>
|
||||||
|
</component>
|
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -7,7 +7,7 @@
|
|||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_13" default="false" project-jdk-name="13" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
|
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
|
||||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
|
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
|
||||||
<output url="file://$MODULE_DIR$/target/classes" />
|
<output url="file://$MODULE_DIR$/target/classes" />
|
||||||
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
@@ -9,7 +9,8 @@
|
|||||||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
|
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="jdk" jdkName="12" jdkType="JavaSDK" />
|
<orderEntry type="jdk" jdkName="13" jdkType="JavaSDK" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.9" level="project" />
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
@@ -3,6 +3,18 @@
|
|||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
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">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>3.9</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
|
||||||
<groupId>org.example</groupId>
|
<groupId>org.example</groupId>
|
||||||
<artifactId>battleShipAiTest</artifactId>
|
<artifactId>battleShipAiTest</artifactId>
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
package com.battleShipAi;
|
package com.battleShipAi;
|
||||||
|
|
||||||
|
|
||||||
import jdk.internal.net.http.common.Pair;
|
import org.apache.commons.lang3.tuple.Pair;
|
||||||
|
|
||||||
public class BattleShip {
|
public class BattleShip {
|
||||||
int[][] field;
|
int[][] field;
|
||||||
@@ -33,11 +33,11 @@ public class BattleShip {
|
|||||||
for (int xi = 0; xi < width; xi++) {
|
for (int xi = 0; xi < width; xi++) {
|
||||||
for (int yi = 0; yi < height; yi++) {
|
for (int yi = 0; yi < height; yi++) {
|
||||||
if (returnVal == null) {
|
if (returnVal == null) {
|
||||||
returnVal = new Pair<Integer, Integer>(xi, yi);
|
returnVal = Pair.of(xi, yi);//new Pair<Integer, Integer>(xi, yi);
|
||||||
bestScore = scoredField[xi][yi];
|
bestScore = scoredField[xi][yi];
|
||||||
} else {
|
} else {
|
||||||
if (bestScore < scoredField[xi][yi]) {
|
if (bestScore < scoredField[xi][yi]) {
|
||||||
returnVal = new Pair<Integer, Integer>(xi, yi);
|
returnVal = Pair.of(xi,yi);//new Pair<Integer, Integer>(xi, yi);
|
||||||
bestScore = scoredField[xi][yi];
|
bestScore = scoredField[xi][yi];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -4,85 +4,87 @@ package com.battleShipAi;
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Game {
|
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) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
Scanner in = new Scanner(System.in);
|
Scanner in = new Scanner(System.in);
|
||||||
System.out.println("How wide should the playfield be?");
|
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?");
|
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?");
|
System.out.println("How many ships do you want to play with?");
|
||||||
int loop = in.nextInt();
|
int loop = in.nextInt();
|
||||||
int[] ships = new int[loop];
|
int[] ships = new int[loop];
|
||||||
String[][] field = new String[width][height];
|
field = new String[width][height];
|
||||||
for(String[] row : field) {
|
for (int x = 0; x < width; x++) {
|
||||||
for (String cel: row) {
|
for(int y = 0; y < height; y++) {
|
||||||
cell = "O";
|
field[x][y] = "O";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int i = 0; i < loop; i++) {
|
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));
|
System.out.println(String.format("What do you want the size of ship %d to be?", i+1));
|
||||||
ships[i] = in.nextInt();
|
ships[i] = in.nextInt();
|
||||||
while(true) {
|
while(true) {
|
||||||
System.out.println("\nDo you want to set the ship vertical or horizontal? (v/h)");
|
System.out.println("\nDo you want to set the ship vertical or horizontal? (v/h)");
|
||||||
String type = in.next();
|
String type = in.next();
|
||||||
if (type.equals("v")){
|
if (placeShip(in, ships[i], type.equals("v"))){
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private static boolean placeShip(Scanner in, int ship, boolean vert) {
|
||||||
System.out.println("Enter the column position of the top of the ship");
|
System.out.println("Enter the column position of the top of the ship");
|
||||||
int col = in.nextInt();
|
int col = in.nextInt();
|
||||||
System.out.println("Enter the row position of the top of the ship");
|
System.out.println("Enter the row position of the top of the ship");
|
||||||
int row = in.nextInt();
|
int row = in.nextInt();
|
||||||
boolean success = true;
|
int turn = (vert) ? row : col;
|
||||||
try {
|
|
||||||
for (int ij = row; ij < ships[i]; ij++) {
|
boolean success = (vert && ship + row < height) || (!vert && ship + col < width);
|
||||||
if (field[col][ij].equals("S")) {
|
for (int ij = turn; ij < turn+ship; ij++) {
|
||||||
success = false;
|
if (!vert) {
|
||||||
break;
|
success = !(field[ij][row].equals("S"));
|
||||||
|
field[ij][row] = "S";
|
||||||
} else {
|
} else {
|
||||||
|
success = !(field[col][ij].equals("S"));
|
||||||
field[col][ij] = "S";
|
field[col][ij] = "S";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
if (success) {
|
||||||
success = false;
|
System.out.println("Board currently looks like this:");
|
||||||
}
|
|
||||||
if (success){
|
|
||||||
System.out.println("Board currently looks like this:")
|
|
||||||
printBoard();
|
printBoard();
|
||||||
break;
|
return true;
|
||||||
}
|
} else {
|
||||||
} else if (type.equals("h")) {
|
System.out.println("Something went wrong try again");
|
||||||
//todo refactor top part so we can reuse it here.
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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 {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
for(int i = 0; i < loop; i++) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
BattleShip opponent = new BattleShip()
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void printBoard() {
|
private static void printBoard() {
|
||||||
//todo print functie die het bord toont
|
//todo print functie die het bord toont
|
||||||
//todo bord aanmaken
|
//todo bord aanmaken
|
||||||
|
String printable = "";
|
||||||
|
for(String[] row : field) {
|
||||||
|
for (String cell : row) {
|
||||||
|
printable += cell + " ";
|
||||||
|
}
|
||||||
|
printable += "\n";
|
||||||
|
}
|
||||||
|
System.out.println(printable);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user