Results 1 to 7 of 7
Thread: Tryin to create a deck of cards
- 08-03-2011, 04:23 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
Tryin to create a deck of cards
Hey!
Im tryin to create a deck of cards and im having a bit of trouble.
I have a card class with a string to represent the suit and an int for the value of the card.
I have setters and getters for both.
Then I have a deck class with a card array to hold the deck.
Im having trouble generating the deck of cards tho.
my code for generating the desk is;
My problem is that the card value is not increasing. Its just stayin the same no matter what i seem to do. Any advice would be great!!Java Code:public card[] generateDeck(card c){ c=new card(); int value=2; //value of first card is two. Up to 15 for one suit for(int j=0;j<12;j++){ //13 cards in a suit c.setSuit("hearts"); c.setValue(value); deck[j]=c; value++; } }
Thanks
- 08-03-2011, 04:45 PM #2
Well, when are you increasing the integer that value stores?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 08-03-2011, 04:51 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
- 08-03-2011, 05:05 PM #4
Oi sorry, I don't know how I missed that. It'd be easier to help you if you provided an SSCCE that demonstrated the problem, as that piece of code looks pretty kosher by itself.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 08-03-2011, 05:16 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
ok here goes. 3 classes. deck, card and test(main).
card classJava Code:public class deck { card[] deck = new card[52]; public card[] genDeck(card c){ c=new card(); String suit="hearts"; int value=2; for(int j=0;j<=12;j++){ c.setSuit(suit); c.setValue(value); deck[j]=c; value++; } return deck; } }
and finally the testJava Code:public class card { private String suit; private int value; public void setSuit(String s){ suit=s; } public void setValue(int v){ value=v; } public String getSuit(){ return suit; } public int getValue(){ return value; } }
Java Code:public class test { public static void main(String[]args){ card c = new card(); deck newDeck = new deck(); card[] list=newDeck.genDeck(c); for(int j=0;j<list.length;j++){ System.out.println(list[j].getSuit()+list[j].getValue()); } } }
this is the output. The nullpointer I can handle. Its the number im having trouble with
hearts14
hearts14
hearts14
hearts14
hearts14
hearts14
hearts14
hearts14
hearts14
hearts14
hearts14
hearts14
hearts14
Exception in thread "main" java.lang.NullPointerException
at test.main(test.java:10)
- 08-03-2011, 05:27 PM #6
Here is more of an SSCCE:
In any case, your problem is that you only ever have one instance of Card (notice how class names start with an upper-case letter). Each time through the loop, you're updating its value and then adding it to the array. But it's the SAME instance of Card, so you're updating EVERY Card that's been added to the array.Java Code:public class Test { public static class Deck { Card[] deck = new Card[52]; public Card[] genDeck(Card c){ c = new Card(); String suit="hearts"; int value=2; for(int j=0;j<=12;j++){ c.setSuit(suit); c.setValue(value); deck[j]=c; value++; } return deck; } } public static class Card { private String suit; private int value; public void setSuit(String s){ suit=s; } public void setValue(int v){ System.out.println(v); value=v; } public String getSuit(){ return suit; } public int getValue(){ System.out.println("r: " + value); return value; } } public static void main(String[]args){ Card c = new Card(); Deck newDeck = new Deck(); Card[] list=newDeck.genDeck(c); for(int j=0; j<list.length; j++){ System.out.println(list[j].getSuit()+list[j].getValue()); } } }
One solution would be to create a new Card each iteration of the loop and add that to the array instead.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 08-03-2011, 05:32 PM #7
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
Similar Threads
-
Help with creating a deck of cards
By Carouselification in forum New To JavaReplies: 5Last Post: 03-06-2011, 04:45 AM -
Deck of cards problem
By VelvetMirror in forum New To JavaReplies: 2Last Post: 02-16-2011, 08:02 PM -
Poker Simulation (shuffling a deck of cards?)
By StateofMind in forum New To JavaReplies: 4Last Post: 04-06-2010, 08:59 PM -
Deck of Cards
By khunmato in forum New To JavaReplies: 13Last Post: 09-06-2009, 05:47 PM -
Problem with making/sorting a deck of cards
By Franneldort in forum New To JavaReplies: 9Last Post: 11-07-2008, 12:47 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks