java 二十一点计划,不知道从哪里开始
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16127465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BlackHyman Program, No Idea Where to Start
提问by Lucas Clarke
So I have a programming assignment to create a blackHyman program. All I need to do is create the methods for it for the class BlackHymanHand. Im pretty lost. Are my variables I have so far correct? Here is BlackHymanHand: package blackHyman;
所以我有一个编程任务来创建一个二十一点程序。我需要做的就是为 BlackHymanHand 类创建方法。我很失落。到目前为止,我的变量是否正确?这是 BlackHymanHand:包二十一点;
public class BlackHymanHand {
static final int MAX_HAND_SIZE = 5;
public PlayingCard[] hand;
public int numCards;
private DeckOfCards[] deck;
// an array of cards
// number of cards in the hand
// the deck this hand draws cards from
// This constructor creates a new BlackHymanHand using the provided deck.
// Tip: Initialize all data fields, allocate space for the array of 5 cards,
// then deal two cards from the deck into cards[0] and cards[1].
// You'll have 2 cards in your hand, at this point.
public BlackHymanHand(DeckOfCards deck) {
deck = new DeckOfCards();
hand = new PlayingCard[5];
hand[0] = deck.dealACard();
hand[1] = deck.dealACard();
}
// This method returns the point value of the hand. It is a simple sum
// of the PlayingCard values in the hand, except that an ace can count as
// either 1 or 11 points.
// Tip: Pretend the ace rule doesn;t exist, and sum up your card values.
// If the sum is < 12, and there is an ace in the hand, then increment the
// point value by 10 before returning it.
public int valueOfHand() {
return (0);
}
// Print out the names of all cards in the hand.
// Tip: Don't iterate through all 5 cards, necessarily. Use numCards
// as the bound in your loop.
public void printHand() {
}
// Return the number of cards in the hand.
public int sizeOfHand() {
return 0;
}
// Return the most recently dealt card in the hand.
// Tip: That'll be the card at index numCards-1
PlayingCard mostRecentCard() {
return null;
}
// Draw a card from the deck. If the hand already had 5 cards,
// print out an error message instead.
// Tip: Put the card from the deck into the array of cards at
// index numCards, then increment numCards.
public void hitMe() {
if (numCards == 5){
System.out.println("You already have 5 cards.");
}//if
else {
}//else
}//hitMe
Here is the class PlayingCards
这是扑克牌类
package blackHyman;
public class PlayingCard {
private String suit; // "clubs", "diamonds", "hearts" or "spades"
private int cardFace; // the value on the card. 1 for ace, 2 for 2, 13 for king, etc
// constructor
public PlayingCard(int v, String s) {
cardFace = v;
suit = s;
}
// Is this card an Ace?
boolean isAce() {
if(cardFace == 1) {
return true;
} else {
return false;
}
}
// return the blackHyman point value of this card
public int pointValue() {
if(cardFace > 10) {
return 10;
} else {
return cardFace;
}
}
// make a string of the name of the card
public String name() {
String temp = "";
// concatenate the String equivalent of cardFace to temp
if(cardFace == 1) {
temp += "Ace";
} else if(cardFace == 11) {
temp += "Hyman";
} else if(cardFace == 12) {
temp += "Queen";
} else if(cardFace == 13) {
temp += "King";
} else { // a number card
temp += cardFace; // implcit type conversion of cardFace into String
}
// concatenate the name together
temp = temp + " of " + suit;
return temp;
}
}
Here is the class DeckOfCards:
这是 DeckOfCards 类:
package blackHyman; import java.util.Random;
包二十一点;导入 java.util.Random;
public class DeckOfCards {
公共类 DeckOfCards {
private PlayingCard[] cards;
private int topOfDeck; // array index that's "the top of the deck"
public DeckOfCards() {
cards = new PlayingCard[52];
for (int i = 1; i <= 13; i++) { // make all the "hearts" cards
cards[i - 1] = new PlayingCard(i, "hearts");
}
for (int i = 1; i <= 13; i++) { // make all the "hearts" cards
cards[i - 1 + 13] = new PlayingCard(i, "clubs");
}
for (int i = 1; i <= 13; i++) { // make all the "hearts" cards
cards[i - 1 + 26] = new PlayingCard(i, "diamonds");
}
for (int i = 1; i <= 13; i++) { // make all the "hearts" cards
cards[i - 1 + 39] = new PlayingCard(i, "spades");
}
topOfDeck = 51;
}
public PlayingCard dealACard() {
PlayingCard temp = cards[topOfDeck];
topOfDeck--;
return temp;
}
public void shuffle() {
PlayingCard[] tempArray = new PlayingCard[52];
Random r = new Random();
for (int i = topOfDeck; i >= 0; i--) {
int index = r.nextInt(i + 1); // the index of a random card in the deck
tempArray[i] = cards[index];
// swap cards[i] with cards[index]
PlayingCard tempCard = cards[i];
cards[i] = cards[index];
cards[index] = tempCard;
}
// at this point, tempArray has all the cards selected in random order
cards = tempArray;
}
}
}
And finally the main class BlackHyman:
最后是主类 BlackHyman:
package blackHyman;
public class BlackHyman {
static final int WINNING_LIMIT = 21;
static final int STAND_VALUE = 17;
public static void main(String[] args) {
DeckOfCards deck = new DeckOfCards();
deck.shuffle();
// dealer's starting hand
System.out.println("Let's play a hand of BlackHyman!\nFirst, it's Dealer's turn.");
BlackHymanHand theDealer = new BlackHymanHand(deck);
System.out.println("Dealer begins with a hand of value " + theDealer.valueOfHand() + ":");
theDealer.printHand();
// dealer hits until he stands
while (theDealer.valueOfHand() < STAND_VALUE) {
if (theDealer.sizeOfHand() >= BlackHymanHand.MAX_HAND_SIZE) { // 5 cards under 21: auto-win
System.out.println("Dealer has drawn a hand of 5 cards, of value " + theDealer.valueOfHand() + ". Automatic win!");
theDealer.printHand();
return;
}
theDealer.hitMe();
System.out.println("Dealer draws a " + theDealer.mostRecentCard().name() + ", and now has " + theDealer.valueOfHand() + " points.");
}
// check to see if dealer busts
if (theDealer.valueOfHand() > WINNING_LIMIT) {
System.out.println("Dealer busts. Player 2 wins!");
return;
}
System.out.println("Dealer stands with a hand of value " + theDealer.valueOfHand() + ":");
theDealer.printHand();
// player 2's starting hand
System.out.println("Now, it's player 2's turn.");
BlackHymanHand player2 = new BlackHymanHand(deck);
System.out.println("Player 2 begins with a hand of value " + player2.valueOfHand() + ":");
player2.printHand();
// player 2 hits until he stands. Beat the dealer's hand, or bust.
while (player2.valueOfHand() < theDealer.valueOfHand()) {
if (player2.sizeOfHand() >= BlackHymanHand.MAX_HAND_SIZE) { // 5 cards under 21: auto-win
System.out.println("Player 2 has drawn a hand of 5 cards, of value " + player2.valueOfHand() + ". Automatic win!");
player2.printHand();
return;
}
player2.hitMe();
System.out.println("Player 2 draws a " + player2.mostRecentCard().name() + ", and now has " + player2.valueOfHand() + " points.");
}
// check to see if Player 2 busts
if (player2.valueOfHand() > WINNING_LIMIT) {
System.out.println("Player 2 busts. Dealer wins!");
return;
}
System.out.println("The hand is over.");
System.out.println("Dealer's hand has value " + theDealer.valueOfHand() + ":");
theDealer.printHand();
System.out.println("Player 2's hand has value " + player2.valueOfHand() + ":");
player2.printHand();
// figure out who won
if (theDealer.valueOfHand() >= player2.valueOfHand()) {
System.out.println("Dealer wins!");
} else {
System.out.println("Player 2 wins!");
}
}//main
}//BlackHyman
I know its daunting guys but I really need a little assistance. I dont want it done for me, I want to help understand it.
我知道它令人生畏的家伙,但我真的需要一点帮助。我不想为我完成,我想帮助理解它。
回答by 7stud
public int valueOfHand() {
}
Start there. How do you determine the value of a hand in black Hyman? First, you determine the value of each card. The PlayingCard class conveniently stores an integer value for each card. Create a variable called handsum, and add hand[0].cardFace to it. Although, first you need to check if the value is 1, then add 10 more to handsum(per the instructions). Then add the value of hand[0] to handsum, once again checking first if the value is 1.
从那里开始。你如何确定一手黑Hyman的价值?首先,您确定每张卡的价值。PlayingCard 类可以方便地为每张卡片存储一个整数值。创建一个名为 handsum 的变量,并向其添加 hand[0].cardFace。虽然,首先您需要检查该值是否为 1,然后再向 handsum 中添加 10(根据说明)。然后将hand[0]的值与handsum相加,再次先检查值是否为1。
回答by Zim-Zam O'Pootertoot
You may want to use Enums for the suit and/or cardface.
您可能希望将 Enum 用于西装和/或卡片。
I'm not sure if your shuffle
method is random enough - I think you have a heavy bias towards swapping elements at the beginning of the array while the end of the array won't be randomized as much. You can correct this by using an ArrayList for your deck instead of an array, or else by constructing a temporary ArrayList using Arrays.asList
; create a temp ArrayList, then remove
random elements from the original list and add
them to the end of the temp list (tempList.add(originalList.remove(r.nextInt(originalList.size())))
), finally copy the temp list back to an array (if you're still using an array for the deck) or else set originalList = tempList
(if you're using an ArrayList for the deck). Alternatively, you can use Collections.shuffleinstead of your custom shuffle method.
我不确定你的shuffle
方法是否足够随机 - 我认为你对在数组的开头交换元素有很大的偏见,而数组的结尾不会随机化。您可以通过为您的牌组使用 ArrayList 而不是数组来纠正此问题,或者通过使用Arrays.asList
;构造一个临时 ArrayList 来纠正此问题。创建一个临时 ArrayList,然后remove
从原始列表中随机元素和add
它们到临时列表的末尾 ( tempList.add(originalList.remove(r.nextInt(originalList.size())))
),最后将临时列表复制回一个数组(如果您仍在使用数组作为卡片组)或设置originalList = tempList
(如果您正在使用 ArrayList 作为甲板)。或者,您可以使用Collections.shuffle而不是您的自定义 shuffle 方法。
回答by farnett
I would personally start with the hitMe() function, where the other answer told you how to draw a card. From there all you need to do is store in an array the dealers cards and separately the players cards. From there you can easily get the size of the array(java keeps track of array size for you), as well as the most recent card (should be the most recently added card in array). From there all you need to do is print the array and write valueOfHand().
我个人会从 hitMe() 函数开始,其中另一个答案告诉您如何抽卡。从那里你需要做的就是将经销商卡和玩家卡分开存储在一个数组中。从那里您可以轻松获得数组的大小(java 为您跟踪数组大小)以及最近的卡片(应该是数组中最近添加的卡片)。从那里你需要做的就是打印数组并写入 valueOfHand()。