Я делаю модель ООП, где игроки могут присоединяться к играм на игровой платформе. Один из критериев состоял в том, чтобы не допустить, чтобы одно и то же лицо было обоими игроками, играющими в игру, и теперь я застрял в том, как расширить это число, так как число игроков может быть максимальным
В классе игрока ..
публичный класс GameExt2 {
private final int minAge;
private final PlayerExt2[] players;
private String gameName;
private int gameId;
private double fee;
private double amountOwed;
private double totalFeesOwed;
private int numberOfPlayersPlaying;
private String playerNames;
private int oweLimit;
private int maxNumberPlayers;
public GameExt2( String gameName, int gameId, int minAge, double fee, int oweLimit, int maxNumberPlayers){
this.gameId=gameId;
this.gameName=gameName;
this.minAge=minAge;
this.fee=fee;
this.oweLimit=oweLimit;
this.players=new PlayerExt2[maxNumberPlayers];
public String matchId(PlayerExt2 player){
return("Sorry " + player.getName()+ ", you cannot be both players");
}
public String isTooYoung(PlayerExt2 player){
return("Sorry " + player.getName()+ ", you are too young to play" +gameName);
}
public String maxPlayersReached(PlayerExt2 player){
return("Sorry " + player.getName()+ ", there are already "+maxNumberPlayers+ " playing");
}
private void playerJoined(PlayerExt2 player){
System.out.println(player.getName() + " has joined the game " + gameName);
}
public void addPlayer(PlayerExt2 player) {
int nextFreeSlot = getNextFreeSlot();
if (nextFreeSlot > 0) {
if (isEligible(player)) {
if (!isAlreadyPlaying(player)) {
players[nextFreeSlot] = player;
playerJoined(player);
player.addGameJoined();
player.addFeeOwed(fee);
player.setNameGamePlaying(gameName); //necessary for printing details of the player
}
else {
matchId(player);
// Already playing
}
}
else {
isTooYoung(player);
//player inelligible to play
}
}
else {
maxPlayersReached(player);
// game already full
}
}
private boolean isAlreadyPlaying(PlayerExt2 newPlayer) {
for (PlayerExt2 player : players) {
if (player.getId() == newPlayer.getId()) return true;
}
return false;
}
private int getNextFreeSlot() {
for (int i = 0; i < players.length; i++) {
if (players[i] == null) return i;
}
return -1; // negative indicates there's no free slot
}
private boolean isEligible(PlayerExt2 player) {
return player.getAge() > minAge;
}
Player class...
public String addFeeOwed(double fee){
amountOwed=amountOwed+fee;
return("For playing this game of "+nameGamePlaying+ ", "+playerName+"'s balance now stands at £"+amountOwed);
}
public void addGameJoined(){
gamesJoined++;
}
раздел основного ...
GameExt2 snakesAndLadders=new GameExt2("SnakesAndLadders",4564345,8,3,10,4);
PlayerExt2 marina= new PlayerExt2(123, "Marina", 15, 4,1999,0);
marina.calculateAge();
System.out.println(marina.printDetails());
snakesAndLadders.addPlayer(marina);
PlayerExt2 erin=new PlayerExt2(163,"Erin",3,6,2010,0);
erin.calculateAge();
System.out.println(erin.printDetails());
snakesAndLadders.addPlayer(erin);
PlayerExt2 matthew=new PlayerExt2(312,"Matthew",27,5,2002,12);
matthew.calculateAge();
System.out.println(matthew.printDetails());
snakesAndLadders.addPlayer(matthew);