我在写这个节目时已经疯了。我的套牌似乎没有工作,我已经找到了每一小窍门的信息,但是经过8个小时的直播它仍然无效=(请帮助指出我需要更好地工作的地方或如何更好地编码。
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;
}
}
答案 0 :(得分:1)
Deck的默认构造函数是36张牌,而不是52张。首先修复它。
答案 1 :(得分: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
复制构造函数不会进行任何实际复制。
答案 2 :(得分:0)
来自默认构造函数:
cards[index] = new Card(value, suit);
从你的拷贝构造函数:
cards[index] = new Card(suit, value);
订单在Java中很重要;你不能指望编译器知道诉讼意味着什么以及价值意味着变量名称。
此外,在deal(int numCards)
内,您拨打deal(5)
。这将继续一遍又一遍地调用 forever ,直到计算机内存不足为止。 (这称为递归,使用正确非常棘手。根本不需要使用它。)
除了其他回答者提出的有效观点之外,还有。