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;
}
}
Your default constructor for Deck makes a deck of 36 cards, not 52. Start by fixing that.
In your Deck
constructor, initialize your cards
member array as follows:
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++;
}
}
}
Also, your Deck
copy constructor doesn't do any actual copying.
From your default constructor:
cards[index] = new Card(value, suit);
From your copy constructor:
cards[index] = new Card(suit, value);
Order matters in Java; you can't expect the compiler to know what the suit means and what the value means just by variable names.
Also, inside deal(int numCards)
, you call deal(5)
. That's going to keep calling itself over and over again forever until the computer runs out of memory. (This is called recursion, and it's very tricky to use correctly. You shouldn't need to use it at all.)
That's in addition to the valid points made by other answerers.
精彩评论