2011-04-14 102 views
1

我一直在瘋狂在這個程序即時通訊寫作。我的甲板似乎不工作,我已經爲每一個信息的追蹤,但8小時後直接它仍然無法工作=(請幫助指出我需要更好地工作或如何去編碼它更好。java撲克牌陣列

package poker; 

public class Deck { 

private Card[] cards; 

// deck constructor with initial array 
public Deck() { 
    Card[] x= new Card[52]; 
    int index = 0; 
    for (int suit = 0; suit < 3; suit++) { 
     for (int value = 1; value < 13; value++) { 
      cards[index] = new Card(value, suit); 
      index++; 

     } 

    } 
} 

// copy constructor with a shallow copy of the array 
public Deck(Deck other) { 
    Card[] c = new Card[52]; 

    int index = 0; 
    for (int suit = 0; suit <= 3; suit++) { 
     for (int value = 1; value <= 13; value++) { 
      cards[index] = new Card(suit, value); 
      index++; 
     } 

    } 
} 

// method for cards in any position 
public Card getCardAt(int position) { 
    if (position >= cards.length) { 
     throw new IndexOutOfBoundsException("Values are out of bounds"); 
    } else { 
     return cards[position]; 
    } 
} 

// number of cards left after each draw 
public int getNumCards() { 

    return cards.length; 
} 

// Randomized rearrangement of cards 





//have no idea to go about this any further 
public void shuffle() { 
    int temp=0; 
    for (int row=0;row<cards.length;row++){ 
     int random = (int)(Math.random()*((cards.length-row)+1)); 
    Deck.this.cards[temp]= this.getCardAt(row); 
    cards[row]=cards[random]; 
    cards[random]=cards[temp]; 
    } 
} 
    //cutting of the cards 
public void cut(int position) { 
    //int temp = this.cards 
} 

// something to think about on dealing from taking the differences in 
// dealing the cards 
public Card[] deal(int numCards) { 
    /* numCards = 5; 
    for (int i = 0; i < numCards; i++) { 

     numCards = cards.length - numCards; 
    } 
    return deal(numCards);*/ 
    { 
      numCards = this.getNumCards(); 
      numCards ++; 

      return deal(5); 
     } 

} 

} 

我試圖JUnit測試,但我似乎在他們吮吸,但我想

package poker; 

import junit.framework.TestCase; 

public class StudentTests extends TestCase { 
public void testDeck() { 
    int value=0; 
    int suit = 0; 
    Card[] x = new Card[52]; 
    //Card = new Card(getValue(), getSuit()); 

    assertTrue(getValue() == 1 && getSuit() == value); 
    assertTrue(getValue() == 1 && getSuit() == suit); 
    ; 
} 
public void testCopyConstructor(){ 

     int value = 0; 
     int suit = 0; 
     Card[] sc = new Card[52]; 
     //Card = new Card(getValue(), getSuit()); 

     assertTrue(getValue() == 1 && getSuit() == 0); 

} 
/* public void testShuffle() 
    { 
    Card[] sc = new Card[52]; 
    Card[] sc2 = new Card[52]; 
     assertTrue(sc==(sc2)); 
     //shuffle method 
     assertFalse(sc==(sc2)); 

     //another shuffle method here 
     //i have no idea 

     assertFalse(sc==(sc2));} */ 

private int getValue() { 
    // TODO Auto-generated method stub 
    return 1; 
} 

private int getSuit() { 
    // TODO Auto-generated method stub 
    return 0; 
} 
} 
+2

我感覺你,但這個問題可能會被關閉,除非你添加一些更多的細節。究竟是什麼問題?如果我們甚至不知道它應該做什麼,「套牌似乎不工作」並不是很有幫助。你知道問題出在哪裏嗎?你是否收到特定的錯誤信息? – Pops 2011-04-14 19:06:19

回答

0

在您的Deck構造函數中,初始化您的cards成員數組如下:

public Deck() 
{ 
    cards = new Card[52]; 

    int index = 0; 

    for (int suit = 0; suit <= 3; suit++) 
    { 
     for (int value = 1; value <= 13; value++) 
     { 
     cards[index] = new Card(value, suit); 
     index++; 
     } 
    } 
} 

此外,您Deck拷貝構造函數並不做任何實際的複製。

1

橋面你的默認構造函數使得36卡,而不是52開始甲板通過固定這一點。

+0

我數36.('價值'從1開始。) – Pops 2011-04-14 19:23:04

+0

我固定了循環卡=新卡[52]; \t \t int index = 0; \t \t對(INT西裝= 0;花色<= 3;西裝++){ \t \t \t爲(int值= 1;值<= 13;值++){ \t \t \t \t卡[索引] =新卡(價值,套裝); \t \t \t \t index ++; – Cferrel 2011-04-15 02:25:56

0

從默認的構造函數:

cards[index] = new Card(value, suit); 

從你的拷貝構造函數:

cards[index] = new Card(suit, value); 

在Java中訂購事宜;你不能指望編譯器知道訴訟的含義以及變量名稱的含義。

另外,在deal(int numCards)裏面,你可以撥打deal(5)。這將繼續不斷地呼喚自己永遠直到電腦內存不足。 (這被稱爲遞歸,正確使用非常困難,您根本不需要使用它。)

這是除了其他答覆者提出的有效答案外。