简体   繁体   English

Java数组列表和Black Jack游戏

[英]Java Array Lists, and a Black Jack game

I'm trying to make a very simple Black Jack game, and I'm having problems drawing cards from an Array List and putting them into a players hand, and a dealers hand... 我正在尝试制作一个非常简单的Black Jack游戏,但是在从Array List中抽牌并将其放入玩家手和庄家手时遇到了问题。

Here's what I have so far, anyone willing to help will be greatly appreciated. 到目前为止,这就是我所拥有的,任何愿意帮助的人将不胜感激。

I also realize that I many not be doing it in the most efficient way... 我也意识到我很多人没有以最有效的方式来做...

CARDS ARRAY LIST 卡阵列列表

import java.util.ArrayList;

public class deck {
    ArrayList<card> deck = new ArrayList<card>();

public deck () {

    deck = new ArrayList<card>();

    //Spades
    deck.add(new card ("2", "Spades"));
    deck.add(new card ("3", "Spades"));
    deck.add(new card ("4", "Spades"));
    deck.add(new card ("5", "Spades"));
    deck.add(new card ("6", "Spades"));
    deck.add(new card ("7", "Spades"));
    deck.add(new card ("8", "Spades"));
    deck.add(new card ("9", "Spades"));
    deck.add(new card ("10", "Spades"));
    deck.add(new card ("2", "Spades"));
    deck.add(new card ("J", "Spades"));
    deck.add(new card ("Q", "Spades"));
    deck.add(new card ("K", "Spades"));
    deck.add(new card ("A", "Spades"));
    //Clubs
    deck.add(new card ("2", "Clubs"));
    deck.add(new card ("3", "Clubs"));
    deck.add(new card ("4", "Clubs"));
    deck.add(new card ("5", "Clubs"));
    deck.add(new card ("6", "Clubs"));
    deck.add(new card ("7", "Clubs"));
    deck.add(new card ("8", "Clubs"));
    deck.add(new card ("9", "Clubs"));
    deck.add(new card ("10", "Clubs"));
    deck.add(new card ("2", "Clubs"));
    deck.add(new card ("J", "Clubs"));
    deck.add(new card ("Q", "Clubs"));
    deck.add(new card ("K", "Clubs"));
    deck.add(new card ("A", "Clubs"));
    //Hearts
    deck.add(new card ("2", "Hearts"));
    deck.add(new card ("3", "Hearts"));
    deck.add(new card ("4", "Hearts"));
    deck.add(new card ("5", "Hearts"));
    deck.add(new card ("6", "Hearts"));
    deck.add(new card ("7", "Hearts"));
    deck.add(new card ("8", "Hearts"));
    deck.add(new card ("9", "Hearts"));
    deck.add(new card ("10", "Hearts"));
    deck.add(new card ("2", "Hearts"));
    deck.add(new card ("J", "Hearts"));
    deck.add(new card ("Q", "Hearts"));
    deck.add(new card ("K", "Hearts"));
    deck.add(new card ("A", "Hearts"));
    //Diamonds
    deck.add(new card ("2", "Diamonds"));
    deck.add(new card ("3", "Diamonds"));
    deck.add(new card ("4", "Diamonds"));
    deck.add(new card ("5", "Diamonds"));
    deck.add(new card ("6", "Diamonds"));
    deck.add(new card ("7", "Diamonds"));
    deck.add(new card ("8", "Diamonds"));
    deck.add(new card ("9", "Diamonds"));
    deck.add(new card ("10", "Diamonds"));
    deck.add(new card ("2", "Diamonds"));
    deck.add(new card ("J", "Diamonds"));
    deck.add(new card ("Q", "Diamonds"));
    deck.add(new card ("K", "Diamonds"));
    deck.add(new card ("A", "Diamonds"));
}

public ArrayList<card> getCards(){
    return deck;
}

public card deal(){
    card one = deck.get((int) (Math.random() * deck.size()));
    deck.remove(one);
    return one;
}

}

PLAYER & DEALER HAND CLASS 玩家手牌

Here is where I'm looking to fill dealPlayer() & dealDealer() with two random cards from the array, I'm just in a stump right now and cant seem to figure out anything that will work... Thanks in advance for help, if theres anything else i need to post, please let me know! 这是我要用数组中的两张随机卡填充DealPlayer()和dealDealer()的地方,我现在正处于一个树桩,似乎无法找出任何可行的方法...在此先感谢帮助,如果还有其他需要发布的信息,请告诉我!

import java.util.ArrayList;

public class dealer {

    deck deck = new deck();
    String dealerTopCardString = "";
    card playerHandCard;
    card dealerHandCard;
    ArrayList<card> dealerHand = new ArrayList<card>();
    ArrayList<card> playerHand = new ArrayList<card>();

public void start() {
    dealerHand = new ArrayList<card>();
    playerHand = new ArrayList<card>();
}

public void dealPlayer() {
    //LOOKING FOR HELP HERE
}

public void dealDealer() {
    //LOOKING FOR HELP HERE
}

public String getPlayerHand() {

    String playerHandString = "";
    for (int i = 0; i < playerHand.size(); i++)
        playerHandCard = playerHand.get(i);
        card card = new card(playerHandCard.getValue(), playerHandCard.getSuit());
        playerHandString += "\n";
        playerHandString += card.getValue();
        playerHandString += "\t";
        playerHandString += card.getSuit();

        return playerHandString;

    }

public String getDealerHand() {

        String dealerHandString = "";
        for (int i = 0; i < dealerHand.size(); i++)
            dealerHandCard = dealerHand.get(i);
            card card = new card(dealerHandCard.getValue(), dealerHandCard.getSuit());
            dealerHandString += "\n";
            dealerHandString += card.getValue();
            dealerHandString += "\t";
            dealerHandString += card.getSuit();

            return dealerHandString;
    }

public String getDealerTopCard(){

    return getDealerTopCard;
}

public int getPlayerHandValue(){

    return getPlayerHandValue;
}

public int getDealerHandValue(){


    return getDealerHandValue;
}

public void resetDealerHand(){
    dealerHand.clear();
}

public void resetPlayerHand(){
    playerHand.clear();
}

}

When you post a question, it's usually best practice to say what you have tried . 发布问题时,通常最好的做法是说出您尝试过的内容 That way others can help you see where you might have gone wrong. 这样,其他人可以帮助您查看可能出了问题的地方。

I'm just going to give you some general advice on how you should go about solving the problem. 我将为您提供一些有关如何解决问题的一般建议。 If you have specific problems with implementing it you can ask a new question (or update this one depending on how related it is). 如果您在实施时遇到特定问题,可以提出一个新问题(或根据相关程度进行更新)。

In a real game of blackjack, you'd shuffle the deck and then remove the top 4 cards. 在二十一点的真实游戏中,您需要洗牌,然后移除前四张牌。 You can do the same thing here: 您可以在此处执行相同的操作:

  1. Shuffle the ArrayList 随机排列ArrayList
  2. Remove the top 4 cards to put in the player's/dealer's hands. 取出前四张纸牌,放在玩家/经销商的手中。 (Maybe you could use the remove method to remove the first or last element in the deck.) (也许您可以使用remove方法删除平台中的第一个或最后一个元素。)

This looks like it might be homework, so you might be required to use an ArrayList. 看来这可能是家庭作业,因此可能需要使用ArrayList。 That's really not the best data structure to use here though. 但是,实际上这并不是最好的数据结构。 If you have a choice, I'd use an ArrayDeque instead. 如果您有选择,我会改用ArrayDeque It has efficient pop() and remove() methods for taking the first or last item out of the collection. 它具有有效的pop()remove()方法,用于从集合中取出第一个或最后一个项目。

(Hint: "deque" stands for double-ended-queue , but it's pronounced "deck". This is because it's conceptually like a deck of cards...) (提示:“双端队列”代表双端队列 ,但发音为“ deck”。这是因为从概念上讲,它就像一副纸牌...)

Just shuffle your deck of cards using Collections#shuffle 只需使用Collections#shuffle洗牌

Collections.shuffle(deck);

Then just get the next card from the top of the deck using List#remove , so something like this for dealing the starting hands 然后只需使用List#remove从卡片组的顶部获取下一张卡片,那么类似这样的处理起手牌

playerHand.add(deck.remove(0));   // top card to player
dealerHand.add(deck.remove(0));   // top card to dealer
playerHand.add(deck.remove(0));
dealerHand.add(deck.remove(0));

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

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