add Thread safety for Player

This commit is contained in:
ole 2024-11-30 13:48:50 +01:00
parent d00e12e72f
commit 48a5958414
2 changed files with 5 additions and 5 deletions

View File

@ -13,7 +13,7 @@ public class Board {
this.createShip(size - 13);
}
public HitResponse hit (Point point){
public synchronized 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);
@ -50,7 +50,7 @@ public class Board {
return ships;
}
public boolean addHits(HitResponse hitResponse) {
public synchronized boolean addHits(HitResponse hitResponse) {
if (this.getHitResponsOnPoint(hitResponse.getPoint()) == null){
this.hits.add(hitResponse);
return true;
@ -58,7 +58,7 @@ public class Board {
return false;
}
public HitResponse getHitResponsOnPoint(Point point) {
public synchronized 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);

View File

@ -13,7 +13,7 @@ public abstract class Player {
this.board = new Board(size);
}
public void receiveShoot(Point point) {
public synchronized void receiveShoot(Point point) {
HitResponse hitResponse = board.getHitResponsOnPoint(point);
if (!(hitResponse == null)){
enemy.receiveHit(hitResponse);
@ -22,7 +22,7 @@ public abstract class Player {
}
}
public void receiveHit(HitResponse hitResponse) {
public synchronized void receiveHit(HitResponse hitResponse) {
enemy.board.addHits(hitResponse);
}