From 43545afb45ec75ccba8345571f646ce31ba4f8d0 Mon Sep 17 00:00:00 2001 From: Ole Wachtel Date: Tue, 3 Dec 2024 12:11:06 +0100 Subject: [PATCH] add coin flip and turn logic --- src/HitResponse.java | 4 ++++ src/LocalPlayer.java | 42 ++++++++++++++++++++++++++++++++++++- src/OnlinePlayer.java | 5 +++++ src/OnlinePlayer_1_1_0.java | 16 +++++++++++++- src/Player.java | 18 ++++++++++++---- 5 files changed, 79 insertions(+), 6 deletions(-) diff --git a/src/HitResponse.java b/src/HitResponse.java index 89f713a..2e5f0ca 100644 --- a/src/HitResponse.java +++ b/src/HitResponse.java @@ -34,4 +34,8 @@ public class HitResponse { public String toString() { return this.getPoint().toString() + " " + this.type.ordinal(); } + + public HitResponseType getType() { + return type; + } } diff --git a/src/LocalPlayer.java b/src/LocalPlayer.java index 4c581f6..5bc6dd9 100644 --- a/src/LocalPlayer.java +++ b/src/LocalPlayer.java @@ -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); } } \ No newline at end of file diff --git a/src/OnlinePlayer.java b/src/OnlinePlayer.java index 90dc5cd..88ac476 100644 --- a/src/OnlinePlayer.java +++ b/src/OnlinePlayer.java @@ -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); + } diff --git a/src/OnlinePlayer_1_1_0.java b/src/OnlinePlayer_1_1_0.java index 6146796..be1a678 100644 --- a/src/OnlinePlayer_1_1_0.java +++ b/src/OnlinePlayer_1_1_0.java @@ -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; + } + } } \ No newline at end of file diff --git a/src/Player.java b/src/Player.java index 5e5d83a..faf9abb 100644 --- a/src/Player.java +++ b/src/Player.java @@ -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); }