简体   繁体   English

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

[英]Writing a method for java game blackjack

So I am struggling to create the method offerCard , for a game program called simple21 or "blackjack".因此,我正在努力为名为 simple21 或“二十一点”的游戏程序创建方法offerCard

How would I create conditions ( if | loops ) so that each Computer player ( player1 , player2 , player3 ) would use the method distinctly?我将如何创建条件( if | loops ),以便每个计算机播放器( player1player2player3 )都能清楚地使用该方法?

For instance, if I call the method on player1 then it would analyze its own cards, then consider the other players' cards.例如,如果我在player1上调用该方法,那么它会分析自己的牌,然后考虑其他玩家的牌。

Similarly if I call this method on player 2, it would analyze it's own cards before analyzing other players cards.同样,如果我在玩家 2 上调用此方法,它会先分析自己的牌,然后再分析其他玩家的牌。

Can I write if loops for each player and call the method for each player : player1.offerCard , player2.offercard , ... etc?我可以为每个玩家编写if循环并为每个玩家调用方法: player1.offerCardplayer2.offercard ,...等? Would it only run the loop the player the method was called on?它只会在调用该方法的播放器上运行循环吗?

    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 is an object type... each instance of ComputerPlayer (ie ComputerPlayer player1 , ComputerPlayer player2 , ComputerPlayer player3 ) is an instance of ComputerPlayer. ComputerPlayer是一种对象类型... ComputerPlayer每个实例(即ComputerPlayer player1ComputerPlayer player2ComputerPlayer player3 )都是 ComputerPlayer 的一个实例。 When you write the method for offerCard当你为offerCard编写方法offerCard

You'll need to call the methods of player1, player2, player3 and humanPlayer individually to grab the necessary fields of data, store them in local variables (inputs), carry out some process, and come up with a decision (output) as to whether or not the computer will accept another card (an integer).您需要分别调用 player1、player2、player3 和 humanPlayer 的方法来获取必要的数据字段,将它们存储在局部变量(输入)中,执行一些过程,然后做出决定(输出)计算机是否接受另一张卡(整数)。

One gotcha you'll find is each computer player takes the same inputs for method offerCard ( ComputerPlayer player1 , ComputerPlayer player2 , ComputerPlayer player3 ).您会发现一个问题是每个计算机播放器对方法offerCard都采用相同的输入( ComputerPlayer player1ComputerPlayer player2ComputerPlayer player3 )。 Only, one of the computer players will be this .只是,其中一位电脑玩家会是this So what you can do is create an interface that accepts a list of (new class) Player .因此,您可以做的是创建一个接受(新类) Player列表的interface It will have one method, something like calculate(List<Player> players)它将有一种方法,例如calculate(List<Player> players)

This new method decide will then iterate over the players that are not this player (Human/ComputerPlayer will both extend player with different implementations) and come up with a way to provide some response that can be included in a larger calculation that determines whether or not to accept a card.这个新方法decide然后将迭代不是this玩家的玩家(Human/ComputerPlayer 都将使用不同的实现扩展玩家)并提出一种方法来提供一些响应,该响应可以包含在更大的计算中,以确定是否接受卡。

Using the interface means you can process HumanPlayer s and ComputerPlayer s polymorphically.使用该接口意味着您可以多态地处理HumanPlayerComputerPlayer Without it you'll have a lot more work to do.没有它,您将有更多的工作要做。 I don't know its too important whether the decisions the computer player makes are good?不知道电脑玩家做出的决定好不好太重要了? But I'd assume being "ok" is enough to do what would be considered a good job.但我认为“还可以”足以做被认为是好的工作。 I'll leave that to your judgement.我会把它留给你的判断。

I had a bit of a go at this last night but its far from complete.昨晚我在这方面做了一些尝试,但还远未完成。 Note I've started some of the methods off by returning arbitrary values like 12/14... these need to be updated with actual values/counts etc. but can help you continue with development until you're ready to implement them more fully.注意我已经通过返回诸如 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