From ce4995fb94e3fe8ed3c5e5e75b4991f1056aa9ce Mon Sep 17 00:00:00 2001 From: ole Date: Fri, 15 Nov 2024 17:35:35 +0100 Subject: [PATCH] add Shoot logik and Structure for backend --- src/AiPlayer.java | 4 +- src/Board.java | 99 ++++++++++++++++++----------- src/GameController.java | 27 ++------ src/HalloSchiffeVersenken.java | 6 -- src/HitResponse.java | 15 +++-- src/HumanPlayer.java | 23 +------ src/LocalPlayer.java | 4 ++ src/Player.java | 18 +++++- src/Ship.java | 110 ++++++++++++++++++++++++++++----- src/SpecificAiPlayer1.java | 10 +-- 10 files changed, 198 insertions(+), 118 deletions(-) diff --git a/src/AiPlayer.java b/src/AiPlayer.java index 6343dcd..06db3f0 100644 --- a/src/AiPlayer.java +++ b/src/AiPlayer.java @@ -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); } } diff --git a/src/Board.java b/src/Board.java index 73564c9..c2c1112 100644 --- a/src/Board.java +++ b/src/Board.java @@ -1,43 +1,72 @@ +import java.awt.*; import java.util.List; public class Board { -/* - - hits : List - - ships : List - - size : int - - display : BoardDisplay -*/ - /* - * + hit(HitResponse) - * + click(Point) : void - */ - - List hits; - List ships; - 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 void setSize(int s) { - this.size = s; - } - - public void initBoard() { + private List hits; + private List ships; + private final int size; - 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); + + 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; } -} \ No newline at end of file + + private void createShip(int semester){ + List 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 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; + } + + +} + diff --git a/src/GameController.java b/src/GameController.java index a452755..797def8 100644 --- a/src/GameController.java +++ b/src/GameController.java @@ -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 localPlayerClass, Class enemyClass, int size) throws InstantiationException, IllegalAccessException { - } + LocalPlayer localPlayer = localPlayerClass.newInstance(); + AiPlayer aiPlayer = enemyClass.newInstance(); + localPlayer.setEnemy(aiPlayer); + aiPlayer.setEnemy(localPlayer); + } } \ No newline at end of file diff --git a/src/HalloSchiffeVersenken.java b/src/HalloSchiffeVersenken.java index 8450b7d..8072168 100644 --- a/src/HalloSchiffeVersenken.java +++ b/src/HalloSchiffeVersenken.java @@ -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"); diff --git a/src/HitResponse.java b/src/HitResponse.java index b913c3c..bc9fca0 100644 --- a/src/HitResponse.java +++ b/src/HitResponse.java @@ -3,18 +3,21 @@ import java.awt.*; 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; } } diff --git a/src/HumanPlayer.java b/src/HumanPlayer.java index eb2e466..6723e71 100644 --- a/src/HumanPlayer.java +++ b/src/HumanPlayer.java @@ -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); } } diff --git a/src/LocalPlayer.java b/src/LocalPlayer.java index 686b874..cfa7cab 100644 --- a/src/LocalPlayer.java +++ b/src/LocalPlayer.java @@ -1,4 +1,8 @@ public class LocalPlayer extends Player { + + LocalPlayer(int size) { + super(size); + } } \ No newline at end of file diff --git a/src/Player.java b/src/Player.java index 3dbf7c8..192b3f4 100644 --- a/src/Player.java +++ b/src/Player.java @@ -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; + } } diff --git a/src/Ship.java b/src/Ship.java index 2a5155a..08db6b6 100644 --- a/src/Ship.java +++ b/src/Ship.java @@ -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 - - - + setPosition(Point) : void + static List> 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")) + ); - */ - int size; - boolean horizontal; - Point position; - String name; - - public void setPosition(Point pos) { - // raum für gedanken und die Methode + private int size; + private boolean horizontal; + private Point position; + private String name; + private int hitsOnMe; + private boolean sunk; + + 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; + } } diff --git a/src/SpecificAiPlayer1.java b/src/SpecificAiPlayer1.java index 93d8aae..0bcce8c 100644 --- a/src/SpecificAiPlayer1.java +++ b/src/SpecificAiPlayer1.java @@ -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); } }