add Thread safety for Player
This commit is contained in:
parent
d00e12e72f
commit
48a5958414
|
@ -13,7 +13,7 @@ public class Board {
|
||||||
this.createShip(size - 13);
|
this.createShip(size - 13);
|
||||||
}
|
}
|
||||||
|
|
||||||
public 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++) {
|
||||||
HitResponseType type = this.ships.get(i).shootOnShip(point);
|
HitResponseType type = this.ships.get(i).shootOnShip(point);
|
||||||
|
@ -50,7 +50,7 @@ public class Board {
|
||||||
return ships;
|
return ships;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean addHits(HitResponse hitResponse) {
|
public synchronized boolean addHits(HitResponse hitResponse) {
|
||||||
if (this.getHitResponsOnPoint(hitResponse.getPoint()) == null){
|
if (this.getHitResponsOnPoint(hitResponse.getPoint()) == null){
|
||||||
this.hits.add(hitResponse);
|
this.hits.add(hitResponse);
|
||||||
return true;
|
return true;
|
||||||
|
@ -58,7 +58,7 @@ public class Board {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HitResponse getHitResponsOnPoint(Point point) {
|
public synchronized HitResponse getHitResponsOnPoint(Point point) {
|
||||||
for (int i = 0; i < this.hits.size(); i++){
|
for (int i = 0; i < this.hits.size(); i++){
|
||||||
if (this.hits.get(i).getPoint().equals(point)){
|
if (this.hits.get(i).getPoint().equals(point)){
|
||||||
return this.hits.get(i);
|
return this.hits.get(i);
|
||||||
|
|
|
@ -13,7 +13,7 @@ public abstract class Player {
|
||||||
this.board = new Board(size);
|
this.board = new Board(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void receiveShoot(Point point) {
|
public synchronized void receiveShoot(Point point) {
|
||||||
HitResponse hitResponse = board.getHitResponsOnPoint(point);
|
HitResponse hitResponse = board.getHitResponsOnPoint(point);
|
||||||
if (!(hitResponse == null)){
|
if (!(hitResponse == null)){
|
||||||
enemy.receiveHit(hitResponse);
|
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);
|
enemy.board.addHits(hitResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue