Results 1 to 12 of 12
  1. #1
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default A little confused (class variable scope?)

    Everything in main works up until I try to call getHand.
    After deck.generate(), the deck looks like:
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

    After deck.shuffle(), the deck is randomized to something like:
    23 12 33 9 32 28 52 6 11 42 48 44 21 22 31 40 25 3 45 47 5 35 10 2 14 34 36 16 29 38 7 18 37 4 1 39 46 30 19 24 41 43 27 50 17 49 15 8 51 13 26 20

    Then deck.getHand() is called, and something odd happens with the print statements (still in code):
    23 12
    23 12

    Both the player and the dealer are dealt the same hand. I've tried several ways to force the index to shift, and currently it is what I have below. I am at a loss for why the values at the index are not changing to zero (or perhaps they are, but revert back for the next call?).

    What is expected (based on above example):
    playerHand should have two consecutive integers, 23 and 12.
    dealerHand should have the next two consecutive integers, 33 and 9.


    Java Code:
    import javax.swing.JOptionPane;
    
    public class PlayGame {
    
    	public static void main(String[] args) {
    	
    		DeckOfCards deck = new DeckOfCards();
    		deck.generate();
    		deck.shuffle();
    		int[] playerHand = deck.getHand();
    		int[] dealerHand = deck.getHand();
    
    		System.out.println(
    				playerHand[0] +
    				" " +
    				playerHand[1]);
    		System.out.println(
    				dealerHand[0] +
    				" " +
    				dealerHand[1]);
    	}
    }
    
    class DeckOfCards {
    
    	// Data fields:
    	private int[] deck = new int[52];
    	private int[] hand = new int[2];
    	
    	// Zero-parameter Constructor:
    	public DeckOfCards() {
    	}
    	
    	public int[] generate() {
    	/* Returns a fresh, unshuffled deck
    	   by setting values of deck[] from
    	   1 to 52. */
    		for (int i = 0; i < deck.length; i++) {
    			deck[i] = i + 1;
    		}
    		
    		return deck;
    	}
    	
    	public int[] shuffle() {
    	/* Returns a shuffled deck */
    		for (int i = 0; i < deck.length; i++) {
    			int index = (int)(Math.random() * deck.length);
    			int temp = deck[i];
    			deck[i] = deck[index];
    			deck[index] = temp;
    		}
    	
    		return deck;
    	}
    	
    	public int[] getHand() {
    	/* Returns a 2-card hand. */
    		for (int i = 0; i < 2; i++) {
    			if (deck[i] != 0) {
    				hand[i] = deck[i];
    				deck[i] = 0;
    			}
    		}
    		
    		return hand;
    	}
    	
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Everytime you call getHand i is set to 0 so the cards obtained will always be from the start of the array. It would be better if you used a List and removed the cards. Or a quick fix, create an instance variable that continues to increment and use that instead of i to index your array.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    It looks like your problem is it's just selecting the first two cards from the random deck every time you call the getHand method. You need to find a way to modify the deck to have the 2 chosen cards removed, or change the index to be 0 + initial handsize.
    Using an arrayList<Integer> may be the best way to represent the deck, when you choose cards for a hand, also call al.remove().

    You may have to add some logic for when the hand is over to reset the deck, but that should be fairly simple.

  4. #4
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    Ah, I think I see what's going on now. The first hand is taken fine, but when the second getHand is called it sees 0s in the first and second indices, and since i can only be 0 or 1, it just returns hand (which is the same hand from any previous call).

    I'm not sure how to use array lists (only ~4 weeks into Intro. to Java) and I'm just trying to work with what I know.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Then do my second suggestion and use a separate variable to index the cards.
    Java Code:
    hand[i] = deck[someOtherCounter++];
    Then get rid of the if statement and fannying about with setting values to 0.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Oh!

    I just thought you will also need to create a new array and assign it to hand as the first line of the method. Otherwise both hands will end up with the 3rd and 4th cards.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Like above you can create a class instance variable that always starts at 0 and increments when the getHand method is called. so instead of
    Java Code:
    for(int i = 0; something; something)
    you could do
    Java Code:
    while(condition){
    add card to hand
    increment instance variable
    }
    This is rough and you might have to do it a little differently, but it should work.

    If you want to use an arrayList it's not too difficult to use them, they tend to be very similar to arrays.

    The syntax is different and in the api it documents it all in much more detail.

    You can use the .get method like indexing into an array.
    Use .remove() to remove an item.

    An arrayList allows you to have a list which can resize if necessary.

    Here is the link to more documentation, if you don't feel comfortable because you are new, thats fine, but this is a good opportunity to use them, and it may give a a good understanding of them.

    The final thing I want to explain is the part in between <> in
    arrayList<Integer>

    an arrayList normally takes objects, so if you store a bunch of Integer objects in an arraylist it would send them all out as Objects, not Integers. If you use the <> which is known as generics(of which I am still somewhat unfamiliar) it states that the arrayList must take type integer, therefore it's able to give you back Integers.

    http://download.oracle.com/javase/1....ArrayList.html
    Last edited by sunde887; 02-03-2011 at 02:31 AM.

  8. #8
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    @Sunde: They sound a bit far out of my range at the moment, but I'll certainly give it a look to see if I can understand it. Thank you!


    @Junky: That was exactly the problem; the scope of the hand being returned was accessible to the entire class, and each time a getHand was called, it would change the one hand (with both playerHand and dealerHand referencing the same array). Once I declared the int[] hand within getHand, it worked perfectly. Thank you as well!

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Actually you had 2 issues. The fact that you were returning a reference to the same array and also that the second time the method got called nothing changed the hand array.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    try adding
    Java Code:
    private static count;
    as a new variable in the deck class.

    then do
    Java Code:
    for(int i = 0; i < 2; i++){
      hand[i] = deck[count++];
    }
    The count will continually increase, so after you call getHand() count will be 2, the second call getHand() will be 4, etc.

    If you already figured it out, you can ignore this.

  11. #11
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    Right. I understood the second issue right after posting, but couldn't alleviate it without resolving the first issue.

    Here's what DeckOfCards looks like now (and it works; thanks again for the help):

    Java Code:
    class DeckOfCards {
    
    	// Data fields:
    	private int[] deck = new int[52];
    	private int currIndex;
    	
    	// Zero-parameter Constructor:
    	public DeckOfCards() {
    	}
    	
    	public int[] generate() {
    	/* Returns a fresh, unshuffled deck
    	   by setting values of deck[] from
    	   1 to 52. */
    		for (int i = 0; i < deck.length; i++) {
    			deck[i] = i + 1;
    		}
    		
    		return deck;
    	}
    	
    	public int[] shuffle() {
    	/* Returns a shuffled deck */
    		for (int i = 0; i < deck.length; i++) {
    			int index = (int)(Math.random() * deck.length);
    			int temp = deck[i];
    			deck[i] = deck[index];
    			deck[index] = temp;
    		}
    	
    		return deck;
    	}
    	
    	public int[] getHand() {
    	/* Returns a 2-card hand. */
    		int[] hand = new int[2];
    		int i = 0;
    		for (i = 0; i < 2; i++)
    			hand[i] = deck[currIndex++];
    		
    		return hand;
    	}
    	
    }

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

Similar Threads

  1. Try/Catch Class Scope Issue
    By bnshrdr in forum New To Java
    Replies: 7
    Last Post: 01-11-2011, 02:47 PM
  2. variable scope between classes
    By newbie123 in forum New To Java
    Replies: 6
    Last Post: 03-03-2010, 11:20 PM
  3. Calling a class and a little confused
    By Boomer1 in forum New To Java
    Replies: 1
    Last Post: 12-29-2009, 06:10 PM
  4. Variable Scope
    By Laura Warren in forum New To Java
    Replies: 3
    Last Post: 01-11-2009, 10:16 PM
  5. Yet another Wrapper Class confused guy.
    By JAdeline in forum New To Java
    Replies: 2
    Last Post: 08-15-2008, 04:04 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
  •