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)。这将继续不断地呼唤自己永远直到电脑内存不足。 (这被称为递归,正确使用非常困难,您根本不需要使用它。)

这是除了其他答复者提出的有效答案外。