Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: Tryin to create a deck of cards

  1. #1
    mrbrendano is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default 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;

    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++;
    }
    }
    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!!

    Thanks

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default

    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!

  3. #3
    mrbrendano is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Well, when are you increasing the integer that value stores?
    value++ no??

    I tried another way there but still didnt work. I created an increaceValue method in the card class and called that in the loop but didnt work either!!

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default

    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!

  5. #5
    mrbrendano is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    ok here goes. 3 classes. deck, card and test(main).


    Java 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;
    	}
    	
    }
    card class

    Java 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;
    	}
    	
    }
    and finally the test

    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)

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default

    Here is more of an SSCCE:

    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());
    		}
    	}
    }
    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.

    One solution would be to create a new Card each iteration of the loop and add that to the array instead.
    Fubarable likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    mrbrendano is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    Oh right. I wasnt sure how exactly you wanted it. Il know for next time! I will also start putting a capitol for the start of the class.

    i moved the c=new card() into the loop and it sorted my problem right out.

    Thanks very much Kevin

Similar Threads

  1. Help with creating a deck of cards
    By Carouselification in forum New To Java
    Replies: 5
    Last Post: 03-06-2011, 04:45 AM
  2. Deck of cards problem
    By VelvetMirror in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 08:02 PM
  3. Poker Simulation (shuffling a deck of cards?)
    By StateofMind in forum New To Java
    Replies: 4
    Last Post: 04-06-2010, 08:59 PM
  4. Deck of Cards
    By khunmato in forum New To Java
    Replies: 13
    Last Post: 09-06-2009, 05:47 PM
  5. Problem with making/sorting a deck of cards
    By Franneldort in forum New To Java
    Replies: 9
    Last Post: 11-07-2008, 12:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •