我正在尝试学习Java,我复制了一个模拟二十一点游戏的程序(来自这里:https://www.youtube.com/watch?v=buGFs1aQgaY)。
我理解了大部分内容,但是我被困在程序如何得出一手牌之前。在第四类,称为“播放器”,在某些时候有:
cardNum = this.hand [c] .getNumber();
让我们说:
this.hand [c] =俱乐部之王。
我不明白程序如何给出结果:
cardNum = 13。
getNumber方法如何工作? (该方法在第一类中进行,称为Card)
我将发布该程序的所有类。
第一类。
public class Card {
private Suit mySuit;
private int myNumber;
public Card(Suit aSuit, int aNumber){
this.mySuit = aSuit;
if (aNumber >= 1 && aNumber <= 13){
this.myNumber = aNumber;
} else{
System.err.println(aNumber + " is not a valid Card number");
System.exit(1);
}}
public int getNumber(){
return myNumber;
}
public String toString(){
String numStr = "Err";
switch(this.myNumber){
case 2:
numStr = "Two";
break;
case 3:
numStr = "Three";
break;
case 4:
numStr = "Four";
break;
case 5:
numStr = "Five";
break;
case 6:
numStr = "Six";
break;
case 7:
numStr = "Seven";
break;
case 8:
numStr = "Eight";
break;
case 9:
numStr = "Nine";
break;
case 10:
numStr = "Ten";
break;
case 11:
numStr = "Jack";
break;
case 12:
numStr = "Queen";
break;
case 13:
numStr = "King";
break;
case 1:
numStr = "Ace";
break;
}
return numStr + " of " + mySuit.toString();
}
}
第二类
public enum Suit {
Clubs,
Diamonds,
Spades,
Hearts
}
第三类
import java.util.Random;
public class Deck {
private Card[] myCards;
private int numCards;
public Deck(){
this(1,false);
}
public Deck(int numDecks, boolean shuffle) {
this.numCards = numDecks * 52;
this.myCards = new Card[this.numCards];
int c = 0;
for (int d = 0; d < numDecks; d++) {
for (int s = 0; s < 4; s++) {
for (int n = 1; n <= 13; n++) {
this.myCards[c] = new Card(Suit.values()[s], n);
c++;
}
}
}
if (shuffle) {
this.shuffle();
}
}
public void shuffle(){
Random rng = new Random();
Card temp;
int j;
for (int i = 0; i < this.numCards; i++){
j = rng.nextInt(this.numCards);
temp = this.myCards[i];
this.myCards[i] = this.myCards[j];
this.myCards[j] = temp;
}
}
public Card dealNextCard() {
Card top = this.myCards[0];
for (int c = 1; c < this.numCards; c++) {
this.myCards[c - 1] = this.myCards[c];
}
this.myCards[this.numCards - 1] = null;
this.numCards--;
return top;
}
public void printDeck(int numToPrint){
for(int c=0; c < numToPrint; c++) {
System.out.printf("% 3d/%d/%s\n", c + 1, this.numCards,
this.myCards[c].toString());
}
System.out.printf("/t/t[%d other]\n", this.numCards-numToPrint);
}
}
FORTH CLASS
public class Player {
private String name;
private Card[]hand = new Card[10];
private int numCards;
public Player(String aName) {
this.name = aName;
this.emptyHand();
}
public void emptyHand(){
for(int c=0; c<10; c++){
this.hand[c] = null;
}
this.numCards = 0;
}
public boolean addCard(Card aCard) {
if (this.numCards == 10) {
System.err.printf("%s's hand already has 10 cards;" +
"cannot add another\n", this.name);
System.exit(1);
}
this.hand[this.numCards] = aCard;
this.numCards++;
return(this.getHandSum()<= 21);
}
public int getHandSum(){
int handSum = 0;
int cardNum;
int numAces = 0;
for(int c=0; c < this.numCards; c++) {
cardNum = this.hand[c].getNumber();
if (cardNum == 1) {
numAces++;
handSum += 11;
} else if (cardNum > 10) {
handSum += 10;
} else {
handSum += cardNum;
}
}
while (handSum >21 && numAces > 0){
handSum -= 10;
numAces--;
}
return handSum;
}
public void printHand(boolean showFirstCard){
System.out.printf("%s's cards:\n", this.name);
for(int c=0; c<this.numCards; c++) {
if(c==0 && !showFirstCard) {
System.out.println(" [hidden]");
} else {
System.out.printf(" %s\n", this.hand[c].toString());
}
}
}
}
第五类 - 计划
import java.util.Scanner;
public class GameRunner{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Deck theDeck = new Deck(1, true);
theDeck.printDeck(52);
Player me = new Player("Player 1");
Player dealer = new Player("Dealer");
me.addCard(theDeck.dealNextCard());
dealer.addCard(theDeck.dealNextCard());
me.addCard(theDeck.dealNextCard());
dealer.addCard(theDeck.dealNextCard());
System.out.println("Cards are dealt\n");
me.printHand(true);
dealer.printHand(false);
System.out.println("\n");
boolean meDone = false;
boolean dealerDone = false;
String ans;
while(!meDone || !dealerDone) {
if(!meDone){
System.out.println("Hit or Stay? (Enter H or S):");
ans = sc.next();
System.out.println();
if(ans.compareToIgnoreCase("H") == 0) {
meDone = !me.addCard(theDeck.dealNextCard());
me.printHand(true);
} else {
meDone = true;
}
}
if(!dealerDone){
if (dealer.getHandSum() < 17) {
System.out.println("The Dealer hits\n");
dealerDone = !dealer.addCard(theDeck.dealNextCard());
dealer.printHand(false);
} else {
System.out.println("The Dealer stays\n");
dealerDone = true;
}
}
System.out.println();
}
sc.close();
me.printHand(true);
dealer.printHand(true);
int mySum = me.getHandSum();
int dealerSum = dealer.getHandSum();
if(mySum > dealerSum && mySum <= 21 || dealerSum>21) {
System.out.println("Your win");
} else{
System.out.println("Dealer wins!");
}
}
}
当您创建类Card
的每个实例时,为该卡传递Suit和VALUE:
... = new Card(Suit.values()[s], n);
在代码中的上述示例中,n
变量是卡的值,并且从1到13,因为for
循环设置创建卡片组。
在类Card
的构造函数中,我们将该值存储在名为myNumber
的私有成员中,如下所示:
public class Card {
private Suit mySuit;
private int myNumber; // <-- the value of the card is stored here
public Card(Suit aSuit, int aNumber){
// ... other code ...
this.myNumber = aNumber; // <-- store the passed in value in the instance member
// ... other code ...
}
}
最后,getNumber()
函数只返回卡的存储值,之前放入myNumber
:
public int getNumber(){
return myNumber;
}