gpt4 book ai didi

Java 数组列表和 Black Jack 游戏

转载 作者:行者123 更新时间:2023-12-01 07:08:23 32 4
gpt4 key购买 nike

我正在尝试制作一个非常简单的黑 jack 游戏,但我在从数组列表中抽牌并将它们放入玩家手中和庄家手中时遇到问题......

这就是我到目前为止所掌握的内容,任何愿意提供帮助的人将不胜感激。

我也意识到我很多时候没有以最有效的方式来做这件事......

卡片数组列表

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

}

闲家和庄家手牌等级

这里是我想要用数组中的两张随机卡填充 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();
}

}

最佳答案

当您发布问题时,最佳做法通常是 say what you have tried 。这样其他人就可以帮助您了解哪里可能出了问题。

我只是想给你一些关于如何解决问题的一般性建议。如果您在实现时遇到具体问题,您可以提出一个新问题(或根据问题的相关程度更新此问题)。

在真正的二十一点游戏中,您需要洗牌,然后取出最上面的 4 张牌。您可以在这里做同样的事情:

  1. Shuffle the ArrayList
  2. 取出顶 4 张牌并将其放入闲家/庄家手中。 (也许您可以使用 remove 方法删除卡片组中的第一个或最后一个元素。)

这看起来像是家庭作业,因此您可能需要使用 ArrayList。但这确实不是这里使用的最佳数据结构。如果你有选择,我会使用 ArrayDeque反而。它具有高效的 pop()remove() 方法,用于从集合中取出第一个或最后一个项目。

(提示:“deque”代表双端队列,但它的发音为“deck”。这是因为它在概念上就像一副纸牌......)

关于Java 数组列表和 Black Jack 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18729849/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com