begonnen development game onderdeel.

This commit is contained in:
beppe
2020-01-11 15:36:34 +01:00
parent 27ef239de7
commit c2199b2330
8 changed files with 124 additions and 61 deletions

35
.gitignore vendored
View File

@@ -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

4
.idea/compiler.xml generated
View File

@@ -9,8 +9,8 @@
<module name="battleShipAi" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="battleShipAi" target="1.5" />
<bytecodeTargetLevel target="8">
<module name="battleShipAi" target="1.8" />
</bytecodeTargetLevel>
</component>
</project>

View 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
View File

@@ -7,7 +7,7 @@
</list>
</option>
</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" />
</component>
</project>

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<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-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
@@ -9,7 +9,8 @@
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="jdk" jdkName="12" jdkType="JavaSDK" />
<orderEntry type="jdk" jdkName="13" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.9" level="project" />
</component>
</module>

View File

@@ -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">
<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>
<artifactId>battleShipAiTest</artifactId>

View File

@@ -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<Integer, Integer>(xi, yi);
returnVal = Pair.of(xi, yi);//new Pair<Integer, Integer>(xi, yi);
bestScore = scoredField[xi][yi];
} else {
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];
}
}

View File

@@ -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);
}
}