fixed Exception handling

This commit is contained in:
2025-05-13 13:01:17 +02:00
parent 136be17443
commit 703097ad4d
2 changed files with 22 additions and 11 deletions

View File

@@ -4,9 +4,9 @@ import java.util.List;
public abstract class Game {
private static int counter = 0;
private final int id;
protected final int id;
//null nicht notwendig
private String winner = null;
protected String winner = null;
protected Game() {
@@ -22,12 +22,16 @@ public abstract class Game {
}
public void setWinner(String winner) {
String player1 = getPlayer1();
System.out.println("Player 1: " + player1);
String player2 = getPlayer2();
System.out.println("Player 2: " + player2);
this.winner = this.winner == null && player1 != null && player2 != null && (winner == player1 || winner == player2) ? this.winner = winner : this.winner;
}
if (this.winner != null)
throw new IllegalStateException("winner already set");
if (getPlayer1() == null || getPlayer2() == null)
throw new IllegalStateException("Opponents are not yet known");
if (getPlayer1().equals(winner) || getPlayer2().equals(winner))
this.winner = winner;
else
throw new IllegalArgumentException("Unknown player " + winner);
}
public abstract String getPlayer1();