add coin flip and turn logic

This commit is contained in:
Ole Wachtel 2024-12-03 12:11:06 +01:00
parent addf542300
commit 43545afb45
5 changed files with 79 additions and 6 deletions

View File

@ -34,4 +34,8 @@ public class HitResponse {
public String toString() { public String toString() {
return this.getPoint().toString() + " " + this.type.ordinal(); return this.getPoint().toString() + " " + this.type.ordinal();
} }
public HitResponseType getType() {
return type;
}
} }

View File

@ -1,18 +1,58 @@
import java.util.Random;
public class LocalPlayer extends Player { public class LocalPlayer extends Player {
public LocalPlayer(){
super();
Random random = new Random();
this.myCoin = random.nextBoolean();
}
@Override @Override
public synchronized 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);
} else { } 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 @Override
public synchronized void receiveHit(HitResponse hitResponse) { public synchronized void receiveHit(HitResponse hitResponse) {
enemy.board.addHits(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);
} }
} }

View File

@ -2,6 +2,8 @@ public abstract class OnlinePlayer extends Player implements AsyncSocketListener
protected AsyncSocket socket; protected AsyncSocket socket;
protected int wantedBoardSize; protected int wantedBoardSize;
protected boolean hasReceivedCoinPackage;
public OnlinePlayer(int size, AsyncSocket socket) { public OnlinePlayer(int size, AsyncSocket socket) {
this.socket = socket; this.socket = socket;
this.wantedBoardSize = size; this.wantedBoardSize = size;
@ -17,4 +19,7 @@ public abstract class OnlinePlayer extends Player implements AsyncSocketListener
@Override @Override
public abstract void receiveHit(HitResponse hitResponse); public abstract void receiveHit(HitResponse hitResponse);
@Override
public abstract void receiveCoin(boolean coin);
} }

View File

@ -30,7 +30,13 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
// TODO: IAMU // 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": case "SHOOT":
@ -69,4 +75,12 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
public synchronized void receiveHit(HitResponse hitResponse) { public synchronized void receiveHit(HitResponse hitResponse) {
super.socket.send(new SocketPackage("HIT", hitResponse.toString())); 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;
}
}
} }

View File

@ -1,3 +1,4 @@
import java.util.Random;
public abstract class Player { public abstract class Player {
protected boolean myTurn; protected boolean myTurn;
@ -6,8 +7,16 @@ public abstract class Player {
protected Player enemy; protected Player enemy;
protected String name; protected String name;
protected Board board; 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) { public void createBoard(int size) {
this.board = new Board(size); this.board = new Board(size);
@ -17,9 +26,7 @@ public abstract class Player {
public abstract void receiveHit(HitResponse hitResponse); public abstract void receiveHit(HitResponse hitResponse);
public void click(Point point) { public abstract void shoot(Point point);
}
public void beginTrun() { public void beginTrun() {
@ -35,4 +42,7 @@ public abstract class Player {
public String getName() { public String getName() {
return this.name; return this.name;
} }
public abstract void receiveCoin(boolean coin);
} }