所以我在写这个21点程序,我有麻烦把我的类数组变成链表。我真的不知道该怎么办,如果你能帮助我,我将不胜感激。
这是我的Card类
public class Card
{
/********* Instance Data **************/
private String stringFaceValue; //string face of the card
private String stringSuitValue; //string suit of the card
private int numFaceValue; //integer face of the card
//ace is 1 and Jack-King is 11-13
private int numSuitValue; //integer suit of the card
private int uniqueID; //a unique ID of each card
/********** Contructors **************/
public Card(int inSuit, int inFaceVal)
{
numFaceValue = inFaceVal;
numSuitValue = inSuit;
stringFaceValue = "";
uniqueID = 0;
if(inSuit == 1)
stringSuitValue = "Hearts";
else if(inSuit == 2)
stringSuitValue = "Clubs";
else if(inSuit == 3)
stringSuitValue = "Diamonds";
else
stringSuitValue = "Spades";
}
/************ Methods ***************/
public String toString()
{
//local constants
//local variables
String format; //formatted string to be returned
/********** begin toSTring ***************/
//check which face value to return
switch(numFaceValue)
{
case 1:
stringFaceValue = "Ace";
break;
case 2:
stringFaceValue = "Two";
break;
case 3:
stringFaceValue = "Three";
break;
case 4:
stringFaceValue = "Four";
break;
case 5:
stringFaceValue = "Five";
break;
case 6:
stringFaceValue = "Six";
break;
case 7:
stringFaceValue = "Seven";
break;
case 8:
stringFaceValue = "Eight";
break;
case 9:
stringFaceValue = "Nine";
break;
case 10:
stringFaceValue = "Ten";
break;
case 11:
stringFaceValue = "Jack";
break;
case 12:
stringFaceValue = "Queen";
break;
case 13:
stringFaceValue = "King";
break;
}//end switch
//format the string
format = stringFaceValue + " of " + stringSuitValue;
//return the string
return format;
}
public int getNumber()
{
return numFaceValue;
}
} // end class Card'
My DeckofCards类
import java.lang.*;
import java.text.*;
import java.util.*;
public class DeckofCards
{
private static Card[] cards;//the array of cards
private static int numCards;//the number of cards in the deck
public DeckofCards(boolean shuffle)
{
numCards = 52;
cards = new Card[numCards];
int cardIndex = 0;
//for each suit of cards
for(int suit = 0; suit < 4; suit++)
{
for(int faceVal = 1; faceVal <= 13; faceVal ++)
{
//add a new card to deck
cards[cardIndex] = new Card(suit, faceVal);
cardIndex++;
}
}
//if the user wants to shuff
if(shuffle == true)
{
DeckofCards.shuffle();
}
}//end constructor
public static void shuffle()
{
//initialize random number generator
Random shuffleDeck = new Random();
//temporary card value
Card temp;
int tempIndex;
for(int i = 0; i < numCards; i++)
{
//get a random card to swap
tempIndex = shuffleDeck.nextInt(numCards);
//swap the cards
temp = cards[i];
cards[i] = cards[tempIndex];
cards[tempIndex] = temp;
}
}//end shuffle
public Card dealNextCard()
{
//get the top card
Card top = cards[0];
for(int c = 1; c < numCards; c++)
{
cards[c - 1] = cards[c];
}
cards[numCards - 1] = null;
numCards--;
return top;
}//end dealNextCard
public void printDeck()
{
for(int c = 0; c < numCards; c++)
{
System.out.println(cards[c]);
}
}//end printDeck
}
My player Hand class
public class Hand
{
private Card[] hand = new Card[10];
private int numCards;
public Hand()
{
//set player hand to empty
this.emptyHand();
}//end constructor
public void emptyHand()
{
for(int c = 0; c < 10; c++)
{
hand[c] = null;
}
numCards = 0;
}//end emptyHand
public boolean addCard(Card aCard)
{
hand[numCards] = aCard;
numCards++;
return (this.getHandSum() <= 21);
}//end addCard
public int getHandSum()
{
int handSum = 0;
int numAces = 0;
int cardNum;
//calc the total
for(int c = 0; c < numCards; c++)
{
cardNum = this.hand[c].getNumber();
if(cardNum == 1)
{
numAces++;
handSum+= 11;
}
else if(cardNum > 10)
{
handSum += 10;
}
else
handSum += cardNum;
}//end for calcTotal
//if we have aces that bust the total
while(handSum > 21 && numAces > 0)
{
handSum -= 10;
numAces --;
}
return handSum;
}//end getHandsum
public void printHand()
{
for(int c = 0; c < numCards; c++)
{
System.out.println("t" + hand[c].toString());
}
}//end printHand
}
And my Driver
import java.util.*;
import java.text.*;
import java.lang.*;
public class Game
{
public static void main(String[]args)
{
//local constants
//local variables
boolean turnDone = false; //sees if the user wants to hit
String toPlay;
int sum;
String answer;
DeckofCards theDeck = new DeckofCards(true);
Hand myHand = new Hand();
/************ begin main *****************/
//show startup
System.out.println("nn************************");
System.out.println("* WELCOME TO BLACKJACK *");
System.out.println("************************nn");
//prompt the user if they want to play again
System.out.print("Do you want to play?(Yes or No): ");
toPlay = Keyboard.readString();
while(toPlay.equalsIgnoreCase("yes"))
{
System.out.print("n");
//deal yourself your hand
myHand.addCard(theDeck.dealNextCard());
myHand.addCard(theDeck.dealNextCard());
//print your hand
System.out.println("Your Cards Were Dealtn");
myHand.printHand();
System.out.println("n");
//while I want to get a card
while(!turnDone)
{
System.out.print("Do you want to hit?: ");
answer = Keyboard.readString();
System.out.print("n");
if(answer.equalsIgnoreCase("yes"))
{
//add the next card and see if busto
turnDone = !myHand.addCard(theDeck.dealNextCard());
myHand.printHand();
System.out.print("n");
}
else
{
turnDone = true;
}
}//end while turn isn't done
//display the hand
sum = myHand.getHandSum();
if(sum <= 21)
System.out.println("You win!!n");
else
System.out.println("You busted!!n");
//show startup
System.out.println("nn************************");
System.out.println("* WELCOME TO BLACKJACK *");
System.out.println("************************nn");
//prompt the user if they want to play again
System.out.println("Do you want to play?");
toPlay = Keyboard.readString();
//empty the hand
myHand.emptyHand();
//set the turnboolean to false
turnDone = false;
}//end toPlay
}//end main
}
一切运行和编译良好,它的工作。我只是不知道怎么把它翻译成链表格式
http://blog.xeiam.com/blog/2013/07/28/convert-an-array-into-a-linkedlist-in-java/
从链接:
"要将数组转换为java.util.LinkedList,我们可以使用java.util.Arrays类的asList()方法。类中的Arrays。util包提供了一个将数组转换为列表的实用程序方法。Arrays.asList()方法将数组转换为固定大小的List。要创建一个LinkedList,我们只需要将List传递给java.util.LinkedList类的构造函数。java.util.ArrayList也可以用这种方式创建。"