From 48a5958414ac703dba32c8b8d02ba7e1a2201c04 Mon Sep 17 00:00:00 2001 From: ole Date: Sat, 30 Nov 2024 13:48:50 +0100 Subject: [PATCH] add Thread safety for Player --- src/Board.java | 6 +++--- src/Player.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Board.java b/src/Board.java index 5cec0cd..99b1723 100644 --- a/src/Board.java +++ b/src/Board.java @@ -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); diff --git a/src/Player.java b/src/Player.java index 192b3f4..5eff33b 100644 --- a/src/Player.java +++ b/src/Player.java @@ -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); }