简体   繁体   中英

Writing a method for java game blackjack

So I am struggling to create the method offerCard , for a game program called simple21 or "blackjack".

How would I create conditions ( if | loops ) so that each Computer player ( player1 , player2 , player3 ) would use the method distinctly?

For instance, if I call the method on player1 then it would analyze its own cards, then consider the other players' cards.

Similarly if I call this method on player 2, it would analyze it's own cards before analyzing other players cards.

Can I write if loops for each player and call the method for each player : player1.offerCard , player2.offercard , ... etc? 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. When you write the method for 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).

One gotcha you'll find is each computer player takes the same inputs for method offerCard ( ComputerPlayer player1 , ComputerPlayer player2 , ComputerPlayer player3 ). Only, one of the computer players will be this . So what you can do is create an interface that accepts a list of (new class) Player . It will have one method, something like 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.

Using the interface means you can process HumanPlayer s and ComputerPlayer s polymorphically. 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.

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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