Java扑克卡阵列

问题描述

| 我一直在写这个程序疯了。我的套牌似乎不起作用,我一直在寻找每一个信息,但连续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;
}
}
    

解决方法

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++;
      }
   }
}
另外,您的“ 2”副本构造函数不会进行任何实际的复制。     ,Deck的默认构造函数可以存储36张牌,而不是52张。     ,从默认构造函数中:
cards[index] = new Card(value,suit);
从您的副本构造函数:
cards[index] = new Card(suit,value);
Java中的订单事项;您不能指望编译器仅通过变量名就知道该西装的含义和值的含义。 另外,在
deal(int numCards)
中,您称
deal(5)
。在计算机内存耗尽之前,这将不断地反复调用自己。 (这称为递归,正确使用非常棘手。您完全不需要使用它。) 这是其他答复者提出的有效观点的补充。     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...