Array passing variables

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,761
  • Joined: 16-October 07

Re: Array passing variables

Posted 09 October 2012 - 01:13 PM

The problem is now the size of the hand...

I'd go the other way:
final int HAND_SIZE = 13; // magic numbers, bad
int[] handOne = new int[HAND_SIZE];
int[] handTwo = new int[HAND_SIZE];
//...
int cardNo = 0;
for (int i = 0; i < HAND_SIZE; i++) {
	handOne[i] = deck[cardNo++];
	handTwo[i] = deck[cardNo++];


Was This Post Helpful? 0
  • +
  • -

#17 alex71385  Icon User is offline

  • D.I.C Head

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

Re: Array passing variables

Posted 09 October 2012 - 01:44 PM

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

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

Thank you!
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);
		
		System.arraycopy(deck, 0, handOne, 0, 13);
		System.arraycopy(deck, 13, handTwo, 0, 13);
		System.arraycopy(deck, 26, handThree, 0, 13);
		System.arraycopy(deck, 39, handFour, 0, 13);
		
		for (int i = 0; i < 13; i++) {
	        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];
			  
			}
	}


I got it with the arraycopy. Now I'm trying to implement the assignment of suit and rank to the card that was originally done with the deck array (as seen with // here) to each one of the hand arrays. I know the best way would be creating it's own method and just passing different array to it each time, but I've only done methods with parameters already assigned. Not sure how I can write out the code and pass array to it later instead of writing
public static void cards(int[] handOne) {
String suit = suits[handOne[i] / 13];
String rank = ranks[handOne[i] % 13];
}


Because in the end I need to print out something like: "Hand 1: 1A 5S JA etc" for each array.
Was This Post Helpful? 0
  • +
  • -

#18 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2780
  • View blog
  • Posts: 11,765
  • Joined: 20-September 08

Re: Array passing variables

Posted 09 October 2012 - 01:58 PM

Quote

I got it with the arraycopy.

Yes, but you should avoid 'magic numbers'. Use a variable to specify index. pbl's method is another approach worth looking at.

baavgai's comment should also be re-examined. You're now beginning to use 'parallel arrays' which should be avoided
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2