Results 1 to 5 of 5
  1. #1
    StateofMind is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default Poker Simulation (shuffling a deck of cards?)

    I'm needing to write a program that simulates a poker game, and I'm stuck trying to shuffle my deck of cards. I made a 'Card' class, and now I'm working on a 'Deck' class that shuffles an array of 'Card' objects. I have my array built, but I don't really know how to go about shuffling the cards. Can anyone help?

    Here is my 'Card class:
    Java Code:
    public class Card
    {
    	public Card(String cardName, String suitName, int cardValue)
    	{
    		name = cardName;
    		suit = suitName;
    		value = cardValue;
    	}
    	public void showCard()
    	{
    		System.out.println(name+" of " +suit);
    	}
    	//return the card name
    	public String getName()
    	{
    	
    	return name;
    	}
    	//return the card suit
    	public String getSuit()
    	{
    		
    	return suit;
    	}
    	//return the card value
    	public int getValue()
    	{
    		
    	return value;
    	}
    	
    
    private String name;
    private String suit;
    private int value;
    }
    and here is my 'main' class:
    Java Code:
    public class main 
    {
    	public static void main(String[] args) 
    	{
    /**
    		 * CREATE CARDS *
    **/
    		//make an array of 'cards' objects
    		ArrayList<Card> cards = new ArrayList<Card>();
    		
    		/**
    		 * Spades
    		**/
    		cards.add(new Card("Ace","Spades",14));
    		cards.add(new Card("Two","Spades",2));
    		cards.add(new Card("Three","Spades",3));
    		cards.add(new Card("Four","Spades",4));
    		cards.add(new Card("Five","Spades",5));
    		cards.add(new Card("Six","Spades",6));
    		cards.add(new Card("Seven","Spades",7));
    		cards.add(new Card("Eight","Spades",8));			
    		cards.add(new Card("Nine","Spades",9));
    		cards.add(new Card("Ten","Spades",10));
    		cards.add(new Card("Jack","Spades",11));
    		cards.add(new Card("Queen","Spades",12));
    		cards.add(new Card("King","Spades",13));
    		
    		/**
    		 * Hearts
    		**/
    		cards.add(new Card("Ace","Hearts",14));
    		cards.add(new Card("Two","Hearts",2));
    		cards.add(new Card("Three","Hearts",3));
    		cards.add(new Card("Four","Hearts",4));
    		cards.add(new Card("Five","Hearts",5));
    		cards.add(new Card("Six","Hearts",6));
    		cards.add(new Card("Seven","Hearts",7));
    		cards.add(new Card("Eight","Hearts",8));
    		cards.add(new Card("Nine","Hearts",9));
    		cards.add(new Card("Ten","Hearts",10));
    		cards.add(new Card("Jack","Hearts",11));
    		cards.add(new Card("Queen","Hearts",12));
    		cards.add(new Card("King","Hearts",13));
    		
    		/**
    		 * Diamonds
    		**/
    		cards.add(new Card("Ace","Diamonds",14));
    		cards.add(new Card("Two","Diamonds",2));
    		cards.add(new Card("Three","Diamonds",3));
    		cards.add(new Card("Four","Diamonds",4));
    		cards.add(new Card("Five","Diamonds",5));
    		cards.add(new Card("Six","Diamonds",6));
    		cards.add(new Card("Seven","Diamonds",7));
    		cards.add(new Card("Eight","Diamonds",8));
    		cards.add(new Card("Nine","Diamonds",9));
    		cards.add(new Card("Ten","Diamonds",10));
    		cards.add(new Card("Jack","Diamonds",11));
    		cards.add(new Card("Queen","Diamonds",12));
    		cards.add(new Card("King","Diamonds",13));
    		
    		/**
    		 * Clubs
    		**/
    		cards.add(new Card("Ace","Clubs",14));
    		cards.add(new Card("Two","Clubs",2));
    		cards.add(new Card("Three","Clubs",3));
    		cards.add(new Card("Four","Clubs",4));
    		cards.add(new Card("Five","Clubs",5));
    		cards.add(new Card("Six","Clubs",6));
    		cards.add(new Card("Seven","Clubs",7));
    		cards.add(new Card("Eight","Clubs",8));
    		cards.add(new Card("Nine","Clubs",9));
    		cards.add(new Card("Ten","Clubs",10));
    		cards.add(new Card("Jack","Clubs",11));
    		cards.add(new Card("Queen","Clubs",12));
    		cards.add(new Card("King","Clubs",13));
    		
    /**
    		 * START GAME *
    **/
    		//create a deck of cards and shuffle
    		////here will be:
    		///Deck deck = new deck
    		///shuffle.deck
    		///etc.
    		
    	}
    
    }
    I plan on constructing the 'Deck' class like:
    Java Code:
    Deck(ArrayList<Card> card)
    { }
    public void shuffle()
    {
    ???
    }
    ...or something.

    I don't know anything about Poker so the 'value' field for Ace, Jack, Queen, and King are probably wrong. I'll fix that once I get the program running right.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Uhm, List has a method "shuffle", what does that do?

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,532
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by masijade View Post
    Uhm, List has a method "shuffle", what does that do?
    Small correction: the shuffle( ... ) method is a static utility method in the Collections class.

    kind regards,

    Jos

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by JosAH View Post
    Small correction: the shuffle( ... ) method is a static utility method in the Collections class.

    kind regards,

    Jos
    Oops. ;-)

  5. #5
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Talking

    I seem to remember being stuck in a similar quandary not so very long ago.


    You could try having a look through this thread.

    going round and round in class..

    my problem was slightly different but ultimately i was creating a PlayingCard() class and a DeckOfCards() class (the deck of cards contains 52 PlayingCard() objects) and then shuffling them.

    there are plenty of pointers in this thread ( but JosA and Tolls get into a an esoteric discussion about eficiency which confused the hell out of me at the time!! :rolleyes:)

    but focus on the posts between myself and Gary (no offense Jos and Tolls) and you should start to find the right track..

    any further questions you have post back in this thread.

    as a help, there is a much simpler way to make an array list of cards in the deck by declaring Enums in the cards class,, then having a (for each loop) to create the ArrayList of ranks and suits in the deck class.

    Also remember a card is a card. its just one card that is all that it is. you cannot shuffle one card, but you can shuffle a deck of cards

    Hope this helps :D

    kind regards
    Sonny
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

Similar Threads

  1. Please help Me out with this poker Problem
    By ckrisasa in forum Networking
    Replies: 8
    Last Post: 12-03-2009, 01:59 PM
  2. Deck of Cards
    By khunmato in forum New To Java
    Replies: 13
    Last Post: 09-06-2009, 05:47 PM
  3. Problem with making/sorting a deck of cards
    By Franneldort in forum New To Java
    Replies: 9
    Last Post: 11-07-2008, 12:47 AM
  4. Shuffling Help
    By KnivesACE in forum New To Java
    Replies: 1
    Last Post: 10-20-2008, 10:54 PM
  5. creating a deck of cards using a linked list
    By boomba88 in forum New To Java
    Replies: 2
    Last Post: 09-11-2008, 03:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •