我正在建立一个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);
答案 0 :(得分:1)
如果您认为这应该从概念上进行构造,那么这些并不是玩家应该自己执行的约束。在现实生活中,玩家并不关心自己是否太小而无法玩。在比赛中,这将是某些管理员或裁判的职责。我们不必对此进行精确建模,但要点是它应该在玩家的控制范围之外。我将这种逻辑放在ng run {PROJECT_NAME}:server
类中,并添加了Game
个类。
您已经看到这已经对您的代码产生了奇怪的影响。您的播放器有一个Player
字段。这有意义吗? numberOfPlayers
是一个人的财产吗?您的护照或驾照上是否有该信息?
这是一个粗略的结构(不是完整的代码)
numberOfPlayers
答案 1 :(得分:0)
一些注意事项:
下面的代码,如有任何疑问,请问。
//In Game.class or equivalent
private final int MAX_PLAYERS = 2;
private List<Player> playerList; //Should be initialised with the main Game(?) class.
public String joinGame(Person player){
if(playerList.size() < MAX_PLAYERS){
if(!playerList.contains(player) && player.getPlayerAge() >= minAge){
playerList.add(player)
return (player.getPlayerName() + " has joined the game " + nameGamePlaying + ".");
}else{
return ("Sorry, " + player.getPlayerName()+ " is too young to play or is already playing " + nameGamePlaying);
}
} else {
return ("Sorry, "+ player.getPlayerName()+ " cannot play " + nameGamePlaying + " as there are already two players.");
}
}
//In Person.class
@Override
public boolean equals(Object obj){
if(this == obj){
return true;
}
if(obj == null){
return false;
}
if(getClass() != obj.getClass()){
return false;
}
Person toCompare = (Person) obj;
if(this.playerId != toCompare.getPlayerId()){
return false;
}
//Optional, depending on your construct.
if(playerName == null){
if(toCompare.getPlayerName != null){
return false;
}
}else{
if(!playerName.equals(toCompare.getPlayerName(){
return false;
}
}
//... More logical equality checks
return true;
}