proper thread killing and online player myTurn handling
This commit is contained in:
parent
be6b50739f
commit
44f04a454f
|
@ -225,7 +225,10 @@ public class AsyncSocket {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.socket.close();
|
this.socket.close();
|
||||||
if (this.checkerThread != null) this.checkerThread.join();
|
if (this.checkerThread != null) {
|
||||||
|
this.checkerThread.interrupt();
|
||||||
|
this.checkerThread.join();
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|
|
@ -9,7 +9,6 @@ public class HumanPlayer extends LocalPlayer {
|
||||||
@Override
|
@Override
|
||||||
public void shoot(Point point) {
|
public void shoot(Point point) {
|
||||||
if (!this.myTurn) return;
|
if (!this.myTurn) return;
|
||||||
this.myTurn = false;
|
|
||||||
enemy.receiveShoot(point);
|
enemy.receiveShoot(point);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,11 @@ public class LocalPlayer extends Player {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public synchronized void receiveShoot(Point point) {
|
public synchronized void receiveShoot(Point point) {
|
||||||
if (!this.enemy.myTurn) return;
|
if (!this.enemy.myTurn) {
|
||||||
|
System.out.println("enemy tried to fire when not their turn!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.enemy.myTurn = false;
|
||||||
|
|
||||||
HitResponse hitResponse = board.getHitResponseOnPoint(point);
|
HitResponse hitResponse = board.getHitResponseOnPoint(point);
|
||||||
if (!(hitResponse == null)){
|
if (!(hitResponse == null)){
|
||||||
|
@ -29,7 +33,7 @@ public class LocalPlayer extends Player {
|
||||||
switch (hitResponse.getType()) {
|
switch (hitResponse.getType()) {
|
||||||
case HIT, SUNK -> this.myTurn = false;
|
case HIT, SUNK -> this.myTurn = false;
|
||||||
case MISS -> this.myTurn = true;
|
case MISS -> this.myTurn = true;
|
||||||
case VICTORY -> GameController.getMainFrame().showPanelLose("", this); //TODO Was halt bei victory passiert ist hier wurder verloheren
|
case VICTORY -> GameController.getMainFrame().showPanelLose("LoseScreen", this);
|
||||||
}
|
}
|
||||||
GameController.getMainFrame().refreshGameBoard();
|
GameController.getMainFrame().refreshGameBoard();
|
||||||
}
|
}
|
||||||
|
@ -45,7 +49,7 @@ public class LocalPlayer extends Player {
|
||||||
switch (hitResponse.getType()) {
|
switch (hitResponse.getType()) {
|
||||||
case HIT, SUNK -> this.myTurn = true;
|
case HIT, SUNK -> this.myTurn = true;
|
||||||
case MISS -> this.myTurn = false;
|
case MISS -> this.myTurn = false;
|
||||||
case VICTORY -> GameController.getMainFrame().showPanelWin("", this); // TODO was halt beim victory passier ist hier wurde gewonnen
|
case VICTORY -> GameController.getMainFrame().showPanelWin("", this);
|
||||||
}
|
}
|
||||||
GameController.getMainFrame().refreshGameBoard();
|
GameController.getMainFrame().refreshGameBoard();
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,12 +22,6 @@ public abstract class OnlinePlayer extends Player implements AsyncSocketListener
|
||||||
|
|
||||||
public abstract void receive(String message);
|
public abstract void receive(String message);
|
||||||
|
|
||||||
@Override
|
|
||||||
public abstract void receiveShoot(Point point);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public abstract void receiveHit(HitResponse hitResponse);
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public abstract void receiveCoin(boolean coin);
|
public abstract void receiveCoin(boolean coin);
|
||||||
|
|
||||||
|
|
|
@ -58,7 +58,23 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
||||||
Point point = new Point(data.get(0));
|
Point point = new Point(data.get(0));
|
||||||
int typeIndex = Integer.parseInt(data.get(1));
|
int typeIndex = Integer.parseInt(data.get(1));
|
||||||
if (Point.isValidSyntax(data.get(0)) && typeIndex >= 0 && typeIndex < HitResponseType.values().length){
|
if (Point.isValidSyntax(data.get(0)) && typeIndex >= 0 && typeIndex < HitResponseType.values().length){
|
||||||
this.enemy.receiveHit(new HitResponse(typeIndex, point));
|
|
||||||
|
HitResponse hitResponse = new HitResponse(typeIndex, point);
|
||||||
|
|
||||||
|
this.enemy.receiveHit(hitResponse);
|
||||||
|
|
||||||
|
switch (hitResponse.getType()) {
|
||||||
|
case HIT, SUNK:
|
||||||
|
this.myTurn = false;
|
||||||
|
break;
|
||||||
|
case MISS:
|
||||||
|
this.myTurn = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case VICTORY:
|
||||||
|
// GameController.getMainFrame().showPanelWin("", this.enemy);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -102,6 +118,18 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public synchronized void receiveHit(HitResponse hitResponse) {
|
public synchronized void receiveHit(HitResponse hitResponse) {
|
||||||
|
switch (hitResponse.getType()) {
|
||||||
|
case HIT, SUNK:
|
||||||
|
this.myTurn = true;
|
||||||
|
break;
|
||||||
|
case MISS:
|
||||||
|
this.myTurn = false;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case VICTORY:
|
||||||
|
// GameController.getMainFrame().showPanelLose("", this.enemy);
|
||||||
|
break;
|
||||||
|
}
|
||||||
super.socket.send(new SocketPackage("HIT", hitResponse.toString()));
|
super.socket.send(new SocketPackage("HIT", hitResponse.toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue