Array passing variables

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 317 Views - Last Post: 09 October 2012 - 01:58 PM Rate Topic: -----

#1 alex71385  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 27-October 09

Array passing variables

Posted 09 October 2012 - 11:11 AM

I need assistance figuring out how to pass values of one array from one method to 4 different array in another method (specifically from shuffled deck to four hands). this is my complete code. Thank you.

public class DeckOfCards {
	
	private static String[] suits = {"S", "H", "D", "C"};
	private static String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
	
	public static void main(String[] args) {
		int[] deck = new int[52];
		
		deal(deck);
	}		

	//Method for shuffling cards
	public static void shuffle(int[]deck) {
		//Initialize cards
		for (int i = 0; i < deck.length; i++)
			deck[i] = i;
		
		//Shuffle the cards for 2-51
		for (int i = 51; i > 2; i--) {
			//Generate and index randomly
			int index = (int)(Math.random() * deck.length);
			int temp = deck[i];
			deck [i] = deck[index];
			deck[index] = temp;
			}
		
		//Shuffle card for 0-1
		for (int i = 1; i < 2; i++) {
		int index = 0 + (int)(Math.random() * 2);
		int temp = deck[i];
		deck [i] = deck[index];
		deck[index] = temp;
		}
	}

	
	//Deal the cards
	public static void deal(int[] deck) {
		int[] handOne = new int[13];
		int[] handTwo = new int[13];
		int[] handThree = new int[13];
		int[] handFour = new int[13];
		
		shuffle(deck);
		
		for (int i = 0; i < deck.length; i++) {
            //System.out.print(deck[i] + " \n" );
       
			handOne[i] = deck[i];
			handTwo[i] = deck[i] + 1;
			handThree[i] = deck[i + 2];
			handFour[i] = deck[i + 3];		
		
	        System.out.print(handOne[i] + " " );
	        System.out.print(handTwo[i] + " " );
	        // System.out.print(handThree[i] + " " );
	        // System.out.print(handFour[i] + " " );
			//String suit = suits[deck[i] / 13];
			//String rank = ranks[deck[i] % 13];
			}
	}
}



This is the error I'm getting, and also the cards don't seem to be shuffled anymore.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
at DeckOfCards.deal(DeckOfCards.java:54)
at DeckOfCards.main(DeckOfCards.java:11)

This post has been edited by alex71385: 09 October 2012 - 11:17 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Array passing variables

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2110
  • View blog
  • Posts: 8,774
  • Joined: 20-September 08

Re: Array passing variables

Posted 09 October 2012 - 11:19 AM

Quote

        for (int i = 0; i < deck.length; i++) {
            //System.out.print(deck[i] + " \n" );
            handOne[i] = deck[i];


That's not going to work - 'deck' is 4 times as long as 'handOne'
Was This Post Helpful? 0
  • +
  • -

#3 alex71385  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 27-October 09

Re: Array passing variables

Posted 09 October 2012 - 11:21 AM

View Postg00se, on 09 October 2012 - 12:19 PM, said:

Quote

        for (int i = 0; i < deck.length; i++) {
            //System.out.print(deck[i] + " \n" );
            handOne[i] = deck[i];


That's not going to work - 'deck' is 4 times as long as 'handOne'


I'm trying to break down deck array into the four hand arrays.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2110
  • View blog
  • Posts: 8,774
  • Joined: 20-September 08

Re: Array passing variables

Posted 09 October 2012 - 11:33 AM

Quote

I'm trying to break down deck array into the four hand arrays.

OK, but bear in mind all you have at that point is an array of int, all values being zero
Was This Post Helpful? 0
  • +
  • -

#5 alex71385  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 27-October 09

Re: Array passing variables

Posted 09 October 2012 - 11:46 AM

View Postg00se, on 09 October 2012 - 12:33 PM, said:

Quote

I'm trying to break down deck array into the four hand arrays.

OK, but bear in mind all you have at that point is an array of int, all values being zero

This is the hint my instructor gave me "After the shuffle, your deck is an array of 52 sorted slot numbers. In the deal method, create 4 new arrays, one for each hand. Then loop through the deck array, and copy element i to the first hand array, i+1 to the second, i+2 to the third, and i+3 to the fourth. So for each iteration of the loop, you will want to advance 4 cards. Now you have four arrays, one for each hand, and each array has 13 slot numbers corresponding to cells in the deck array."

So I put shuffle method in there so that maybe the new values will be passed for the deck and uses the array copy method shown in the book (targetArray[i] = sourceArray[i] but it's not working like I though it would be.
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2110
  • View blog
  • Posts: 8,774
  • Joined: 20-September 08

Re: Array passing variables

Posted 09 October 2012 - 12:01 PM

Quote

This is the hint my instructor gave me "After the shuffle, your deck is an array of 52 sorted slot numbers.

It should be but isn't (the array contains nothing other than zeros) - there's something you need to do, but haven't
Was This Post Helpful? 0
  • +
  • -

#7 alex71385  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 27-October 09

Re: Array passing variables

Posted 09 October 2012 - 12:04 PM

View Postg00se, on 09 October 2012 - 01:01 PM, said:

Quote

This is the hint my instructor gave me "After the shuffle, your deck is an array of 52 sorted slot numbers.

It should be but isn't (the array contains nothing other than zeros) - there's something you need to do, but haven't

The array should consist of the random numbers filled from the shuffle method.
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2110
  • View blog
  • Posts: 8,774
  • Joined: 20-September 08

Re: Array passing variables

Posted 09 October 2012 - 12:07 PM

Oops sorry - i'm only half awake ;) Yes - you're on track on that front
Was This Post Helpful? 0
  • +
  • -

#9 alex71385  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 27-October 09

Re: Array passing variables

Posted 09 October 2012 - 12:11 PM

View Postg00se, on 09 October 2012 - 01:07 PM, said:

Oops sorry - i'm only half awake ;) Yes - you're on track on that front

Can you point me in the right direction of how to get values of that array passed and split into 4 other arrays please?
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2110
  • View blog
  • Posts: 8,774
  • Joined: 20-September 08

Re: Array passing variables

Posted 09 October 2012 - 12:33 PM

This is how i'd do it

       shuffle(deck);

        int[][] hands = new int[][] { handOne, handTwo, handThree, handFour };

        for (int i = 0; i < hands.length; i++) {
            System.arraycopy(deck, i * 13, hands[i], 0, hands[i].length);
	    //DEBUG
	    System.out.println(java.util.Arrays.toString(hands[i]));
        }



You might want that 2d array to stick around

This post has been edited by g00se: 09 October 2012 - 12:34 PM
Reason for edit:: 2d

Was This Post Helpful? 0
  • +
  • -

#11 alex71385  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 27-October 09

Re: Array passing variables

Posted 09 October 2012 - 12:46 PM

View Postg00se, on 09 October 2012 - 01:33 PM, said:

This is how i'd do it

       shuffle(deck);

        int[][] hands = new int[][] { handOne, handTwo, handThree, handFour };

        for (int i = 0; i < hands.length; i++) {
            System.arraycopy(deck, i * 13, hands[i], 0, hands[i].length);
	    //DEBUG
	    System.out.println(java.util.Arrays.toString(hands[i]));
        }



You might want that 2d array to stick around


We haven't covered 2d arrays yet so I'm not too sure what's going on in that code. Is there a way to do different single arrays? because after the had arrays are done, i still need to implement the code that identifies what number is what card. (suits and ranks array coding in deal method)
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4880
  • View blog
  • Posts: 11,272
  • Joined: 16-October 07

Re: Array passing variables

Posted 09 October 2012 - 12:47 PM

You have some good, some bad:
// maybe
for (int i = 0; i < deck.length; i++) {
	handOne[i] = deck[i]; // fine
	handTwo[i] = deck[i] + 1; // OOPS, check that syntax
	handThree[i] = deck[i + 2]; // makes sense



The problem here is that you're only moving for i++. You want a larger step than that. Or, you want to keep i++ but multiply the card you grab from deck.

To be honest, you're making things harder than they need be. Your deck should be it's own object with a deal method.
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2110
  • View blog
  • Posts: 8,774
  • Joined: 20-September 08

Re: Array passing variables

Posted 09 October 2012 - 01:01 PM

Quote

Is there a way to do different single arrays?

Certainly. Two ways:

a. Get rid of the loop and just do the arraycopy four times
b. Use a single loop and adjust the indexes appropriately
Was This Post Helpful? 1
  • +
  • -

#14 alex71385  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 27-October 09

Re: Array passing variables

Posted 09 October 2012 - 01:04 PM

View Postbaavgai, on 09 October 2012 - 01:47 PM, said:

You have some good, some bad:
// maybe
for (int i = 0; i < deck.length; i++) {
	handOne[i] = deck[i]; // fine
	handTwo[i] = deck[i] + 1; // OOPS, check that syntax
	handThree[i] = deck[i + 2]; // makes sense



The problem here is that you're only moving for i++. You want a larger step than that. Or, you want to keep i++ but multiply the card you grab from deck.

To be honest, you're making things harder than they need be. Your deck should be it's own object with a deal method.

I changed the code to
for (int i = 0; i < deck.length; i = i + 4) {
            //System.out.print(deck[i] + " \n" );
       
			handOne[i] = deck[i];
			handTwo[i] = deck[i + 1];
			handThree[i] = deck[i + 2];
			handFour[i] = deck[i + 3];	

But I'm getting error what looks like it's third loop:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 16
at DeckOfCards.deal(DeckOfCards.java:50)
at DeckOfCards.main(DeckOfCards.java:10)
35 1 23 22 5 37 39 28

I'm assuming that's because that's not really dividing the array and once it reaches the value in deck array that's larger than 13 it can no longer support inputs but not sure how to fix it. i see that it's basically transferring the i value to same value in the other array instead of taking the value of the deck and putting it into the next available array slot.
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8017
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: Array passing variables

Posted 09 October 2012 - 01:12 PM

This is not only the problem. Doing i = i + 4 will assign values to
handOne[0], handOne[4], handOne[8],.... what about the others ?
I would do it that way

int k = 0;
for (int i = 0; i < handOne.length; i++) {       
			handOne[i] = deck[k++];
			handTwo[i] = deck[k++];
			handThree[i] = deck[k++];
			handFour[i] = deck[k++];	
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2