2 Replies - 368 Views - Last Post: 07 October 2012 - 03:42 PM Rate Topic: -----

#1 alex71385  Icon User is offline

  • D.I.C Head

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

Printing out array in ranges

Posted 07 October 2012 - 02:41 PM

The whole program is shuffle and deal 52 cards to 4 people and print out their hands. Something similar to:
Person1: AS KD 3H 8S 6S 4C 6H TS JD AC 5C JH 5S
Person2: 4D 7H 8C 3C QD 2S TH JC 5H 7D QS 9H KS
etc.
The only way that I thought I could get it to work doesn't. I have no errors in code but get an error on runtime:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
at DeckOfCards.deal(DeckOfCards.java:57)
at DeckOfCards.main(DeckOfCards.java:13)

public class DeckOfCards {
	public static void main(String[] args) {
		int[] deck = new int[52];
		String[] suits = {"S", "H", "D", "C"};
		String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
		String[] playerOne = new String[13];
		String[] playerTwo = new String[13];
		String[] playerThree = new String[13];
		String[] playerFour = new String[13];
		//Shuffle the cards
		shuffle(deck);
		deal(deck, suits, ranks, playerOne, playerTwo, playerThree, playerFour);
		
		//Print out the hands
		for(int i = 0; i < 52; i++) {
			System.out.print("Person 1: " + playerOne[i] + " ");
			//}
		}
	}		

	//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, String[] suits, String[] ranks, String[] playerOne, String[] playerTwo, String[] playerThree, String[] playerFour) {
		for (int i = 0; i < 52; i++) {
			String suit = suits[deck[i] / 13];
			String rank = ranks[deck[i] % 13];
			String card = rank + suit;
			if (i < 13) {
				playerOne[i] = card;
				}
			else if (i < 26) {
				playerTwo[i] = card;
			}
			else if (i < 39) {
				playerThree[i] = card;
			}
			else if (i <52) {
				playerFour[i] = card;
			}
	}
	}
}





Advice please? Thank you in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Printing out array in ranges

#2 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

Re: Printing out array in ranges

Posted 07 October 2012 - 03:01 PM

Your loop is going from 0-51 but your player arrays are only 0-12, so when you do playerOne[i] it will get out of bounds

Your player arrays really screams for a 2D array

I don't like the way you are representing your deck. Right now you have to pass around the int array, suits, and ranks array just to deal with the deck

I made some changes to your code which hopefully will make it easier for you to work with

public static void main(String[] args) {
	//A 2D array containing each players cards
	//The first dimension represent a player
	//The second dimension represent the player's cards
	String[][] players = new String[4][13]; //Four players each with 13 cards
	String[] deck = initDeck();
	shuffle(deck);
	dealCards(deck, players);
}

public String[] initDeck() {
	String[] suits = {"S", "H", "D", "C"};
	String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
	String[] deck = new String[52];
	
	//Fill the deck with the suits and ranks
	//Each suit should have one of each rank
	int deckIndex = 0;
	for (int i=0; i<suits.length; i++) { //Loop through every suits
		for (int j=0; j<ranks.length; j++) { //Loop through every rank
			deck[deckIndex] = ranks[j] + suits[i];
			deckIndex++;
		}
	}

    return deck;
}

public void dealCards(String[] deck, String[][] players) {
	//See if you can implement this part :)/>
	throw new NotImplementedException();
}


This post has been edited by CasiOo: 07 October 2012 - 03:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 alex71385  Icon User is offline

  • D.I.C Head

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

Re: Printing out array in ranges

Posted 07 October 2012 - 03:42 PM

We were supposed to start with the code from the book and then modify it according to instructors directions/instructions. The shuffle was to be moved to another method, and I can try to figure out the 2D arrays, but we haven't covered them yet, they will be in our next assignment.

2. Perform the shuffling in a separate method. Be sure to pass the array as a parameter.

3. Change the shuffling algorithm to the one I specify below. This method gives a truly random shuffle.
Specifications:
3. Here’s the new shuffling algorithm. Recall that we start with 52 cards, in positions 0-51.
a. Pick a random integer in the range 0-51, inclusive. Swap card 51 with this (there is a small chance you will pick card 51 to swap with itself). After this, card 51 is fixed.
b. Pick a random integer in the range 0-50, inclusive. Swap card 50 with this (there is a small chance you will pick card 50 to swap with itself. After this, card 50 is fixed.
c. Continue on in this manner until you have fixed cards 2-51. Then pick a random integer in the range 0-1. Inclusive. Swap card 0 with this (50-50 chance it’s a real swap). Then you are finished.

4. “Deal” 13 cards to each of 4 people. Print the cards that each person is dealt in the manner specified below.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1