2015-11-20 36 views
0

我一直停留在这了一段时间,现在的目标是创建一个数组来保存的牌,一旦我做创建数组打印出来,也就是打印是唯一的卡心中之王。为什么它没有迭代?创建一副牌和洗牌他们

public class Card 
{ 
    // Card suits (provided for your convenience - use is optional) 
    public static final int SPADES = 0; 
    public static final int HEARTS = 1; 
    public static final int CLUBS = 2; 
    public static final int DIAMONDS = 3; 

    // Card faces (provided for your convenience - use is optional) 
    public static final int ACE  = 1; 
    public static final int TWO  = 2; 
    public static final int THREE = 3; 
    public static final int FOUR  = 4; 
    public static final int FIVE  = 5; 
    public static final int SIX  = 6; 
    public static final int SEVEN = 7; 
    public static final int EIGHT = 8; 
    public static final int NINE  = 9; 
    public static final int TEN  = 10; 
    public static final int JACK  = 11; 
    public static final int QUEEN = 12; 
    public static final int KING  = 13; 




    // define fields here 
    private static int suit; 
    private static int val; 
    private String[] suits = {"Clubs", "Spades", "Diamonds", "Hearts"}; 
    private String[] vals = {"Ace","2", "3", "4", "5", "6", "7", 
      "8", "9", "10", "Jack", "Queen", "King" }; 

    // This constructor builds a card with the given suit and face, turned face down. 
    public Card(int suit, int val) 
    { 

     this.val = val; 
     this.suit = suit; 

    } 

    public @Override String toString() 
    { 
     return vals[val] + " Of " + suits[suit]; 
    } 

    // This method retrieves the suit (spades, hearts, etc.) of this card. 
    public int getSuit() 
    { 

     return this.suit; 

    } 

    // This method retrieves the face (ace through king) of this card. 
    public int getFace() 
    { 
//  return this.val; 

     switch(val) 
     { 
     case 0 : return 1; 
     case 1 : return 2; 
     case 2 : return 3; 
     case 3 : return 4; 
     case 5 : return 6; 
     case 6 : return 7; 
     case 7 : return 8; 
     case 8 : return 9; 
     case 9 : return 10; 
     case 10 : return 10; 
     case 12 : return 10; 
     default : return 0; 
     } 
    } 








}` 


import java.util.ArrayList; 
import java.util.Random; 
// This class represents the deck of cards from which cards are dealt to players. 
public class Deck 
{ 
    //array 
    public static Card[] cards; 
// private static ArrayList<Card> cards; 

    int i; 
    int counter; 


    // This constructor builds a deck of 52 cards. 
    Deck() 
    { 
     i = 51; 
     //array implementation 
     cards = new Card[52]; 
     int x = 0; 


     for(int i = 0; i < 4; i++) 
     { 
      for(int j = 0; j < 13;j++) 
      { 

       //Array implementation 
       cards[x] = new Card(i,j); 
       x++; 
      } 
     } 


    } 




    // This method takes the top card off the deck and returns it. 
    public Card deal() 
    { 

     //Array implementation 
     int index = 0; 
     Card temp = cards[index]; 

     return temp; 

    } 


    // this method returns true if there are no more cards to deal, false otherwise 
    public boolean isEmpty() 
    { 
     if(cards.length == 0) 
     { 
      return true; 
     } 
     return false; 
    } 

    //this method puts the deck int some random order 
// public void shuffle() 
// { 
//  for(int i = 51; i > 0; i--) 
//  { 
//   int rand = (int) (Math.random() * (i+1)); 
//   Card temp = this.cards[i]; 
//   this.cards[i] = cards[rand]; 
//   cards[rand] = temp; 
//  } 
// 
// } 


} 


public class BlackJack extends Deck 
{ 
    public static void main(String []args) 
    { 
     System.out.println("Ready to play a game of BlackJack?"); 

     Deck deck = new Deck(); 
     for(int i = 0; i < cards.length;i++) 
     { 
      System.out.println(cards[i]); 
     } 
+0

甲骨文的Java教程有卡例子的甲板上,这将是对你有用。 https://docs.oracle.com/javase/tutorial/java/javaOO/QandE/creating-answers.html –

回答

5

问题是由以下原因造成的:

// define fields here 
private static int suit; 
private static int val; 

,因为它们是静态的,所有的卡实例共享这些,所以他们都将有它们被设定的最后一个值。去除静电。

+0

谢谢你,我不能相信我没有注意到,哈哈。 –

1

为了给你一个小提示你怎么可能使代码更容易阅读/维护:

  • 使用枚举,而不是常量。你可以给他们构造函数/成员变量就像类
  • 使用的for-each-循环,它的方式更容易阅读,并防止常见的off-by-1的问题
  • 类型的“静态”前的所有其他改性剂和保持所有静态变量和成员与任何非静态内容分离。这样,你永远无法通过事故混淆了这个代码的

坏/不寻常的侧面:

  • 我使用静态嵌套的枚举和类。通常每个那些应该进入自己的文件,为非静态
  • 我喜欢根据自己的范围前缀我的变量,所以我不能乱举起手来:PXXX为参数,SXXX静态变量,mXxxx会员,没有前缀为方法变量

 
public class CardGame {

static public enum SuitType { SPADES, HEARTS, CLUBS, DIAMONDS } static public enum FaceType { ACE("Ace", 1), // TWO("2", 2), // THREE("3", 3), // FOUR("4", 4), // FIVE("5", 5), // SIX("6", 6), // SEVEN("7", 7), // EIGHT("8", 8), // NINE("9", 9), // TEN("10", 10), // JACK("Jack", 11), // QUEEN("Queen", 12), // KING("King", 13), // ; private final String mName; private final int mValue; private FaceType(final String pName, final int pValue) { mName = pName; mValue = pValue; } public String getName() { return mName; } public int getValue() { return mValue; } public int getValue_alternative() { return ordinal() + 1; // ordinal is the index of the enum, starting at ace=0 ... king=12 // sorting and comparing of suits could also be done by .ordinal() if need be } } static public class Card { private final SuitType mSuit; private final FaceType mFace; public Card(final SuitType pSuit, final FaceType pFace) { mSuit = pSuit; mFace = pFace; } public SuitType getSuit() { return mSuit; } public FaceType getFace() { return mFace; } @Override public String toString() { return mFace.getName() + " of " + mSuit + " - " + mFace.getValue() + (mFace.getValue() > 1 ? " points" : " point"); } } static public class Deck { private final ArrayList<Card> mCards; public Deck() { mCards = createFullDeck(); } private ArrayList<Card> createFullDeck() { // can also be static as it dowes not use member cariables => potential factory method final ArrayList<Card> ret = new ArrayList<>(); for (final SuitType suit : SuitType.values()) { for (final FaceType face : FaceType.values()) { final Card c = new Card(suit, face); ret.add(c); } } return ret; } public void shuffleRandomly() { final ArrayList<Card> tempList = new ArrayList<>(); tempList.addAll(mCards); mCards.clear(); while (tempList.size() > 0) { final int index = (int) (Math.random() * tempList.size()); final Card card = tempList.remove(index); mCards.add(card); } } public void print() { System.out.println(" - - - DECK BEGINS - - - "); int counter = 0; for (final Card c : mCards) { System.out.println("\t" + c); ++counter; } System.out.println("> Printed " + counter + " cards."); System.out.println(" - - - DECK ENDS - - - "); } } public static void main(final String[] args) { final Deck d = new Deck(); d.print(); d.shuffleRandomly(); d.print(); } }