编写了一个甲板和卡类的代码,你可以在下面找到,但我不确定如何使用它们来帮助我创建一个有效的二十一点游戏。
我当然并没有要求你为我做这项工作,也没有告诉我所有的答案,只是为了让我基本了解如何使用基本的初学者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;
}
}
非常感谢你!
答案 0 :(得分:0)
此网站(http://jcode.tripod.com/Games.html)包含各种纸牌游戏的java源代码。学习和理解它们可以帮助你完成任务。
/编辑 链接坏了。可以在http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3562&lngWId=2
找到二十一点游戏的Java源代码