繁体   English   中英

编写java游戏二十一点的方法

[英]Writing a method for java game blackjack

因此,我正在努力为名为 simple21 或“二十一点”的游戏程序创建方法offerCard

我将如何创建条件( if | loops ),以便每个计算机播放器( player1player2player3 )都能清楚地使用该方法?

例如,如果我在player1上调用该方法,那么它会分析自己的牌,然后考虑其他玩家的牌。

同样,如果我在玩家 2 上调用此方法,它会先分析自己的牌,然后再分析其他玩家的牌。

我可以为每个玩家编写if循环并为每个玩家调用方法: player1.offerCardplayer2.offercard ,...等? 它只会在调用该方法的播放器上运行循环吗?

    package simple21;
    
    /**
     * Represents a computer player in this simplified version of the "21" card game.
     */
    public class ComputerPlayer {
    
        /** 
         * The name of the player.
         */
        String name;
        
        /**
         * The player's one hidden card (a value from 1 - 10).
         */
        private int hiddenCard = 0;
        
        /** 
         * The sum of the player's cards, not counting the hidden card. 
         */
        private int sumOfVisibleCards = 0;
        
        /**
         * Flag indicating if the player has passed (asked for no more cards).
         */
        boolean passed = false;
        
        /**
         * Constructs a computer player with the given name.
         * @param name of the user.
         */
        public ComputerPlayer (String name) {
            this.name = name;
        }
        
        /**
         * Decides whether to take another card. In order to make this decision, this player considers 
         * their own total points (sum of visible cards + hidden card). 
         * This player may also consider other players' sum of visible cards, but not the value 
         * of other players' hidden cards.
         * @param human The other human player
         * @param player1 Another (computer) player
         * @param player2 Another (computer) player
         * @param player3 Another (computer) player
         * @return true if this player wants another card
         */
        public boolean offerCard(HumanPlayer human, ComputerPlayer player1, ComputerPlayer player2, ComputerPlayer player3) { 
            // Students: your code goes here.
            
    
            return (human.getSumOfVisibleCards() + hiddenCard >= 15 && player1.getScore() <= 15);
        }
      

ComputerPlayer是一种对象类型... ComputerPlayer每个实例(即ComputerPlayer player1ComputerPlayer player2ComputerPlayer player3 )都是 ComputerPlayer 的一个实例。 当你为offerCard编写方法offerCard

您需要分别调用 player1、player2、player3 和 humanPlayer 的方法来获取必要的数据字段,将它们存储在局部变量(输入)中,执行一些过程,然后做出决定(输出)计算机是否接受另一张卡(整数)。

您会发现一个问题是每个计算机播放器对方法offerCard都采用相同的输入( ComputerPlayer player1ComputerPlayer player2ComputerPlayer player3 )。 只是,其中一位电脑玩家会是this 因此,您可以做的是创建一个接受(新类) Player列表的interface 它将有一种方法,例如calculate(List<Player> players)

这个新方法decide然后将迭代不是this玩家的玩家(Human/ComputerPlayer 都将使用不同的实现扩展玩家)并提出一种方法来提供一些响应,该响应可以包含在更大的计算中,以确定是否接受卡。

使用该接口意味着您可以多态地处理HumanPlayerComputerPlayer 没有它,您将有更多的工作要做。 不知道电脑玩家做出的决定好不好太重要了? 但我认为“还可以”足以做被认为是好的工作。 我会把它留给你的判断。

昨晚我在这方面做了一些尝试,但还远未完成。 注意我已经通过返回诸如 12/14 之类的任意值来启动一些方法......这些需要使用实际值/计数等进行更新,但可以帮助您继续开发,直到您准备好更全面地实现它们.

package simple21;

import java.util.Random;

/**
 * Represents a computer player in this simplified version of the "21" card game.
 */
class ComputerPlayer implements Player {

    // final = read-only once set per instance.
    private final String name;

    private int hiddenCard;
    private int sumOfVisibleCards;
    private int cardCount;
    boolean passed;

    public ComputerPlayer(String name) {
        this.name = name;
        this.hiddenCard = 0;
        this.sumOfVisibleCards = 0;
        this.cardCount = 2;
        this.passed = false;
    }

    /**
     * Computer decides whether to take another card. In order to make this decision, this computer player considers
     * their own total points (sum of visible cards + hidden card).
     * This player may also consider other players' sum of visible cards, but not the value
     * of other players' hidden cards.
     *
     * @param human   The other human player
     * @param player1 Another (computer) player
     * @param player2 Another (computer) player
     * @param player3 Another (computer) player
     * @return true if this player wants another card
     */
    public boolean offerCard(HumanPlayer human, ComputerPlayer player1, ComputerPlayer player2, ComputerPlayer player3) {
        // Students: your code goes here.


        return (human.getSumOfVisibleCards() + hiddenCard >= 15 && player1.getScore() <= 15);
    }

    public void acceptCard(int cardValue) {

    }

    private int getScore() {
        return 14;
    }

    public int getSumOfVisibleCards() {
        return sumOfVisibleCards;
    }
}

class HumanPlayer implements Player {

    // final = read-only once set (individual HumanPlayer instances can have different names)
    private final String name;
    private int hiddenCard;
    private int visibleCard;
    private int cardCount;

    public HumanPlayer(String name) {
        this.name = name;
        this.cardCount = 2;
        this.visibleCard = 0;
        // Could use SecureRandom for a more correct approach.
        this.hiddenCard = new Random().nextInt(21) + 1;
    }

    public boolean offerCard(HumanPlayer human, ComputerPlayer player1, ComputerPlayer player2, ComputerPlayer player3) {
        // true if the average of visible cards is on the high side.
        // You'll probably want to be more sophisticated here... perhaps include the
        // count as part of the calculation as well as the total of your own cards and current count.

        return (player1.getSumOfVisibleCards() + player2.getSumOfVisibleCards() + player3.getSumOfVisibleCards()) / 3 >= 8 && cardCount <= 4;
    }

    public void acceptCard(int value) {
        cardCount++;
        visibleCard += value;
    }

    public int getSumOfVisibleCards() {
        int sumOfVisibleCards = 12; // set to arbitrary (valid) value.
        return sumOfVisibleCards;
    }
}

interface Player {
    boolean offerCard(HumanPlayer human, ComputerPlayer player1, ComputerPlayer player2, ComputerPlayer player3);
    void acceptCard(int value);
    int getSumOfVisibleCards();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM