ole #7

Merged
lgc merged 23 commits from ole into main 2024-11-26 14:01:01 +00:00
10 changed files with 198 additions and 118 deletions
Showing only changes of commit ce4995fb94 - Show all commits

View File

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

View File

@ -1,43 +1,72 @@
import java.awt.*;
import java.util.List; import java.util.List;
public class Board { public class Board {
/*
- hits : List<HitResponse>
- ships : List<Ship>
- size : int
- display : BoardDisplay
*/
/*
* + hit(HitResponse)
* + click(Point) : void
*/
List<HitResponse> hits; private List<HitResponse> hits;
List<Ship> ships; private List<Ship> ships;
int size; private final int size;
public Board() {
// MISS,SHIP, HIT public Board(int size) {
// MISS bedeutet nichts auf der Position vorhanden this.size = size;
// SHIP bedeutet ungetroffenes Schiff auf der Position vorhanden this.createShip(size - 13);
// HIT bedeutet getroffenes Schiff auf der Position vorhanden }
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 // fuck you Luca and Ole von Florian und nicht von Florian
} }
public void startLocalGame(HumanPlayer player, AiPlayer enemy, int size) { public void startLocalGame(Class<? extends LocalPlayer> localPlayerClass, Class<? extends AiPlayer> enemyClass, int size) throws InstantiationException, IllegalAccessException {
//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;
}
LocalPlayer localPlayer = localPlayerClass.newInstance();
AiPlayer aiPlayer = enemyClass.newInstance();
localPlayer.setEnemy(aiPlayer);
aiPlayer.setEnemy(localPlayer);
} }
} }

View File

@ -1,14 +1,8 @@
public class HalloSchiffeVersenken { public class HalloSchiffeVersenken {
public static void main(String[] args) throws InterruptedException { 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("HelloSchiffeVersenekn");
System.out.println("sound"); System.out.println("sound");

View File

@ -4,17 +4,20 @@ public class HitResponse {
private HitResponseType type; private HitResponseType type;
private Point point; private Point point;
public HitResponse(HitResponseType type, Point point) {
this.type = type;
this.point = point;
}
public HitResponseType getHitResponse() { public HitResponseType getHitResponse() {
return this.type; return this.type;
} }
public void setHitResponse(HitResponseType a) {
this.type = a;
}
public Point getPoint() { public Point getPoint() {
return this.point; 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 { 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(int size) {
super(size);
public HumanPlayer() {
Board board = new Board();
this.board = board;
}
public void setBoardSize(int s) {
this.board.setSize(s);
this.board.initBoard();
} }
} }

View File

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

View File

@ -1,4 +1,5 @@
import java.awt.*; import java.awt.*;
import java.util.List;
public abstract class Player { public abstract class Player {
protected boolean myTurn; protected boolean myTurn;
@ -8,12 +9,21 @@ public abstract class Player {
protected String name; protected String name;
protected Board board; 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) { public void receiveHit(HitResponse hitResponse) {
enemy.board.addHits(hitResponse);
} }
public void click(Point point) { public void click(Point point) {
@ -23,4 +33,8 @@ public abstract class Player {
public void beginTrun() { public void beginTrun() {
} }
public void setEnemy(Player enemy) {
this.enemy = enemy;
}
} }

View File

@ -1,24 +1,100 @@
import java.awt.Point; 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 { public class Ship {
/* static List<List<ShipData>> semeterList = Arrays.asList(Arrays.asList(
- size : int new ShipData(2, "PRG 1"),
- horizontal : bool new ShipData(2, "GDI"),
- position : Point new ShipData(2, "MAT 1"),
- name : String new ShipData(2, "THI"),
- button : ShipButton 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 public Ship (int size, String name) {
this.size = size;
*/ this.name = name;
int size; this.horizontal = false;
boolean horizontal; this.position = null;
Point position; this.hitsOnMe = 0;
String name; this.sunk = false;
public void setPosition(Point pos) {
// raum für gedanken und die Methode
} }
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 class SpecificAiPlayer1 extends AiPlayer{
public SpecificAiPlayer1() { public SpecificAiPlayer1(int size) {
Board board = new Board(); super(size);
this.board = board;
}
public void setBoardSize(int s) {
this.board.setSize(s);
this.board.initBoard();
} }
} }