0 Replies - 1493 Views - Last Post: 24 March 2010 - 07:50 PM Rate Topic: -----

#1 secretformula  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 10-December 09

Black Jack Game - Arraylist Exeption

Posted 24 March 2010 - 07:50 PM

So I am trying to make a black jack game however when I attempt to run it I get a "Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 43, Size: 0
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at game.Game.getRandomCard(Game.java:23)
at game.Player.<init>(Player.java:14)
at game.Game.<init>(Game.java:7)
at game.BlackJack.main(BlackJack.java:7)"
I dont see why I am getting this as I have the game constructor initializing the array list.
**Not show two Enums for cardSuit and cardType.
Game.java
package game;

public class Game extends Thread
{
	private static final int NUM_DECKS = 2;
	private static DeckOfCards deck = new DeckOfCards();
	Player player = new Player();
	Dealer dealer = new Dealer();
	
	public Game()
	{
		deck.newDeck(NUM_DECKS);
	}
	public void run()
	{
		System.out.println("Dealer has a "+ dealer.showHand().cardShowing() +" showing");
		player.run();
		dealer.setDealer(true);
		dealer.run();
	}
	public static PlayingCard getRandomCard()
	{
		return deck.getDeckOfCards().get((int)(Math.random() * (52 * NUM_DECKS)));
	}
	public int participantCompare(Participant player, Participant dealer)
	{
		int playerTotal, dealerTotal;
		playerTotal = player.showHand().getTotal();
		dealerTotal = dealer.showHand().getTotal();
		if(playerTotal > dealerTotal )
			return 1;
		else if(playerTotal == dealerTotal)
			return 0;
		else
			return -2;
	}
}

DeckOfCards.java
package game;

import java.security.InvalidParameterException;
import java.util.ArrayList;
public class DeckOfCards 
{
	private ArrayList<PlayingCard> deckOfCards = new ArrayList<PlayingCard>();
	
	public DeckOfCards( )
	{
	}
	private void createWholeDeck()
	{
		cardSuit suit = cardSuit.none;
		cardType type = cardType.none;
		for(int i = 0; i <= 13; i++)
		{
			for(int n = 0; n <= 3; n++)
			{
				switch(i)
				{
				case 0:
					type = cardType.Ace;
					break;
				case 1:
					type = cardType.One;
					break;
				case 2:
					type = cardType.Two;
					break;
				case 3:
					type = cardType.Three;
					break;
				case 4:
					type = cardType.Four;
					break;
				case 5:
					type = cardType.Five;
					break;
				case 6:
					type = cardType.Six;
					break;
				case 7:
					type = cardType.Seven;
					break;
				case 8:
					type = cardType.Eight;
					break;
				case 9:
					type = cardType.Nine;
					break;
				case 10:
					type = cardType.Ten;
					break;
				case 11:
					type = cardType.Jack;
					break;
				case 12:
					type = cardType.Queen;
					break;
				case 13:
					type = cardType.King;
					break;
				}
				switch(n)
				{
				case 0:
					suit = cardSuit.Clubs;
					break;
				case 1:
					suit = cardSuit.Diamonds;
					break;
				case 2:
					suit = cardSuit.Hearts;
					break;
				case 3:
					suit = cardSuit.Spades;
					break;
				}
				PlayingCard card = new PlayingCard(suit, type);
				deckOfCards.add(card);
			}
		}
	}
	public ArrayList<PlayingCard> getDeckOfCards() {
		return deckOfCards;
	}
	public void addCard(cardSuit suit, cardType type )
	{
		PlayingCard card = new PlayingCard(suit, type);
		deckOfCards.add(card);
	}
	public void newDeck()
	{
			createWholeDeck();
	}
	public void newDeck(int numDecks)
	{
		if( numDecks < 1)
			throw new InvalidParameterException("Parameter must be greater than or equal to 1");
		deckOfCards.clear();
		for(int i = 1; i <= numDecks; i++)
		{
			createWholeDeck();
		}
	}
	public void newCustomDeck( int deckSize)
	{
		for(int i = 1; i <= deckSize; i++)
		{
			PlayingCard card = new PlayingCard();
			deckOfCards.add(card);
		}
	}
	public int getCardDeckSize( )
	{
		return deckOfCards.size();
	}
	public String toString()
	{
		return ""+deckOfCards;
	}
}


PlayingCard.java
package game;

public class PlayingCard 
{
	private cardType type;
	private cardSuit suit;
	
	public PlayingCard( ) // Generates a random card
	{
		int r = (int)(Math.random() * 14 );
		switch(r)
		{
		case 0:
			type = cardType.Ace;
			break;
		case 1:
			type = cardType.One;
			break;
		case 2:
			type = cardType.Two;
			break;
		case 3:
			type = cardType.Three;
			break;
		case 4:
			type = cardType.Four;
			break;
		case 5:
			type = cardType.Five;
			break;
		case 6:
			type = cardType.Six;
			break;
		case 7:
			type = cardType.Seven;
			break;
		case 8:
			type = cardType.Eight;
			break;
		case 9:
			type = cardType.Nine;
			break;
		case 10:
			type = cardType.Ten;
			break;
		case 11:
			type = cardType.Jack;
			break;
		case 12:
			type = cardType.Queen;
			break;
		case 13:
			type = cardType.King;
			break;
		}
		r = (int)(Math.random() * 4 );
		switch(r)
		{
		case 0:
			suit = cardSuit.Clubs;
			break;
		case 1:
			suit = cardSuit.Diamonds;
			break;
		case 2:
			suit = cardSuit.Hearts;
			break;
		case 3:
			suit = cardSuit.Spades;
			break;
		}
	}
	public PlayingCard(cardSuit suit, cardType type) // Generates a specified card
	{
		this.suit = suit;
		this.type = type;
	}
	public cardType getType() {
		return type;
	}
	public void setType(cardType type) {
		this.type = type;
	}
	public cardSuit getSuit() {
		return suit;
	}
	public void setSuit(cardSuit suit) {
		this.suit = suit;
	}
	public String toString() {
		return type +" of "+ suit;
	}
}


Player.java
package game;

import io.In; // My own IO library


public class Player extends Thread implements Participant
{
	private boolean dealer = false;
	private boolean myTurn = true;
	private boolean busted = false;
	private Hand hand = null;
	public Player()
	{
		getHand( Game.getRandomCard(), Game.getRandomCard());
	}
	public void run()
	{
		do
		{
			System.out.print("You have: ");
			for(int i = 0; i < hand.getHandSize(); i++)
			{
				System.out.print(hand.getHand()[i] +" ");
			}
			System.out.println("for a total of "+ hand.getTotal());
			for(int i = -5; i != 1;)
			{
				int input = getPlayerAction();
				switch(input)
				{
				case 1:
					hit();
					i=1;
					break;
				case 2:
					stand();
					i=1;
					break;
				case 3:
					if( hand.getHandSize() == 2 )
					{
						doubleDown();
						i=1;
					}
					break;
				default:
					break;
				}
			}
			if( hand.getTotal() > 21 )
			{
				myTurn = false;
				busted = true;
			}
		}
		while(myTurn == true);
	}
	public boolean isBusted() {
		return busted;
	}
	public Hand showHand( )
	{
		return hand;
	}
	private void getHand( PlayingCard card1, PlayingCard card2)
	{
		hand = new Hand(card1, card2);
	}
	private void addCardToHand( PlayingCard card)
	{
		hand.addCard(card);
	}
	private int getPlayerAction()
	{
		System.out.println("Enter an action\nHit: 1\nStand: 1");
		if( hand.getHandSize() == 2 )
			System.out.println("Double Down: 3");
		return In.promptInt("?");
	}
	private void hit()
	{
		System.out.println("Hit");
		addCardToHand(Game.getRandomCard());
	}
	private void stand()
	{
		System.out.println("Stand");
		myTurn = false;
	}
	private void doubleDown()
	{
		System.out.println("Double Down");
	}
	public String toString() {
		return "Player [busted=" + busted + ", dealer=" + dealer + ", hand="
				+ hand + ", myTurn=" + myTurn + "]";
	}
}


And Dealer.java
package game;

public class Dealer extends Thread implements Participant
{
	private boolean dealer = true;
	private boolean myTurn = false;
	private boolean busted = false;
	private Hand hand = null;
	
	public void setDealer(boolean dealer) {
		this.dealer = dealer;
	}
	public Dealer()
	{
		getHand( Game.getRandomCard(), Game.getRandomCard());
	}
	public void run()
	{
		do
		{
			DealerAction();
		}
		while( myTurn && !busted );
	}
	public boolean isBusted() {
		return busted;
	}
	public Hand showHand( )
	{
		return hand;
	}
	private void getHand( PlayingCard card1, PlayingCard card2)
	{
		hand = new Hand(card1, card2);
	}
	private void addCardToHand( PlayingCard card)
	{
		hand.addCard(card);
	}
	private void DealerAction()
	{
		int currentTotal = hand.getTotal();
		if( currentTotal >= 17 )
			stand();
		else if( currentTotal < 9 )
			hit();
		else if( (int)(Math.random()*7) < 4 || (int)(Math.random()*7) == 7 )
			stand();
		else
			hit();
		if( currentTotal > 21 )
			busted = true;
	}
	private void hit()
	{
		System.out.println("Hit");
		addCardToHand(Game.getRandomCard());
	}
	private void stand()
	{
		System.out.println("Stand");
		myTurn = false;
	}
	public String toString() {
		return "Dealer [busted=" + busted + ", dealer=" + dealer + ", hand="
				+ hand + ", myTurn=" + myTurn + "]";
	}
}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1