Compare commits

...

2 Commits

Author SHA1 Message Date
e7f7a20ea6 formatting 2025-05-13 13:06:17 +02:00
703097ad4d fixed Exception handling 2025-05-13 13:01:17 +02:00
4 changed files with 70 additions and 61 deletions

View File

@ -8,7 +8,7 @@ public class ByeGame extends Game{
private final String player1;
private final Game ref;
ByeGame(String player1, Game ref){
ByeGame(String player1, Game ref) {
super();
this.ref = ref;
this.player1 = player1;
@ -38,16 +38,22 @@ public class ByeGame extends Game{
@Override
public List<Game> getAllGames()
{
List<Game> temp = new ArrayList(Arrays.asList(this.getId()));
public List<Game> getAllGames() {
List<Game> temp = new ArrayList<>();
temp.add(this);
temp.addAll(ref.getAllGames());
return temp;
}
@Override
public List<String> getRemaningPlayers() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getRemaningPlayers'");
if (winner != null) {
return Arrays.asList(winner);
} else {
List<String> players = new ArrayList<>();
players.add(player1);
players.addAll(ref.getRemaningPlayers());
return players;
}
}
}

View File

@ -3,10 +3,11 @@ package tournament;
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,13 +23,17 @@ 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();
public abstract String getPlayer2();
@ -39,9 +44,8 @@ public abstract class Game {
public abstract List<Game> getAllGames();
public String toString(){
public String toString() {
return "Game: " + getId() + " Player: "+ getPlayer1()+ " vs Player: " + getPlayer2() + "Winner is: "+ getWinner();
}
}

View File

@ -5,45 +5,46 @@ import java.util.Arrays;
import java.util.List;
public class OrdinaryGame extends Game{
private Game Player1;
private Game Player2;
public OrdinaryGame(Game Player1, Game Player2)
{
public OrdinaryGame(Game Player1, Game Player2) {
this.Player1 = Player1;
this.Player2 = Player2;
}
@Override
public String getPlayer1()
{
public String getPlayer1() {
return Player1.getWinner();
}
@Override
public String getPlayer2()
{
public String getPlayer2() {
return Player2.getWinner();
}
@Override
public List<String> getAllPlayers()
{
List<String> temp = new ArrayList();
public List<String> getAllPlayers() {
List<String> temp = new ArrayList<>();
temp.addAll(Player1.getAllPlayers());
temp.addAll(Player2.getAllPlayers());
return temp;
}
@Override
public List<String> getRemaningPlayers()
{
public List<String> getRemaningPlayers() {
if(this.getWinner() != null)
return new ArrayList<>(Arrays.asList(this.getWinner()));
List<String> temp = new ArrayList();
List<String> temp = new ArrayList<>();
temp.addAll(Player1.getRemaningPlayers());
temp.addAll(Player2.getRemaningPlayers());
return temp;
}
@Override
public List<Game> getAllGames()
{
List<Game> temp = new ArrayList(Arrays.asList(this.getId()));
public List<Game> getAllGames() {
List<Game> temp = new ArrayList<>();
temp.addAll(Player1.getAllGames());
temp.addAll(Player2.getAllGames());
return temp;

View File

@ -6,6 +6,7 @@ import java.util.Arrays;
import java.util.List;
public class SeededGame extends Game{
private String player1;
private String player2;
@ -16,35 +17,32 @@ public class SeededGame extends Game{
}
@Override
public String getPlayer1()
{
public String getPlayer1() {
return player1;
}
@Override
public String getPlayer2()
{
public String getPlayer2() {
return player2;
}
@Override
public List<String> getAllPlayers()
{
public List<String> getAllPlayers() {
//return new ArrayList<String>(Arrays.asList(player1, player2));
return List.of(player1, player2);
}
@Override
public List<String> getRemaningPlayers()
{
if(this.getWinner() != null)
public List<String> getRemaningPlayers() {
if(this.getWinner() != null) {
return new ArrayList<>(Arrays.asList(this.getWinner()));
List<String> temp = new ArrayList(Arrays.asList(player1,player2));
return temp;
}
return List.of(player1, player2);
}
@Override
public List<Game> getAllGames()
{
public List<Game> getAllGames() {
return List.of(this);
}
}