I have been going crazy on this program im writing. My decks dont seem to work and I have hunted for every tidbit of information but after 8 hours straight it still does not work =( Please help on pointing out where i need to work on better or how to go about coding it better.
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);
}
}
}
I tried to Junit test but i seem to suck at them but i tried
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;
}
}
In your
Deckconstructor, initialize yourcardsmember array as follows:Also, your
Deckcopy constructor doesn’t do any actual copying.