2014-12-07 54 views
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; 
    } 
} 

非常感谢!

回答

0

此网站http://jcode.tripod.com/Games.html有各种纸牌游戏的java源代码。学习和理解它们可能会帮助你完成任务。

/编辑 链接断开。可以在这里找到Java游戏的二十一点游戏来源http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3562&lngWId=2

+0

链接已损坏。 – 2014-12-07 02:12:54

+0

嗯,它适用于我。 – yogi 2014-12-07 02:35:56

+0

无法创建一个帐户,你介意链接源代码吗? – 2014-12-07 19:40:50