mirror of
https://github.com/bvanroll/battleShipAi.git
synced 2025-08-28 19:42:46 +00:00
begonnen development game onderdeel.
This commit is contained in:
@@ -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>
|
@@ -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>
|
||||
|
@@ -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];
|
||||
}
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user