add Shoot logik and Structure for backend

This commit is contained in:
ole 2024-11-15 17:35:35 +01:00
parent b9a0a21503
commit ce4995fb94
10 changed files with 198 additions and 118 deletions

View File

@ -1,7 +1,7 @@
public abstract class AiPlayer extends Player {
public void setBoardSize(int s) {
// raum für schmerzen
public AiPlayer(int size) {
super(size);
}
}

View File

@ -1,43 +1,72 @@
import java.awt.*;
import java.util.List;
public class Board {
/*
- hits : List<HitResponse>
- ships : List<Ship>
- size : int
- display : BoardDisplay
*/
/*
* + hit(HitResponse)
* + click(Point) : void
*/
List<HitResponse> hits;
List<Ship> ships;
int size;
private List<HitResponse> hits;
private List<Ship> ships;
private final int size;
public Board() {
// MISS,SHIP, HIT
// MISS bedeutet nichts auf der Position vorhanden
// SHIP bedeutet ungetroffenes Schiff auf der Position vorhanden
// HIT bedeutet getroffenes Schiff auf der Position vorhanden
public Board(int size) {
this.size = size;
this.createShip(size - 13);
}
public HitResponse hit (Point point){
HitResponse response = new HitResponse(HitResponseType.MISS,point);
for (int i = 0; i < this.ships.size(); i++) {
HitResponseType type = this.ships.get(i).shootOnShip(point);
if ( type == HitResponseType.SUNK) {
for (int ii = 0; i < this.ships.size(); ii++) {
if (!this.ships.get(ii).isSunk()) {
response.setType(type);
this.addHits(response);
return response;
}
}
response.setType(HitResponseType.VICTORY);
this.addHits(response);
return response;
} else if (type == HitResponseType.HIT) {
response.setType(type);
this.addHits(response);
return response;
}
}
this.addHits(response);
return response;
}
private void createShip(int semester){
List<ShipData> shipData = Ship.semeterList.get(semester -1);
for (int i = 0; i < shipData.size(); i++) {
this.ships.add(new Ship(shipData.get(i).size(), shipData.get(i).name()));
}
}
public List<Ship> getShips() {
return ships;
}
public boolean addHits(HitResponse hitResponse) {
if (this.getHitResponsOnPoint(hitResponse.getPoint()) == null){
this.hits.add(hitResponse);
return true;
}
return false;
}
public HitResponse getHitResponsOnPoint(Point point) {
for (int i = 0; i < this.hits.size(); i++){
if (this.hits.get(i).getPoint().equals(point)){
return this.hits.get(i);
}
}
return null;
}
}
public void setSize(int s) {
this.size = s;
}
public void initBoard() {
for (int i=0;size>i;i++) {
for (int j=0;size>j;j++) {
HitResponse missPos = new HitResponse();
missPos.setHitResponse(HitResponseType.MISS);
missPos.setPoint(i,j);
hits.add(missPos);
}
}
}
}

View File

@ -5,27 +5,12 @@ public class GameController {
// fuck you Luca and Ole von Florian und nicht von Florian
}
public void startLocalGame(HumanPlayer player, AiPlayer enemy, int size) {
//Player initialisieren Board
player.setBoardSize(size);
enemy.setBoardSize(size);
// Schiffe setzen
// für player durch UI
/*
* Todo
* enemy.setships();
*/
boolean condition = true;
while(condition) {
//Gameloop bis Sieger
//WIP
break;
}
public void startLocalGame(Class<? extends LocalPlayer> localPlayerClass, Class<? extends AiPlayer> enemyClass, int size) throws InstantiationException, IllegalAccessException {
LocalPlayer localPlayer = localPlayerClass.newInstance();
AiPlayer aiPlayer = enemyClass.newInstance();
localPlayer.setEnemy(aiPlayer);
aiPlayer.setEnemy(localPlayer);
}
}

View File

@ -1,14 +1,8 @@
public class HalloSchiffeVersenken {
public static void main(String[] args) throws InterruptedException {
int default_size = 14;
HumanPlayer player = new HumanPlayer();
AiPlayer enemy = new SpecificAiPlayer1(); // auswählen hehe. mal gucken und viel spaßßß
GameController game_controller = new GameController();
game_controller.startLocalGame(player, enemy, default_size); // default_size oder auswählen, viel spaß
/*
System.out.println("HelloSchiffeVersenekn");
System.out.println("sound");

View File

@ -4,17 +4,20 @@ public class HitResponse {
private HitResponseType type;
private Point point;
public HitResponse(HitResponseType type, Point point) {
this.type = type;
this.point = point;
}
public HitResponseType getHitResponse() {
return this.type;
}
public void setHitResponse(HitResponseType a) {
this.type = a;
}
public Point getPoint() {
return this.point;
}
public void setPoint(int x, int y) {
this.point = new Point(x,y);
public void setType(HitResponseType type) {
this.type = type;
}
}

View File

@ -1,26 +1,7 @@
public class HumanPlayer extends LocalPlayer {
/*
- myTurn : bool
- isServer : bool
- waitingForResponse : bool
- enemy : Player
- name : String
- board : Board
+ receiveShoot(Point) : void
+ receiveHit(HitResponse) : void
+ click(Point) : void
+ beginTurn() : void
*/
public HumanPlayer() {
Board board = new Board();
this.board = board;
}
public void setBoardSize(int s) {
this.board.setSize(s);
this.board.initBoard();
public HumanPlayer(int size) {
super(size);
}
}

View File

@ -1,4 +1,8 @@
public class LocalPlayer extends Player {
LocalPlayer(int size) {
super(size);
}
}

View File

@ -1,4 +1,5 @@
import java.awt.*;
import java.util.List;
public abstract class Player {
protected boolean myTurn;
@ -8,12 +9,21 @@ public abstract class Player {
protected String name;
protected Board board;
public void receiveShoot(Point point) {
public Player(int size) {
this.board = new Board(size);
}
public void receiveShoot(Point point) {
HitResponse hitResponse = board.getHitResponsOnPoint(point);
if (!(hitResponse == null)){
enemy.receiveHit(hitResponse);
} else {
enemy.receiveHit(this.board.hit(point));
}
}
public void receiveHit(HitResponse hitResponse) {
enemy.board.addHits(hitResponse);
}
public void click(Point point) {
@ -23,4 +33,8 @@ public abstract class Player {
public void beginTrun() {
}
public void setEnemy(Player enemy) {
this.enemy = enemy;
}
}

View File

@ -1,24 +1,100 @@
import java.awt.Point;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
record ShipData (int size, String name){
}
public class Ship {
/*
- size : int
- horizontal : bool
- position : Point
- name : String
- button : ShipButton
static List<List<ShipData>> semeterList = Arrays.asList(Arrays.asList(
new ShipData(2, "PRG 1"),
new ShipData(2, "GDI"),
new ShipData(2, "MAT 1"),
new ShipData(2, "THI"),
new ShipData(4, "STP"),
new ShipData(6, "ENG")),
Arrays.asList(
new ShipData(2, "PRG 2"),
new ShipData(2, "DBS 1"),
new ShipData(2, "MAT 2"),
new ShipData(2, "STA"),
new ShipData(2, "AUD")),
Arrays.asList(
new ShipData(2, "PRG 3"),
new ShipData(2, "DBS 2"),
new ShipData(2, "MAT 3"),
new ShipData(2, "BSN 1"),
new ShipData(4, "PRP"),
new ShipData(6, "BWL")),
Arrays.asList(
new ShipData(2, "WEB"),
new ShipData(2, "SE 1"),
new ShipData(2, "CG 1"),
new ShipData(2, "BSN 2"),
new ShipData(4, "SEM"),
new ShipData(6, "E BWL")),
Arrays.asList(
new ShipData(2, "WPF 1"),
new ShipData(2, "SE 2"),
new ShipData(2, "CG 2"),
new ShipData(2, "PXP 1"),
new ShipData(6, "EF 1")),
Arrays.asList(
new ShipData(2, "WPF 2"),
new ShipData(1, "PXP 2"),
new ShipData(8, "BAA"))
);
private int size;
private boolean horizontal;
private Point position;
private String name;
private int hitsOnMe;
private boolean sunk;
+ setPosition(Point) : void
*/
int size;
boolean horizontal;
Point position;
String name;
public void setPosition(Point pos) {
// raum für gedanken und die Methode
public Ship (int size, String name) {
this.size = size;
this.name = name;
this.horizontal = false;
this.position = null;
this.hitsOnMe = 0;
this.sunk = false;
}
public boolean setPosition(Point pos) {
//TODO Conte abrfrage ob shif da sein darf
this.position = pos;
return true;
}
public Point getPosition() {
return position;
}
public boolean isShipOnPos(Point pos){
if ((this.horizontal && pos.y == this.position.y && pos.x >= this.position.x && pos.x < this.position.x + size) ||
(!(this.horizontal) && pos.x == this.position.x && pos.y >= this.position.y && pos.y < this.position.y + size)) {
return true;
}
return false;
}
public HitResponseType shootOnShip(Point pos) {
if (this.isShipOnPos(pos)) {
hitsOnMe++;
if (hitsOnMe >= size) {
this.sunk = true;
return HitResponseType.SUNK;
} else {
return HitResponseType.HIT;
}
} else {
return HitResponseType.MISS;
}
}
public boolean isSunk() {
return sunk;
}
}

View File

@ -1,13 +1,7 @@
public class SpecificAiPlayer1 extends AiPlayer{
public SpecificAiPlayer1() {
Board board = new Board();
this.board = board;
}
public void setBoardSize(int s) {
this.board.setSize(s);
this.board.initBoard();
public SpecificAiPlayer1(int size) {
super(size);
}
}