Jump to content

Need help with a simple Card Dealing program

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
guamian

guamian

    Newbie

  • Members
  • Pip
  • 5 posts
Requirement of this program:
The program have to deal 5 cards (doesn't matter it is in a order or not) and the ranks and the suits of the 5 cards has to be print out by toString() method.

The class Card has already been made and it is not allowed to modify. The exercise for the programmer is to make a Deck class, and a main method to initialize.

The structure of the Deck class are not allowed to modify, which means the methods getCard, toString, fill is the part of the class Deck.

I need help:
I am sure that it must be something wrong in the toString methods in the class Deck. But i don't know how did i get out of bound of the array?
Perheps there are something wrong in the getCard and fill methods. Dunno :(
Please help

------------------------------------------------------
import java.util.*;

public class Deck
{
	private Card[] theCards;
	private int noOfCards;
	
	public Deck()
	{
		theCards = new Card[52];
		noOfCards = 52;
		this.fill();
		//fill();
	}
	
	public int getNoOfCards()
	{
		return noOfCards;
	}
	
	
	/**Metoden getCard lämnar ut en referens till det översta 
	 * kortet i leken (det för tillfället sista kortobjektet 
	 * i arrayen). Glöm inte minska värdet på datamedlemmen 
	 * för antalet kort.
	 */
	
	public Card getCard()
	{
		Card a = null;

		a = theCards[noOfCards-1];
		noOfCards--;
		return a;
	}
	
    public String toString() 
    {
        String deckString = "New deck created\n";

        for(int i = 1; i <= 5; i++)
        {
        	deckString += theCards[i].toString() + "\n";
        }
        
        return deckString;
    }

	public void shuffleCards()
	{
		Random random = new Random();
		Card temp;
		int pos1, pos2;				  // Two random positions
		for(int i = 0; i < 30; i++) 
		{
			pos1 = random.nextInt(noOfCards);
			pos2 = random.nextInt(noOfCards);
			// Swap
			temp = theCards[pos1];
			theCards[pos1] = theCards[pos2];
			theCards[pos2] = temp;
		}
	}

	private void fill()
	{		
		int i, j;
		int index = 0;
		for(i = 0; i < 4; i++)
		{
			for(j = 1; j < 14; j++)
			{
				theCards[index] = new Card(i, j);
				index++;
			}
		}
	}
}
--------------------------------------------------------------------
/** Objects of this class represents cards in
 *	a deck (of cards).
 *	A card is immutable, i.e. once created its
 *	rank or suit cannot be changed.
 */

public class Card
{
	
	/** rank: 1 = Ace, 2 = 2, ...
	 *  suit: 1 = spades, 2 = hearts, ...
	 */						
	public Card(int rank, int suit)
	{
		this.rank = rank;
		this.suit = suit;
	}
	
	public int getRank()
	{
		return rank;
	}
	
	public int getSuit()
	{
		return suit;
	}
	
	public String toString()
	{
		//System.out.println("rank =" + rank);
		//System.out.println("suit =" + suit);
		String info = rankTab[rank-1] + " of " + suitTab[suit-1];
		return info;
	}
	
	private int rank, suit;

	// Tables for converting rank & suit to text (why static?)
	private static final String[] rankTab = {
		"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", 
		"Jack", "Queen", "King"	
	};
	
	private static final String[] suitTab = {
		"clubs", "diamonds", "spades", "hearts"
	};
}
-----------------------------------------------------------
public class CardMain
{
	public static void main(String[] args)
	{
		Deck d = new Deck();
		// Initailized with rank 12 (Queen) and suit 3 (spades)
		Card c = new Card(12, 3); 
		
		System.out.println("The five cards you have got are: "
				+ d.toString());
		//d.shuffleCards();
	}
}

Edited by WingedPanther, 03 October 2009 - 06:02 AM.
add code tags (the # button)


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
the problem lies in the fill method from the Deck class.

private void fill()

{

  int i, j;

  int index = 0;

  for(i = 0; i < 4; i++)

  {

    for(j = 1; j < 14; j++)

    {

    theCards[index] = new Card(i, j);

    index++;

    }

  }

}

first of all, you start with i from 0. As you will do rank-1 and suit-1 in the ToString from Card class this will result in -1 -->indexoutofbounds.
So i must start from 1 and up to i<5

Next you do
theCards[index] = new Card(i, j);
where i ranges from 1-4 and j from 1-13. but the constructor is like:
public Card(int rank, int suit)
so i and j must swap place:


private void fill()

{

int i, j;

int index = 0;

for(i = 1; i < 5; i++)

{

for(j = 1; j < 14; j++)

{

theCards[index] = new Card(j, i);

index++;

}

}

}

}


now it works.
I find it a bid odd that the tostring in Deck goes from 1-5 and not 0-5 btw:
for(int i = 1; i <= 5; i++)

#3
guamian

guamian

    Newbie

  • Members
  • Pip
  • 5 posts
Thanks a lot man :) What you explained are really easy to understand. Thanks!