0
編寫了一個甲板和卡類的代碼,你可以在下面找到,但我不確定如何使用這些代碼來幫助我創建一個工作的二十一點遊戲。如何使用以下方法創建二十一點遊戲的基本幫助?
我是當然沒有要求你爲我做這項工作,也沒有告訴我所有的答案,只是給我一個基本的想法,你將如何使用基本的初學Java編程來編寫這個遊戲。
//Represent a playing card
public class Card
{
//Instance variables:
int suit; //0=clubs, 1=diamonds, 2=hearts, 3=spades
int rank; //1=ace, 2=2,..., 10=10, 11=J, 12=Q, 13=K
//Constructor:
public Card (int theSuit, int theRank)
{
suit = theSuit;
rank = theRank;
}
//Print the card in a human-readable form:
public void printCard()
{
String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
String[] ranks = {"narf", "Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King"};
System.out.println(ranks[rank] + " of " + suits[suit]);
}
}
//Represents a standard deck of 52 variables
public class Deck
{
//Instance variable:
Card[] cards;
//Constructor
public Deck()
{
cards = new Card[52];
int index = 0; //Use to assign to the correct index of cards
for (int suit = 0; suit <= 3; suit++)
{
for (int rank = 1; rank <= 13; rank++)
{
cards[index] = new Card(suit, rank);
index++;
}
}
}
public void printDeck()
{
for (int i = 0; i < cards.length; i++)
cards[i].printCard();
}
public void shuffle()
{
int randomIndex;
for (int i = 0; i < 52; i++)
{
randomIndex = (int)(Math.random() * 52);
swapCards (i, randomIndex);
}
}
public void swapCards(int index1, int index2)
{
Card temp = cards[index1];
cards[index1] = cards[index2];
cards[index2] = temp;
}
}
非常感謝!
鏈接已損壞。 – 2014-12-07 02:12:54
嗯,它適用於我。 – yogi 2014-12-07 02:35:56
無法創建一個帳戶,你介意鏈接源代碼嗎? – 2014-12-07 19:40:50