2012-02-06 304 views
0

我正在Java中做一个快速纸牌游戏项目。我将这副牌作为节点列表存储。我认为我无法正确添加和从列表中删除。你看到任何看起来不正确的东西吗?我一直在想办法解决这个问题。 编辑:发布的所有类给你看Java列表和垃圾回收器

卡类

public class Card 
{ 
    private CardType theCard; 
    private CardSuit theSuit; 
    private Card nextCard = null, previousCard = null; 
    Card(Card card) 
    { 
     /* 
     * Question - If I just set "this = c", would this 
     * object be pointing to the same object c? Or will 
     * a separate object be created from Card c 
     */ 
     this.theCard = card.getTheCard(); 
     this.theSuit = card.getTheSuit(); 
     this.nextCard = card.getNext(); 
     this.previousCard = card.getPrevious(); 
    } 
    Card(CardType theCard, CardSuit theSuit) 
    { 
     this.theCard = theCard; 
     this.theSuit = theSuit; 
    } 
    Card(CardType theCard, CardSuit theSuit, Card nextCard, Card previousCard) 
    { 
     this.theCard = theCard; 
     this.theSuit = theSuit; 
     this.nextCard = nextCard; 
     this.previousCard = previousCard; 
    } 
    //the suit order is spade, heart, diamond, and then club. 
    public int getTotValue() 
    { 
     return theCard.getValue() + theCard.getFaceValue() + theSuit.getValue(); 
    } 
    public int getFaceValue() 
    { 
     return theCard.getFaceValue(); 
    } 
    public int getSuitValue() 
    { 
     return theSuit.getValue(); 
    } 
    public String getFace() 
    { 
     return theSuit.getFace(); 
    } 

    public String getSuit() 
    { 
     return theSuit.getFace(); 
    } 
    public int getValue() 
    { 
     return theCard.getValue(); 
    } 
    public Card getNext() 
    { 
     return nextCard; 
    } 
    public void setNext(Card nextCard) 
    { 
     this.nextCard = nextCard; 
    } 
    public Card getPrevious() 
    { 
     return previousCard; 
    } 
    public void setPrevious(Card previousCard) 
    { 
     this.previousCard = previousCard; 
    } 
    /** 
    * @return the theCard 
    */ 
    public CardType getTheCard() { 
     return theCard; 
    } 
    /** 
    * @param theCard the theCard to set 
    */ 
    public void setTheCard(CardType theCard) { 
     this.theCard = theCard; 
    } 
    /** 
    * @return the theSuit 
    */ 
    public CardSuit getTheSuit() { 
     return theSuit; 
    } 
    /** 
    * @param theSuit the theSuit to set 
    */ 
    public void setTheSuit(CardSuit theSuit) { 
     this.theSuit = theSuit; 
    } 
    public String toString() 
    { 
     return (theCard.getFace() + " of " + theSuit.getFace()); 
    } 
} 

CardSuit

public enum CardSuit 
{ 
    SPADE("Spade", 4), 
    HEART("Heart", 3), 
    DIAMOND("Diamond", 2), 
    CLUB("Club", 1); 
    private final String face; 
    private final int value; 
    CardSuit(String face, int value) 
    { 
     this.face = face; 
     this.value = value; 
    } 
    public String getFace() 
    { 
     return face; 
    } 
    public int getValue() 
    { 
     return value; 
    } 
} 

CardType

public enum CardType 
    { 
      ACE("Ace", 11), 
      KING("King", 3, 10), 
      QUEEN("Queen", 2, 10), 
      JACK("Jack", 1, 10), 
      TEN("Ten", 10), 
      NINE("Nine", 9), 
      EIGHT("Eight", 8), 
      SEVEN("Seven", 7), 
      SIX("Six", 6), 
      FIVE("Five", 5), 
      FOUR("Four", 4), 
      THREE("Three", 3), 
      DEUCE("Deuce", 2); 
      private final String face; 
      private final int value; 
      private int faceValue = 0; 
      CardType(String f, int v) 
      { 
       this.face = f; 
       this.value = v; 
      } 
      CardType(String f, int fv, int v) 
      { 
       this.face = f; 
       this.faceValue = fv; 
       this.value = v; 
      } 
      public String getFace() 
      { 
       return face; 
      } 
      public int getValue() 
      { 
       return value; 
      } 
      public int getFaceValue() 
      { 
       return faceValue; 
      } 

    } 

甲板

/** 
* 
*/ 

/** 
* @author Andrew-Desktop 
* 
*/ 
public class Deck extends Pile 
{ 
    /** 
    * the suit order is spade, heart, diamond, and then club. 
    */ 
    public Deck() 
    { 
     for(CardType card: CardType.values()) 
     { 
      for(CardSuit suit: CardSuit.values()) 
      { 
       this.addLastCard(new Card(card, suit)); 
      } 
     } 
    } 

    /** 
    * @param topCard 
    * @param bottomCard 
    */ 
    public Deck(Card topCard, Card bottomCard) 
    { 
     super(topCard, bottomCard); 
     // TODO Auto-generated constructor stub 
    } 

    /** 
    * @param topCard 
    * @param bottomCard 
    * @param nCard 
    */ 
    public Deck(Card topCard, Card bottomCard, int nCard) 
    { 
     super(topCard, bottomCard, nCard); 
     // TODO Auto-generated constructor stub 
    } 
} 

import java.util.Random; 



public class Pile 
{ 
    private static final int GREATER_THAN = 1; 
    private static final int EQUAL_TO = 0; 
    private static final int LESS_THAN = -1; 
    Card topCard, bottomCard; 
    int nCard; 
    Random ran = new Random(); 
    int ranNum; 
    Pile() 
    { 
     topCard = null; 
     bottomCard = null; 
     nCard = 0; 
    } 
    Pile(Card topCard, Card bottomCard) 
    { 
     this.topCard = topCard; 
     this.bottomCard = bottomCard; 
     this.nCard = 52; 
    } 
    Pile(Card topCard, Card bottomCard, int nCard) 
    { 
     this.topCard = topCard; 
     this.bottomCard = bottomCard; 
     this.nCard = nCard; 
    } 
    public void 
    shuffle() throws InterruptedException 
    { 
     for(int i = ran.nextInt(10000);0<i;i--) 
     { 
      Card tempCard = remove(1); 
      this.insert(tempCard, (ran.nextInt(52)+1)); 
     } 
    } 
    public boolean 
    thereIsDuplicates() 
    { 
     Card travCard = topCard; 
     for(int x = 1; x<=nCard && travCard != null; x++) 
     { 
      for(int y = x+1; y<=nCard; y++) 
      { 
       if(travCard == this.getCardAtIndex(y)) 
       { 
//     System.out.println(this.getIndexOfCard(travCard) + ": " + travCard); 
        System.out.println(travCard.toString()); 
        return true; 
       } 
      } 
      travCard = travCard.getNext(); 
     } 
     return false; 
    } 
    public 
    Card remove(Card c) 
    { 
     assert !isEmpty();//Don't know if this even works 
     if(c == topCard)// if topCard 
     { 
      topCard = topCard.getNext(); 
      topCard.setPrevious(null); 
     } 
     else if(c == bottomCard) // if bottom card 
     { 
      bottomCard = bottomCard.getPrevious(); 
      bottomCard.setNext(null); 
     } 
     else 
     { 
      Card tempCard = c.getPrevious(); 
      tempCard.setNext(c.getNext()); 
      tempCard.getNext().setPrevious(tempCard); 
     } 
     nCard--; 
     return null; 
    } 
// public void 
// remove(int i) 
// { 
//  assert (i>0 && i <= nCard && !isEmpty()); 
//  if(i == 1)// if topCard 
//  { 
//   topCard = topCard.getNext(); 
//   topCard.setPrevious(null); 
//  } 
//  else if(this.getCardAtIndex(i).getNext()==null) // if bottom card 
//  { 
//   bottomCard = bottomCard.getPrevious(); 
//   bottomCard.setNext(null); 
//  } 
//  else 
//  { 
//   Card cardBefore = this.getCardAtIndex(i-1); 
//   cardBefore.setNext(cardBefore.getNext().getNext()); 
//   cardBefore.getNext().setPrevious(cardBefore); 
//  } 
//  nCard--; 
//  
// } 
    public Card remove(int givenPosition) throws InterruptedException 
    { 
     Card result = null; // return value 

     if ((givenPosition >= 1) && (givenPosition <= nCard)) 
     { 

      if (givenPosition == 1) // case 1: remove first entry 
      { 
       result = topCard; // save entry to be removed 
       topCard = topCard.getNext(); 
       topCard.setPrevious(null); 
      } 
      else // case 2: givenPosition > 1 
      { 
       Card cardBefore = getCardAtIndex(givenPosition - 1); 
       Card cardToRemove = cardBefore.getNext(); 
       Card cardAfter = cardToRemove.getNext(); 
       cardBefore.setNext(cardAfter); // disconnect the node to be removed 
       cardAfter.setPrevious(cardBefore); 
       result = cardToRemove; // save entry to be removed 
      } // end if 

      nCard--; 
     } // end if 
     if(result == null) 
     { 
      this.printCards(); 
      Thread.sleep(1000); 
     } 

     return result; 
    } 

    /** 
    * <p>Precondition: index must be 0<i and i<53 or less than the number of cards 
    * <p>Postcondition: 
    * @param i - The index of the card. 
    * @return Card at that index. 
    * 
    */ 
    public Card getCardAtIndex(int i) 
    { 
     Card travCard = topCard; 
     assert (i>0 && i<=nCard && !isEmpty()); 
     for(int x = 1; x<=i && travCard != null; x++) 
     { 
      travCard = travCard.getNext(); 
     }  
     return travCard; 
    } 
    public int 
    getIndexOfCard(Card c, int i) 
    { 
     Card travCard = topCard; 
     for(int x = i; x<=nCard; x++) 
     { 
      if(travCard == c) 
       return x; 
      travCard = travCard.getNext(); 
     } 
     return -1; 
    } 
    public Card 
    getCard(Card c)//don't think I'll need this method 
    { 
     Card travCard = topCard; 
     assert (!isEmpty()); 
     while(c!=travCard && null != travCard) 
     { 
      travCard = travCard.getNext(); 
     }  
     return travCard; 
    } 
    /** 
    * Sorts from highest(Ace) to lowest 2 
    * the suit order is spade, heart, diamond, and then club. 
    */ 
    public void 
    addLastCard(Card c) 
    { 
     if(isEmpty()) 
     { 
      topCard = c; 
      bottomCard = c; 
     } 
     else 
     { 
      bottomCard.setNext(c); 
      c.setPrevious(bottomCard); 
      bottomCard = c; 
     } 
     nCard++; 
    } 
    public void 
    sort() 
    { 
     quickSort(topCard, bottomCard); 
    } 
    private void 
    quickSort(Card start, Card end) 
    { 
      Card left = start;       
      Card right = end;       

      if (start != end)     
      { 
        Card pivot = start;  
        while (!(left.getNext()!=right))     
        { 
         while (compare(left, pivot) == LESS_THAN && left != end && left!=right) 
         { 
           left = left.getNext();  
         } 
         while (compare(right, pivot) == GREATER_THAN && right!=start && right!=left) 
         { 
          right = right.getPrevious();   
         } 
         if (left!=right) 
         { 
          swap(left, right);  
         } 
        } 
        swap(start, right);   
        quickSort(start, right.getPrevious()); 
        quickSort(right.getNext(), end); 
      } 
      else // if there is only one element in the partition, do not do any sorting 
      { 
        return;      // the array is sorted, so exit 
      } 
    } 
    public void 
    swap(Card one, Card two) 
    { 
     Card temp = new Card(one); 
     one = two; 
     two = temp; 
    } 
    public void 
    insert(Card theCard, int givenPosition) 
    { 
     if(givenPosition>0 && givenPosition<=nCard) 
     { 
      if(isEmpty())// if an empty list 
      { 
       topCard = theCard; 
       bottomCard = theCard; 
       System.out.println("EmptyList"); 
      } 
      else if(1==givenPosition)// if adding to the top of the pile 
      { 
       theCard.setNext(topCard); 
       topCard.setPrevious(theCard); 
       topCard = theCard; 
      } 
      else if(nCard == givenPosition) // if adding to the bottom of the pile 
      { 
       this.addLastCard(theCard); 
       nCard--; 
      } 
      else 
      { 
       Card tempCard = getCardAtIndex(givenPosition); 
       theCard.setNext(tempCard.getNext()); 
       tempCard.setNext(theCard); 
       theCard.setPrevious(tempCard); 
      } 
      nCard++; 
     } 
    } 
    //the suit order is spade, heart, diamond, and then club. 
    public int 
    compare(Card one, Card two) 
    { 
     if(one.getValue()<two.getValue() || (one.getValue() == two.getValue() && one.getTotValue()<two.getTotValue())) 
     { 
      return LESS_THAN; 
     } 
     else if(one.getValue() == two.getValue() && one.getTotValue() == two.getTotValue()) 
     { 
      return EQUAL_TO; 
     } 
     else if(one.getValue()>two.getValue() || (one.getValue() == two.getValue() && one.getTotValue()>two.getTotValue())) 
     { 
      return GREATER_THAN; 
     } 
     return -5; 
    } 
    public boolean 
    isEmpty() 
    { 
     if(0 == nCard && null == topCard) 
      return true; 
     else 
      return false; 
    } 
    public void 
    printCards() 
    { 
     Card travCard = topCard; 
     int i = 1; 
     while(travCard!=null) 
     { 
      System.out.println(i + ": " + travCard.toString()); 
      travCard = travCard.getNext(); 
      i++; 
     } 
    } 
    /** 
    * @return the topCard 
    */ 
    public Card 
    getTopCard() 
    { 
     return topCard; 
    } 
    /** 
    * @param topCard the topCard to set 
    */ 
    public void 
    setTopCard(Card topCard) 
    { 
     this.topCard = topCard; 
    } 
    /** 
    * @return the bottomCard 
    */ 
    public Card 
    getBottomCard() 
    { 
     return bottomCard; 
    } 
    /** 
    * @param bottomCard the bottomCard to set 
    */ 
    public void 
    setBottomCard(Card bottomCard) 
    { 
     this.bottomCard = bottomCard; 
    } 
    /** 
    * @return the nCard 
    */ 
    public int 
    getnCard() 
    { 
     return nCard; 
    } 
    /** 
    * @param nCard the nCard to set 
    */ 
    public void 
    setnCard(int nCard) 
    { 
     this.nCard = nCard; 
    } 
} 

TwentyOne

public class TwentyOne 
{ 

    /** 
    * @param args 
    * @throws InterruptedException 
    */ 
    public static void main(String[] args) throws InterruptedException 
    { 
     Deck theDeck = new Deck(); 
     theDeck.printCards(); 
     theDeck.shuffle(); 
     theDeck.printCards(); 
    } 

} 

输出看起来是这样的:

577625: Ace of Spade 
577626: Nine of Spade 
577627: Five of Diamond 
577628: Ten of Heart 
577629: Eight of Heart 
577630: Nine of Club 
577631: Jack of Heart 
577632: Eight of Spade 
577633: Queen of Heart 
577634: Seven of Heart 
577635: Deuce of Club 
577636: Jack of Diamond 
577637: Four of Club 
577638: Five of Club 
577639: Ace of Spade 
577640: Nine of Spade 
577641: Five of Diamond 
577642: Ten of Heart 
577643: Eight of Heart 
577644: Nine of Club 
577645: Jack of Heart 
577646: Eight of Spade 
577647: Queen of Heart 
577648: Seven of Heart 
577649: Deuce of Club 
577650: Jack of Diamond 
577651: Four of Club 
577652: Five of Club 
577653: Ace of Spade 
577654: Nine of Spade 
577655: Five of Diamond 
577656: Ten of Heart 
577657: Eight of Heart 
577658: Nine of Club 
577659: Jack of Heart 
577660: Eight of Spade 
577661: Queen of Heart 
577662: Seven of Heart 
577663: Deuce of Club 
577664: Jack of Diamond 
577665: Four of Club 
577666: Five of Club 
577667: Ace of Spade 
577668: Nine of Spade 
577669: Five of Diamond 
577670: Ten of Heart 
577671: Eight of Heart 
577672: Nine of Club 
577673: Jack of Heart 
577674: Eight of Spade 
577675: Queen of Heart 
577676: Seven of Heart 
577677: Deuce of Club 
577678: Jack of Diamond 
577679: Four of Club 
577680: Five of Club 
577681: Ace of Spade 
577682: Nine of Spade 
577683: Five of Diamond 
577684: Ten of Heart 
577685: Eight of Heart 
577686: Nine of Club 
577687: Jack of Heart 
577688: Eight of Spade 
577689: Queen of Heart 
577690: Seven of Heart 
577691: Deuce of Club 
577692: Jack of Diamond 
577693: Four of Club 
577694: Five of Club 
577695: Ace of Spade 
577696: Nine of Spade 
577697: Five of Diamond 
+4

为什么不使用标准的java.util.List和标准的java.util.Collections.shuffle()?你正在重新发明轮子。 – 2012-02-06 20:44:48

+1

这个问题与GC有什么关系?这对于链表来说是一个相当复杂的代码。 – Voo 2012-02-06 21:11:56

+0

如果我们不必猜测你的各种类和方法的实现,这将有所帮助。 – 2012-02-06 21:21:36

回答

1

使用的java.util.List的一个子类。无论是ArrayList(可能是你应该选择的)或LinkedList(如果你感觉像你这样的青蛙需要实现队列的东西)。然后看看使用Collections.shuffle()方法来洗牌。当涉及到随机存取

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#shuffle(java.util.List

LinkedList的将不执行,以及ArrayList的。

+0

我必须自己实现列表类,因为这是一项任务。 – user664509 2012-02-06 20:59:40

0

看着这样的输出,很难弄清楚发生了什么。心中的女王出现多次时有些不妥。尝试测试您的插入方法,而无需调用shuffle并删除。一次测试一种方法有助于隔离问题。

当您看到您能够按顺序插入所有卡时,请参阅删除单张卡时会发生的情况。