mirror of
https://github.com/bvanroll/battleShipAi.git
synced 2025-12-12 11:46:15 +00:00
begonnen development game onderdeel.
This commit is contained in:
16
.idea/compiler.xml
generated
Normal file
16
.idea/compiler.xml
generated
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="CompilerConfiguration">
|
||||||
|
<annotationProcessing>
|
||||||
|
<profile name="Maven default annotation processors profile" enabled="true">
|
||||||
|
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||||
|
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||||
|
<outputRelativeToContentRoot value="true" />
|
||||||
|
<module name="battleShipAi" />
|
||||||
|
</profile>
|
||||||
|
</annotationProcessing>
|
||||||
|
<bytecodeTargetLevel>
|
||||||
|
<module name="battleShipAi" target="1.5" />
|
||||||
|
</bytecodeTargetLevel>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
3
project/readme.md
Normal file
3
project/readme.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# Todo
|
||||||
|
- test class
|
||||||
|
- create full application
|
||||||
@@ -1,12 +1,16 @@
|
|||||||
package com.battleShipAi;
|
package com.battleShipAi;
|
||||||
|
|
||||||
|
|
||||||
|
import jdk.internal.net.http.common.Pair;
|
||||||
|
|
||||||
public class BattleShip {
|
public class BattleShip {
|
||||||
int[][] field;
|
int[][] field;
|
||||||
|
int[][] scoredField;
|
||||||
int width;
|
int width;
|
||||||
int height;
|
int height;
|
||||||
|
int[] ships;
|
||||||
|
|
||||||
|
public BattleShip(int width, int height, int[] ships) {
|
||||||
public BattleShip(int width, int height) {
|
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
field = new int[width][height];
|
field = new int[width][height];
|
||||||
@@ -15,8 +19,57 @@ public class BattleShip {
|
|||||||
cell = 0;
|
cell = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.ships = ships;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pair<Integer, Integer> pickBestMove() {
|
||||||
|
scoreField();
|
||||||
|
return getBestCoors();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Pair<Integer, Integer> getBestCoors() {
|
||||||
|
Pair<Integer, Integer> returnVal = null;
|
||||||
|
int bestScore = 0;
|
||||||
|
for (int xi = 0; xi < width; xi++) {
|
||||||
|
for (int yi = 0; yi < height; yi++) {
|
||||||
|
if (returnVal == null) {
|
||||||
|
returnVal = new Pair<Integer, Integer>(xi, yi);
|
||||||
|
bestScore = scoredField[xi][yi];
|
||||||
|
} else {
|
||||||
|
if (bestScore < scoredField[xi][yi]) {
|
||||||
|
returnVal = new Pair<Integer, Integer>(xi, yi);
|
||||||
|
bestScore = scoredField[xi][yi];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return returnVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void scoreField() {
|
||||||
|
for (int xi = 0; xi < width; xi++) {
|
||||||
|
for (int yi = 0; yi < height; yi++) {
|
||||||
|
if (field[xi][yi] == 0) scoredField[xi][yi] = calculateScore(xi, yi, ships);
|
||||||
|
if (field[xi][yi] == 1) finishHit();
|
||||||
|
if (field[xi][yi] == -1) scoredField[xi][yi] = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void finishHit() {
|
||||||
|
//todo make the values around a hit max priority
|
||||||
|
}
|
||||||
|
|
||||||
|
private int calculateScore(int xi, int yi, int[] ships) {
|
||||||
|
int score = 0;
|
||||||
|
for(int ship : ships) {
|
||||||
|
score += Math.min(ship, xi);
|
||||||
|
score += Math.min(ship, yi);
|
||||||
|
score += Math.min(ship, (width-xi));
|
||||||
|
score += Math.min(ship, (height-yi));
|
||||||
|
}
|
||||||
|
return score;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
88
project/src/main/java/com/battleShipAi/Game.java
Normal file
88
project/src/main/java/com/battleShipAi/Game.java
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
package com.battleShipAi;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Game {
|
||||||
|
//todo vars aanmaken voor bijhouden speler stats.
|
||||||
|
|
||||||
|
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();
|
||||||
|
System.out.println("How high should the playfield be?");
|
||||||
|
int 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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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 {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
for(int i = 0; i < loop; i++) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
BattleShip opponent = new BattleShip()
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void printBoard() {
|
||||||
|
//todo print functie die het bord toont
|
||||||
|
//todo bord aanmaken
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user