不允许同一个玩家同时参与游戏

时间:2018-10-30 09:44:52

标签: java oop

我正在建立一个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);

2 个答案:

答案 0 :(得分:1)

如果您认为这应该从概念上进行构造,那么这些并不是玩家应该自己执行的约束。在现实生活中,玩家并不关心自己是否太小而无法玩。在比赛中,这将是某些管理员或裁判的职责。我们不必对此进行精确建模,但要点是它应该在玩家的控制范围之外。我将这种逻辑放在ng run {PROJECT_NAME}:server类中,并添加了Game个类。

您已经看到这已经对您的代码产生了奇怪的影响。您的播放器有一个Player字段。这有意义吗? numberOfPlayers是一个人的财产吗?您的护照或驾照上是否有该信息?

这是一个粗略的结构(不是完整的代码)

numberOfPlayers

答案 1 :(得分:0)

一些注意事项:

  • joinGame()不应在Player类中。放置此方法是不可扩展的,但是从它提到在Player中未引用的参数名称GamePlaying的事实判断,该方法已经存在于Game.class或任何与之等效的东西中。
  • 如果要比较两个实例是否表示相同的对象,请重写equals方法,因为Java中的许多方法都使用它来进行比较,因此这样做可以省去很多麻烦。 equals方法应该比较两个实例在逻辑上是否相等,因此在这种情况下,我们只需要比较playerId,但是其他类可能需要其他比较字段。
  • 不要硬编码常量,例如最大玩家数。您的示例证明了这一点,因为您实际上最多需要2个玩家,但将值编码为3。当您意识到此错误时,或者如果您仅想增加最大玩家数,则必须跟踪每个出现3并替换它。而是在类级别声明一个常量MAX_PLAYERS,并在必要时引用它。
  • 您需要在Person.class中声明吸气剂,以使以下内容正常工作。
  • 嵌套if语句以消除重复。如果您将后续逻辑嵌套在检查最大玩家数的if语句中,则无需像您最初那样不断检查最大玩家数。

下面的代码,如有任何疑问,请问。


//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;
}