我正在尝试制作一个非常简单的黑杰克游戏,我在从阵列列表中抽取卡片并将它们放入玩家手中时遇到问题,并且经销商手中......
这就是我到目前为止所有愿意提供帮助的人都会非常感激。
我也意识到我很多人都没有以最有效的方式做到这一点......
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&amp;经销商手类
这里是我要填写dealPlayer()&amp; 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();
}
}
答案 0 :(得分:5)
当您发布问题时,say what you have tried通常是最佳做法。这样,其他人可以帮助您了解您可能出错的地方。
我将就如何解决问题向您提供一些一般性建议。如果您在实施它时遇到特定问题,可以提出一个新问题(或根据其相关程度更新此问题)。
在一个真正的二十一点游戏中,你可以将牌组洗牌,然后移除前4张牌。你可以在这里做同样的事情:
remove
方法删除套牌中的第一个或最后一个元素。)这看起来可能是家庭作业,因此您可能需要使用ArrayList。这不是最好的数据结构。如果您有选择,我会使用ArrayDeque代替。它具有高效的pop()
和remove()
方法,用于从集合中取出第一个或最后一个项目。
(提示:“deque”代表双端队列,但它的发音是“deck”。这是因为它在概念上就像一副纸牌......)
答案 1 :(得分:3)
Collections.shuffle(deck);
然后使用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));