Network and Frontend #10
|
@ -34,4 +34,8 @@ public class HitResponse {
|
|||
public String toString() {
|
||||
return this.getPoint().toString() + " " + this.type.ordinal();
|
||||
}
|
||||
|
||||
public HitResponseType getType() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +1,58 @@
|
|||
import java.util.Random;
|
||||
|
||||
public class LocalPlayer extends Player {
|
||||
|
||||
public LocalPlayer(){
|
||||
super();
|
||||
Random random = new Random();
|
||||
this.myCoin = random.nextBoolean();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void receiveShoot(Point point) {
|
||||
HitResponse hitResponse = board.getHitResponsOnPoint(point);
|
||||
if (!(hitResponse == null)){
|
||||
enemy.receiveHit(hitResponse);
|
||||
|
||||
} else {
|
||||
enemy.receiveHit(this.board.hit(point));
|
||||
hitResponse = this.board.hit(point);
|
||||
enemy.receiveHit(hitResponse);
|
||||
}
|
||||
|
||||
switch (hitResponse.getType()) {
|
||||
case HIT, SUNK -> this.myTurn = false;
|
||||
case MISS -> this.myTurn = true;
|
||||
case VICTORY -> System.out.println("Game Over"); //TODO Was halt bei victory passiert ist hier wurder verloheren
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void receiveHit(HitResponse hitResponse) {
|
||||
enemy.board.addHits(hitResponse);
|
||||
switch (hitResponse.getType()) {
|
||||
case HIT, SUNK -> this.myTurn = true;
|
||||
case MISS -> this.myTurn = false;
|
||||
case VICTORY -> System.out.println("Win"); // TODO was halt beim victory passier ist hier wurde gewonnen
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void receiveCoin(boolean coin) {
|
||||
if (!this.haseReceivedCoin) {
|
||||
boolean result = coin ^ this.myCoin; // XOR
|
||||
this.myTurn = result == this.isServer;
|
||||
this.haseReceivedCoin = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void sendCoin() {
|
||||
enemy.receiveCoin(this.myCoin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shoot(Point point){
|
||||
this.myTurn = false;
|
||||
enemy.receiveShoot(point);
|
||||
}
|
||||
|
||||
}
|
|
@ -2,6 +2,8 @@ public abstract class OnlinePlayer extends Player implements AsyncSocketListener
|
|||
protected AsyncSocket socket;
|
||||
protected int wantedBoardSize;
|
||||
|
||||
protected boolean hasReceivedCoinPackage;
|
||||
|
||||
public OnlinePlayer(int size, AsyncSocket socket) {
|
||||
this.socket = socket;
|
||||
this.wantedBoardSize = size;
|
||||
|
@ -17,4 +19,7 @@ public abstract class OnlinePlayer extends Player implements AsyncSocketListener
|
|||
@Override
|
||||
public abstract void receiveHit(HitResponse hitResponse);
|
||||
|
||||
@Override
|
||||
public abstract void receiveCoin(boolean coin);
|
||||
|
||||
}
|
||||
|
|
|
@ -30,7 +30,13 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
|||
|
||||
// TODO: IAMU
|
||||
|
||||
// TODO: COIN
|
||||
case "COIN":
|
||||
if(!this.hasReceivedCoinPackage && (p.getData().equals("1") || p.getData().equals("0"))){
|
||||
this.myCoin = p.getData().equals("1");
|
||||
enemy.receiveCoin(this.myCoin);
|
||||
this.hasReceivedCoinPackage = true;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case "SHOOT":
|
||||
|
@ -69,4 +75,12 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
|||
public synchronized void receiveHit(HitResponse hitResponse) {
|
||||
super.socket.send(new SocketPackage("HIT", hitResponse.toString()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void receiveCoin(boolean coin) {
|
||||
if (!this.haseReceivedCoin) {
|
||||
super.socket.send(new SocketPackage("COIN", String.valueOf(coin ? 1 : 0)));
|
||||
this.haseReceivedCoin = true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
import java.util.Random;
|
||||
|
||||
public abstract class Player {
|
||||
protected boolean myTurn;
|
||||
|
@ -6,8 +7,16 @@ public abstract class Player {
|
|||
protected Player enemy;
|
||||
protected String name;
|
||||
protected Board board;
|
||||
protected boolean myCoin;
|
||||
|
||||
public Player() {}
|
||||
protected boolean sendCoin;
|
||||
|
||||
protected boolean haseReceivedCoin;
|
||||
|
||||
public Player() {
|
||||
this.haseReceivedCoin = false;
|
||||
this.sendCoin = false;
|
||||
}
|
||||
|
||||
public void createBoard(int size) {
|
||||
this.board = new Board(size);
|
||||
|
@ -17,9 +26,7 @@ public abstract class Player {
|
|||
|
||||
public abstract void receiveHit(HitResponse hitResponse);
|
||||
|
||||
public void click(Point point) {
|
||||
|
||||
}
|
||||
public abstract void shoot(Point point);
|
||||
|
||||
public void beginTrun() {
|
||||
|
||||
|
@ -35,4 +42,7 @@ public abstract class Player {
|
|||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
|
||||
public abstract void receiveCoin(boolean coin);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue