Merge pull request 'Kommentare und Withdraw' (#24) from FH_Last_changes into main
Reviewed-on: #24
This commit is contained in:
commit
f13e0ab2ba
|
@ -1,3 +1,7 @@
|
|||
/**
|
||||
* Die Board-Klasse repräsentiert das Spielfeld.
|
||||
* @author Peer Ole Wachtel, Florian Alexy und Florian Hantzschel
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -8,7 +12,11 @@ public class Board {
|
|||
private List<Ship> ships;
|
||||
private final int size;
|
||||
|
||||
|
||||
/**
|
||||
* Initialisiert das Board und die zu Setzenden schiffe.
|
||||
* @param size
|
||||
* @author Peer Ole Wachtel, Florian Alexy und Florian Hantzschel
|
||||
*/
|
||||
public Board(int size) {
|
||||
this.size = size;
|
||||
this.ships = new ArrayList<>();
|
||||
|
@ -64,6 +72,7 @@ public class Board {
|
|||
/**
|
||||
* creates all the ships on a board given a certain semester
|
||||
* @param semester the semester to be played in
|
||||
* @author Peer Ole Wachtel
|
||||
*/
|
||||
private void createShip(int semester){
|
||||
List<ShipData> shipData = Ship.semeterList.get(semester);
|
||||
|
|
|
@ -56,12 +56,9 @@ public class GameBoard extends JPanel {
|
|||
List<Ship> shipsP1 =p1.getBoard().getShips();
|
||||
List<Ship> shipsP2 =p2.getBoard().getShips();
|
||||
|
||||
giveUpButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
// TODO Hier könnte Ihr Backend Code stehen
|
||||
frame.showPanel("MainMenu");
|
||||
}
|
||||
giveUpButton.addActionListener((e) -> {
|
||||
frame.showPanel("MainMenu");
|
||||
p1.withdraw();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@ public class LocalPlayer extends Player {
|
|||
switch (hitResponse.getType()) {
|
||||
case HIT, SUNK -> this.myTurn = false;
|
||||
case MISS -> this.myTurn = true;
|
||||
case VICTORY -> GameController.getMainFrame().showPanelLose("LoseScreen", this);
|
||||
case VICTORY -> this.lose();
|
||||
}
|
||||
GameController.getMainFrame().refreshGameBoard();
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ public class LocalPlayer extends Player {
|
|||
switch (hitResponse.getType()) {
|
||||
case HIT, SUNK -> this.myTurn = true;
|
||||
case MISS -> this.myTurn = false;
|
||||
case VICTORY -> GameController.getMainFrame().showPanelWin("", this);
|
||||
case VICTORY -> this.win();
|
||||
}
|
||||
GameController.getMainFrame().refreshGameBoard();
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
|||
* receives a message from the AsyncSocket
|
||||
* implemented according to version 1.1.0 of https://github.com/lgc-4/ProgProjekt-Netzwerkstandard
|
||||
* @param message the message from the socket
|
||||
* @author Peer Ole Wachtel, Luca Conte
|
||||
* @author Peer Ole Wachtel, Luca Conte, Florian Hantzschel
|
||||
*/
|
||||
@Override
|
||||
public void receive(String message) {
|
||||
|
@ -83,6 +83,10 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
|||
//TODO CHAT
|
||||
break;
|
||||
|
||||
case "WITHDRAW":
|
||||
this.withdraw();
|
||||
break;
|
||||
|
||||
default:
|
||||
//nichts passier da Paket ungültig
|
||||
break;
|
||||
|
@ -153,4 +157,10 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
|||
// SHOULD NEVER BE CALLED ON ONLINE PLAYER. ONLY ON HUMAN PLAYER
|
||||
return;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receiveWithdraw() {
|
||||
this.socket.send(new SocketPackage("WITHDRAW"));
|
||||
super.receiveWithdraw();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -137,6 +137,7 @@ public abstract class Player {
|
|||
* and players
|
||||
*
|
||||
* This method should be called at the end of a game
|
||||
* This method should be called at the end of a game
|
||||
*
|
||||
* @author Luca Conte
|
||||
*/
|
||||
|
@ -146,4 +147,20 @@ public abstract class Player {
|
|||
this.board = null;
|
||||
this.enemy = null;
|
||||
}
|
||||
|
||||
public void win() {
|
||||
GameController.getMainFrame().showPanelWin("", this);
|
||||
}
|
||||
public void lose() {
|
||||
GameController.getMainFrame().showPanelLose("", this);
|
||||
}
|
||||
|
||||
public void withdraw() {
|
||||
this.enemy.receiveWithdraw();
|
||||
this.lose();
|
||||
}
|
||||
|
||||
public void receiveWithdraw(){
|
||||
this.win();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
/**
|
||||
* Diese Klasse implementiert den Einfachsten Ki Spieler.
|
||||
* @author Florian und Florian
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
* */
|
||||
public class SpecificAiPlayerEasy extends AiPlayer{
|
||||
|
||||
/**
|
||||
* Bein einfachen Ki Spieler wird nur der AiPlayer initialisiert und der Name gesetzt,
|
||||
* da in der Eltern-Klasse AiPlayer eine default implementierung für alle Methoden existieren.
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
*/
|
||||
public SpecificAiPlayerEasy() {
|
||||
super();
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
/**
|
||||
* Diese Klasse implementiert den Harten Ki Spieler.
|
||||
* @author Florian und Florian
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
* */
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
// import java.util.Random; wird nicht mehr verwendet
|
||||
|
||||
public class SpecificAiPlayerHard extends AiPlayer{
|
||||
|
||||
private int gridSize;
|
||||
private boolean[][] shotsFired;
|
||||
private final ArrayList<Point> hitQueue;
|
||||
private final Random random;
|
||||
//private final Random random; wird nicht mehr verwendet
|
||||
private int nextChessRow;
|
||||
private int nextChessCol;
|
||||
|
||||
|
@ -24,7 +24,11 @@ public class SpecificAiPlayerHard extends AiPlayer{
|
|||
private Point firstHit; // Speichert den ersten Treffer zur Bestimmung der Orientierung
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Eltern-Klasse wird initialisiert und alle lokalen variablen,
|
||||
* die gesetzt werden können, werden initialisiert.
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
*/
|
||||
public SpecificAiPlayerHard() {
|
||||
super();
|
||||
this.setName("AI Player Hard");
|
||||
|
@ -32,19 +36,28 @@ public class SpecificAiPlayerHard extends AiPlayer{
|
|||
this.shotsFired = new boolean[gridSize][gridSize];*/
|
||||
this.gridSize = 0;
|
||||
this.hitQueue = new ArrayList<>();
|
||||
this.random = new Random();
|
||||
//this.random = new Random(); wird nicht mehr verwendet
|
||||
this.nextChessRow = 0;
|
||||
this.nextChessCol = 0;
|
||||
this.orientierung = Orientierung.UNBEKANNT;
|
||||
this.firstHit = null;
|
||||
}
|
||||
|
||||
// Checks if a position has already been shot at
|
||||
/**
|
||||
* Prüft, ob auf den punkt schonmal geschossen wurde.
|
||||
* @param p zu prüfender Punkt
|
||||
* @return boolean
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
*/
|
||||
public boolean alreadyShot(Point p) {
|
||||
return shotsFired[p.getX()][p.getY()];
|
||||
}
|
||||
|
||||
// Generates the next move for the AI
|
||||
/**
|
||||
* Bestimmt den nächsten punkt, der beschossen werden soll.
|
||||
* @return Position
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
*/
|
||||
public Point getNextMove() {
|
||||
if(gridSize == 0) {
|
||||
this.gridSize = super.board.getSize();
|
||||
|
@ -73,6 +86,12 @@ public class SpecificAiPlayerHard extends AiPlayer{
|
|||
return new Point(row, col);
|
||||
}
|
||||
|
||||
/**
|
||||
* Nachdem receiveShoot beim gegner den schuss verarbeitet hat,
|
||||
* wird diese Methode mit der antwort aufgerufen.
|
||||
* @param hitResponse the hitresponse
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
*/
|
||||
@Override
|
||||
public synchronized void receiveHit(HitResponse hitResponse) {
|
||||
super.receiveHit(hitResponse);
|
||||
|
@ -113,6 +132,7 @@ public class SpecificAiPlayerHard extends AiPlayer{
|
|||
|
||||
/**
|
||||
* Entfernt aus der hitQueue alle Punkte, die nicht der erkannten Orientierung entsprechen.
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
*/
|
||||
private void cleanUpHitQueue() {
|
||||
if (firstHit == null || this.orientierung == Orientierung.UNBEKANNT) {
|
||||
|
@ -136,9 +156,10 @@ public class SpecificAiPlayerHard extends AiPlayer{
|
|||
}
|
||||
|
||||
/**
|
||||
* Fügt benachbarte Felder in der **erkannten Orientierung** hinzu.
|
||||
* - Ist die Orientierung HORIZONTAL, so werden nur links/rechts hinzugefügt.
|
||||
* - Ist sie VERTICAL, so werden nur oben/unten hinzugefügt.
|
||||
* Fügt benachbarte Felder in der erkannten Orientierung hinzu.
|
||||
* Ist die Orientierung HORIZONTAL, so werden nur links/rechts hinzugefügt.
|
||||
* Ist sie VERTICAL, so werden nur oben/unten hinzugefügt.
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
*/
|
||||
private void addPointsByOrientation(Point point) {
|
||||
if (this.orientierung == Orientierung.UNBEKANNT) {
|
||||
|
@ -178,6 +199,7 @@ public class SpecificAiPlayerHard extends AiPlayer{
|
|||
/**
|
||||
* Diese Methode erweitert die hitsQueue um die umliegenden Punkte die Schiffe seien könnten.
|
||||
* @param point
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
*/
|
||||
private void addAdjacentPoints(Point point) {
|
||||
int x = point.getX();
|
||||
|
@ -201,6 +223,7 @@ public class SpecificAiPlayerHard extends AiPlayer{
|
|||
* Die Methode gibt zurück, ob eine Position auf dem Board ist. (Boolean)
|
||||
* @param point Punkt der geprüft werden soll
|
||||
* @return Ist auf dem Board oder nicht.
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
*/
|
||||
private boolean isValidPoint(Point point) {
|
||||
return point.getX() >= 0 && point.getX() < gridSize &&
|
||||
|
@ -208,13 +231,17 @@ public class SpecificAiPlayerHard extends AiPlayer{
|
|||
}
|
||||
/**
|
||||
* Ki Methode um zu schießen.
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
*/
|
||||
@Override
|
||||
public void aiShoot() {
|
||||
this.enemy.receiveShoot(getNextMove());
|
||||
}
|
||||
|
||||
// Advances the chess pattern to the next cell
|
||||
/**
|
||||
* Die Zeilen und spalten variable wird hier angepasst, sodass beim nächsten schuss im Muster geschossen wird.
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
*/
|
||||
private void advanceChessPattern() {
|
||||
nextChessCol += 2;
|
||||
if (nextChessCol >= gridSize) {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
* Diese Klasse implementiert den Medium Ki Spieler.
|
||||
* @author Florian und Florian
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
* */
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -11,6 +11,10 @@ public class SpecificAiPlayerMedium extends AiPlayer{
|
|||
*/
|
||||
private List<Point> hitsQueue = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Eltern-Klasse wird initialisiert und der Name wird gesetzt.
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
*/
|
||||
public SpecificAiPlayerMedium() {
|
||||
super();
|
||||
this.setName("AI Player Medium");
|
||||
|
@ -18,6 +22,7 @@ public class SpecificAiPlayerMedium extends AiPlayer{
|
|||
|
||||
/**
|
||||
* Ki Methode um zu schießen.
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
*/
|
||||
@Override
|
||||
public void aiShoot() {
|
||||
|
@ -25,7 +30,13 @@ public class SpecificAiPlayerMedium extends AiPlayer{
|
|||
// Shoot at the enemy and receive the hit response
|
||||
enemy.receiveShoot(nextShot);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Nachdem receiveShoot beim gegner den schuss verarbeitet hat,
|
||||
* wird diese Methode mit der antwort aufgerufen.
|
||||
* @param hitResponse the hitresponse
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
*/
|
||||
@Override
|
||||
public synchronized void receiveHit(HitResponse hitResponse) {
|
||||
super.receiveHit(hitResponse);
|
||||
|
@ -38,6 +49,7 @@ public class SpecificAiPlayerMedium extends AiPlayer{
|
|||
/**
|
||||
* Die Methode bestimmt welche Position als nächstes beschossen werden soll.
|
||||
* @return
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
*/
|
||||
public Point ComputeNextShot() {
|
||||
Point nextShot;
|
||||
|
@ -61,6 +73,7 @@ public class SpecificAiPlayerMedium extends AiPlayer{
|
|||
/**
|
||||
* Diese Methode erweitert die hitsQueue um die umliegenden Punkte die Schiffe seien könnten.
|
||||
* @param point
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
*/
|
||||
private void addAdjacentPoints(Point point) {
|
||||
int x = point.getX();
|
||||
|
@ -85,6 +98,7 @@ public class SpecificAiPlayerMedium extends AiPlayer{
|
|||
* Diese Methode gibt zurück, ob eine Position schon beschossen wurde. (Boolean)
|
||||
* @param p Punkt der geprüft werden soll
|
||||
* @return wurde schon beschossen oder nicht.
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
*/
|
||||
private boolean alreadyShot(Point p) {
|
||||
|
||||
|
@ -96,6 +110,7 @@ public class SpecificAiPlayerMedium extends AiPlayer{
|
|||
* Die Methode gibt zurück, ob eine Position auf dem Board ist. (Boolean)
|
||||
* @param point Punkt der geprüft werden soll
|
||||
* @return Ist auf dem Board oder nicht.
|
||||
* @author Florian Alexy und Florian Hantzschel
|
||||
*/
|
||||
private boolean isValidPoint(Point point) {
|
||||
return point.getX() >= 0 && point.getX() < board.getSize() &&
|
||||
|
|
Loading…
Reference in New Issue