【发布时间】:2018-10-30 09:44:51
【问题描述】:
我正在制作一个 OOP 模型,玩家可以在其中加入游戏平台内的游戏。标准之一是不允许同一个人同时玩游戏,现在我被困在如何扩展这一点,因为有任意数量的最大玩家
在播放器类中..
公共类 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);
【问题讨论】:
-
如果
playerId是唯一的,那不就是playerOne.playerId != playerTwo.playerId吗? -
为什么构造函数中有方法声明? (猜你格式不对)
-
@Andrew 我想它应该是两个相邻的方法:构造函数,然后是
joinGame。我只是格式化了那里的内容-一团糟。 -
我对您的代码应该如何构建进行了有根据的猜测。如果你学会了正确的缩进,你就不会有这个问题。
-
是的!这会出现在条件语句中吗?(对不起,我是编程新手)