Add komentare #27

Merged
lgc merged 3 commits from ole into main 2024-12-23 19:49:01 +00:00
6 changed files with 79 additions and 2 deletions
Showing only changes of commit 70749c2c62 - Show all commits

View File

@ -1,14 +1,38 @@
/**
* Diese Klasse ist das Board von eimem spieler und enthält alle logischen operationen.
* Sprich ist das Backend Board.
*
* @author Peer Ole Wachtel
*/
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Board { public class Board {
/**
* Alle bisher empfangenen HitResponsen
*/
private List<HitResponse> hits; private List<HitResponse> hits;
/**
* Alle Schiffe des Semesters
*/
private List<Ship> ships; private List<Ship> ships;
/**
* Die größe des Spielfeldes
*/
private final int size; private final int size;
/**
* Erstellt ein neues Board.
* setzt die übergebene Spielfeldgröße.
* Erstellt die Liste aller Schiffe des Semesters
* @param size Die größe des Spielfeldes
* @author Peer Ole Wachtel
*/
public Board(int size) { public Board(int size) {
this.size = size; this.size = size;
this.ships = new ArrayList<>(); this.ships = new ArrayList<>();
@ -16,6 +40,12 @@ public class Board {
this.createShip(size - 13); this.createShip(size - 13);
} }
/**
* Nimmt einen punkt entgegen und Gibt einen Hitrespons zurück.
* @param point auf den geschossen wurde
* @return
* @author Peer Ole Wachtel
*/
public synchronized HitResponse hit (Point point){ public synchronized HitResponse hit (Point point){
HitResponse response = new HitResponse(HitResponseType.MISS,point); HitResponse response = new HitResponse(HitResponseType.MISS,point);
for (int i = 0; i < this.ships.size(); i++) { for (int i = 0; i < this.ships.size(); i++) {
@ -64,6 +94,7 @@ public class Board {
/** /**
* creates all the ships on a board given a certain semester * creates all the ships on a board given a certain semester
* @param semester the semester to be played in * @param semester the semester to be played in
* @author Peer Ole Wachtel
*/ */
private void createShip(int semester){ private void createShip(int semester){
List<ShipData> shipData = Ship.semeterList.get(semester -1); List<ShipData> shipData = Ship.semeterList.get(semester -1);
@ -86,7 +117,7 @@ public class Board {
* to all adjacened hit responses with type HIT using `propagateSunk`. * to all adjacened hit responses with type HIT using `propagateSunk`.
* @param hitResponse the HitResponse to be added * @param hitResponse the HitResponse to be added
* @return true when the hit response was added, otherwise false * @return true when the hit response was added, otherwise false
* *
*/ */
public synchronized boolean addHits(HitResponse hitResponse) { public synchronized boolean addHits(HitResponse hitResponse) {
if (this.getHitResponseOnPoint(hitResponse.getPoint()) == null){ if (this.getHitResponseOnPoint(hitResponse.getPoint()) == null){
@ -106,6 +137,7 @@ public class Board {
/** /**
* @param point the position to get the hit response from * @param point the position to get the hit response from
* @return the hit response at the position `point` * @return the hit response at the position `point`
* @author Peer Ole Wachtel
*/ */
public synchronized HitResponse getHitResponseOnPoint(Point point) { public synchronized HitResponse getHitResponseOnPoint(Point point) {
for (int i = 0; i < this.hits.size(); i++){ for (int i = 0; i < this.hits.size(); i++){

View File

@ -1,14 +1,33 @@
public class HitResponse { public class HitResponse {
/**
* Speichert den typ der HitResponse.
*/
private HitResponseType type; private HitResponseType type;
/**
* Speicher den Punkt wofür die HitResponse gilt.
*/
private Point point; private Point point;
/**
* Erstellt eine neue HitResponse und setzt den Punkt und typ.
* @param type der HitResponse.
* @param point für den die HitResponse gilt.
* @author Peer Ole Wachtel.
*/
public HitResponse(HitResponseType type, Point point) { public HitResponse(HitResponseType type, Point point) {
this.type = type; this.type = type;
this.point = point; this.point = point;
} }
/**
*Erstellt eine neue HitResponse und setzt den Punkt und typ.
* @param typeIndex der HitResponse.
* @param point für den die HitResponse gilt.
* @throws IllegalArgumentException wenn der übergebene int nicht auf ein typ referenziert werden kann.
* @author Peer Ole Wachtel.
*/
public HitResponse (int typeIndex, Point point) { public HitResponse (int typeIndex, Point point) {
if (typeIndex >= 0 && typeIndex < HitResponseType.values().length) { if (typeIndex >= 0 && typeIndex < HitResponseType.values().length) {
this.type = HitResponseType.values()[typeIndex]; this.type = HitResponseType.values()[typeIndex];
@ -26,15 +45,30 @@ public class HitResponse {
return this.point; return this.point;
} }
/**
* Setter für den type
* @param type auf den die HitResponse gesetzt werden soll.
* @author Peer Ole Wachtel
*/
public void setType(HitResponseType type) { public void setType(HitResponseType type) {
this.type = type; this.type = type;
} }
/**
* Gibt den passenden string nach Netzwerkstandard für eine HitResponse zurück.
* @return den passenden string nach Netzwerkstandard für eine HitResponse.
* @author Peer Ole Wachtel.
*/
@Override @Override
public String toString() { public String toString() {
return this.getPoint().toString() + " " + this.type.ordinal(); return this.getPoint().toString() + " " + this.type.ordinal();
} }
/**
* Getter für den typ der HitResponse.
* @return den typ der HitRespnse.
* @author Peer Ole Wachtel.
*/
public HitResponseType getType() { public HitResponseType getType() {
return type; return type;
} }

View File

@ -1,3 +1,7 @@
/**
* Stellt die verschiedenen möglichkeiten einer HitResponse als typ dar.
* @author Peer Ole Wachtel
*/
public enum HitResponseType { public enum HitResponseType {
MISS, HIT, SUNK, VICTORY MISS, HIT, SUNK, VICTORY
} }

View File

@ -2,6 +2,11 @@ import java.util.Random;
public class LocalPlayer extends Player { public class LocalPlayer extends Player {
/**
* erstellt einen LocalPlayer und setzt myCoin random
* @author Peer Ole Wachtel
*/
public LocalPlayer(){ public LocalPlayer(){
super(); super();
Random random = new Random(); Random random = new Random();

View File

@ -31,7 +31,8 @@ public abstract class Player {
public abstract void receiveShoot(Point point); public abstract void receiveShoot(Point point);
public abstract void receiveHit(HitResponse hitResponse); public abstract
void receiveHit(HitResponse hitResponse);
public abstract void shoot(Point point); public abstract void shoot(Point point);

View File

@ -6,6 +6,7 @@ public class Point {
* initialises a point using X and Y coordinates starting at 0 * initialises a point using X and Y coordinates starting at 0
* @param x the x coordinate of the point starting at 0 * @param x the x coordinate of the point starting at 0
* @param y the y coordinate of the point starting at 0 * @param y the y coordinate of the point starting at 0
* @author Peer Ole Wachtel
*/ */
public Point (int x, int y) { public Point (int x, int y) {
this.setX(x); this.setX(x);