Results 1 to 9 of 9
  1. #1
    ThrashingBoy is offline Member
    Join Date
    Apr 2010
    Location
    Phoenix, AZ
    Posts
    25
    Rep Power
    0

    Default A little help with classes.

    I am trying to make a poker program and have recently learned about methods and am trying to incorporate them into my code but I have a problem (of course). But all of my cards are coming up as NULL. I had it drawing cards and everything okay before but each hand would sometimes get duplicate cards. So I tried making my deck a final variable in hopes that it will only make the deck one time and they would be forced to share the deck instead of each hand making their own. I am doubting I am going to be able to get the program to work the way I want with the way I am currently trying to make it work so any help would be nice.

    How can I make it so that the deck is in it's own easy to access class like I have it with a getCard(), and any other method I would like and also have it not create a new deck per player I make?

    Just remember guys, this code is far from finished and I know I need a lot more before I can start running a poker game, but I really need to get over this obstacle to move forward. I am still learning and I have been staring and messing with this code for days so use small words while explaining things please :)

    Java Code:
    public class Poker 
    {
    	static Deck accessDeck = new Deck();
    	
    	public static void main(String[] args)
    	{	
    		
    		Player player1 = new Player();
    		player1.printDeck();
    		//player1.getHand();
    	    //player1.showHand();
    	
    		System.out.println();
    		
    		Player player2 = new Player();
    		player2.printDeck();
    		//player2.getHand();
    		//player2.showHand();
    
    	}
    }
    Java Code:
    import java.util.Random;
    //////////This class creates and shuffles the deck that will be used//////////////////
    
    public class Deck 
    {
    	int[] ranks = {1,2,3,4,5,6,7,8,9,10,11,12,13};
    	String[] suit = {"D", "H", "C", "S"};   
    	String[] deck = new String[52];
    	final String[] DECK = new String[52];         
    	int[] rankValue = new int[52];   
    
    	public final String[] generateDeck()
    	{		
    		int j = 0;	
    		for(int s = 0; s < 4; s++)
    		{
    			int n = 0;
    			for(; n < 13; n++)
    			{
    				deck[j] = ranks[n] + suit[s];
    				rankValue[j] = ranks[n];
    				j += 1;
    			}
    		}		
    		//////////////////////SHUFFELING/////////////////////////
    		//Simply pulls out 2 random cards and makes them switch//
    		//places.  Does this 5000 times.                       //
    		/////////////////////////////////////////////////////////
    		Random myRandom = new Random();
    		String tempCardA;
    		String tempCardB;
    		int tempRankA;
    		int tempRankB;
    		int randomNumber1;
    		int randomNumber2;
    		for(int y = 0; y < 5000; y++)
    		{
    			randomNumber1 = myRandom.nextInt(52);
    			randomNumber2 = myRandom.nextInt(52);
    			
    			while(randomNumber1 == randomNumber2)
    			{
    				randomNumber2 = myRandom.nextInt(52);
    			}			
    			
    			tempCardA = deck[randomNumber1];
    			tempCardB = deck[randomNumber2];
    			tempRankA = rankValue[randomNumber1];
    			tempRankB = rankValue[randomNumber2];
    			
    			deck[randomNumber1] = tempCardB;
    			deck[randomNumber2] = tempCardA;	
    			rankValue[randomNumber1] = tempRankB;
    			rankValue[randomNumber2] = tempRankA;
    		}
    		return deck;
    	}
    	{
    		getDeck(generateDeck());
    	}
    	
    	public String[] getDeck(String[] deck)
    	{
    		final String[] DECK = deck;
    		return DECK;
    	}
    	
    	
    	String playerCard;
    	int nextCard = 1;
    	
    	public String getCard()
    	{		
    		playerCard = DECK[0];
    		DECK[0] = DECK[nextCard];
    		nextCard++;
    		return playerCard;
    	}
    }
    Java Code:
    ////////////Simply gets everything you will need for the players in a single, easy to access class//////////////
    
    public class Player {
    	
    	String playerCard;
    	String[] hand = new String[5];
    	int[] rank = new int[5];
    	Deck accessDeck = new Deck();
    
    	{
    	accessDeck.generateDeck();
    	}
    	
    	public void printDeck()
    	{
    		for(int counter = 0; counter < 52; counter++)
    		{
    			System.out.println(accessDeck.DECK[counter]);
    		}
    		
    	}
    	public String[] getHand()
    	{
    		for(int counter = 0; counter < 5; counter++)
    		{
    			hand[counter] = accessDeck.getCard();
    		}
    		return hand;
    	}
    	
    	public void showHand()
    	{
    		for(int counter = 0; counter < 5; counter++)
    		{
    			System.out.print(hand[counter] + " ");
    		}	
    	}
    	
    	public void showRank()
    	{
    		for(int counter = 0; counter < 5; counter++)
    		{
    			System.out.print(rank[counter] + " ");
    		}		
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I recommend a few things:

    1) Get rid of all statics at this point, including the deck as there's no need nor benefit by making this variable static.
    2) Add another class: Card, which holds a value and a suit.
    3) Deck should be a collection of Cards, say an ArrayList of Card.
    4) The Player objects shouldn't have access to the Deck, but should be able to accept Cards which the Deck will deal them.

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    have a look at Lists

    The List Interface (The Java™ Tutorials > Collections > Interfaces)

    then have a look at shuffling used by Collections

    Lesson: Algorithms (The Java™ Tutorials > Collections)

    you can have a List of Cards and then use shuffle on them to create a random pack.

  4. #4
    ThrashingBoy is offline Member
    Join Date
    Apr 2010
    Location
    Phoenix, AZ
    Posts
    25
    Rep Power
    0

    Default

    I looked up ArrayLists and that is indeed more efficient and easy to use then the String[] I was for the deck. However it is still remaking and reshuffling the deck everytime I try to call a card. How can I code this so that I can call a card without it making an entirely new deck?

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default

    Do the shuffle in an init routine and only call it once per game. Don't do it when you call a card.

  6. #6
    ThrashingBoy is offline Member
    Join Date
    Apr 2010
    Location
    Phoenix, AZ
    Posts
    25
    Rep Power
    0

    Default

    Ok, I have updated my program a little bit but now am even a little bit more confused.
    1) Why does it keep telling me that everything I have made in my Poker class have to be static?
    2) Why can't I set hand = Collections.shuffle(accessCard.getCard()); in my init() method?
    3) Although the ArrayList is easier to use, how do I get the rank of the card from it?

    I have tried changing this code around several different way and putting things in many different way just hoping something will work whether it makes sense or not. Can somebody perhaps update my code a bit to give me a boost?

    Java Code:
    import java.util.ArrayList;
    
    public class Poker
    {
    	static Card accessCard = new Card();
    	static Deck accessDeck = new Deck();
    	static Player player1 = new Player();
    	static Player player2 = new Player();
    	static ArrayList<String> hand = new ArrayList<String>(52);
    
    	public static void main(String[] args)
    	{	
    		hand = accessDeck.getHand();
    
    		System.out.println(accessDeck.getHand());
    
    		player1.showDeck();
    		player2.showDeck();
    	
    	}
    }
    Java Code:
    import java.util.*;
    
    public class Card 
    {
    	private int[] rank = {1,2,3,4,5,6,7,8,9,10,11,12,13};
    	private String[] suit = {"D", "H", "C", "S"};   
    	private ArrayList<String> card = new ArrayList<String>(52);        
    
    	public ArrayList<String> generateDeck()
    	{		
    		int j = 0;	
    		for(int s = 0; s < 4; s++)
    		{
    			int n = 0;
    			for(; n < 13; n++)
    			{
    				card.add(j, rank[n] + suit[s]);
    				j += 1;
    			}
    		}		
    		return card;
    	}	
    	
    	public ArrayList<String> getCard()
    	{
    		card = generateDeck();
    		return card;
    	}
    }
    Java Code:
    import java.util.*;
    
    public class Deck 
    {
    	public Card accessCard = new Card();
    	private ArrayList<String> hand = new ArrayList<String>(52);
    	
    	public ArrayList<String> init()
    	{
    		Collections.shuffle(accessCard.getCard());		
    		return hand;
    	}
    	
    	public ArrayList<String> getHand()
    	{
    	hand = init();
    	System.out.println(hand);
    	return hand;
    	}
    	
    }
    Java Code:
    import java.util.ArrayList;
    
    public class Player {
    	
    	Deck accessDeck = new Deck();
    	Card accessCard = new Card();
    	
    	String playerCard;
    	ArrayList<String> hand = new ArrayList<String>(52);
    	int[] rank = new int[5];
    	
    	{
    		hand = accessDeck.init();
    	}
    	
    	public void showDeck()
    	{
    		System.out.println(hand);
    	}
    	
    }

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    OK, you're making good progress, but you've still got a little confusion here. A Card is just a Card. It has a suit and a rank, and that's all. Its rank and suit should be set in a constructor (because it doesn't make any sense to create a Card without a rank and a suit, and it doesn't make sense to change a Card's rank or suit after it has been created). It should be able to tell you what its suit is and what its rank is, and nothing more. A Card has no business generating a Deck, so your Card class should have no generateDeck() method. A Card should not have any sort of ArrayList -- it is just a Card.

    A Deck is a collection of Card objects (an ArrayList<Card> is a good choice for its internal storage). The Deck's constructor should create 52 Card objects and store them in its ArrayList. You will find a pair of nested loops helpful for this. It should know how to shuffle itself. It should know which Cards and how many have been dealt to players, which Cards and how many are still undealt, and which Card is currently "on top". It should know how to deal() a card to a Player.

    You don't want each Player to have his own Deck. You want one Deck for the game. It might be useful for a Player to have a String name, and it should definitely have a hand -- either an ArrayList<Card> or Card[] raw array is fine. It might be nice if the Player could know the value of his hand -- high card, pair, two pair, three of a kind, straight, flush, etc.

    Call your main class PokerGame rather than Poker. Class names should be singular nouns -- it should make sense to say "create a new X" where X is your class name. "Create a new PokerGame" makes more sense than "create a new Poker" (unless you're talking about fireplace implements). Your PokerGame class should hold a Deck and an ArrayList<Player>.

    Let's say your initial goal is to play a game with two Players, where each Player will be dealt five Cards, and will simply display them. Here's some pseudocode for that:

    Java Code:
    create a new PokerGame, telling it how many Players to create (and maybe their names)
            PokerGame's constructor creates Deck and Players
    call your PokerGame object's startGame() method
            startGame():
                    tell Deck to shuffle()
                    for numCardsInHand
                            for numPlayers
                                    tell Deck to deal a Card to Player
                    for each Player
                            tell Player to showHand()
    That might still be confusing, but think it through and give it a try. Show us what you come up with and ask questions.

    -Gary-
    Last edited by gcalvin; 06-02-2010 at 11:19 AM.

  8. #8
    ThrashingBoy is offline Member
    Join Date
    Apr 2010
    Location
    Phoenix, AZ
    Posts
    25
    Rep Power
    0

    Default

    First of all I would like the thank the help I am getting from these forums, I am reading and trying to do everything myself, but sometimes you just need a little help and I appreciate it.

    I have greatly updated my program and I think it is looking pretty good now but I am running into the problem of comparing the hands. I am not sure how to tell the ranks and suits of the cards in any easy fashion. I have a getRank() and getSuit() method in my Card class but I don't see how it can work with my code anymore. So 3 questions plus any other comments would be appreciated.

    1) How can I get the Ranks and Suits from the cards now that they are all in their own arrays and away from the Card class after being shuffled and dealt?
    2) In my Player class Eclipse complains about the ArrayList of hand is "raw" but it works fine like this and doesn't want to work when I put in <Card> or <Deck>. What should I put in to make Eclipse happy?
    3) Do you think this is a little to complicated of a program to write for my first? (just curious)

    Java Code:
    import java.util.*;
    
    public class PokerGame
    {
    	static Deck accessDeck = new Deck();
    	
    
    	public static void main(String[] args)
    	{	
    		int numOfCards;
    		int numOfPlayers;
    		Scanner input = new Scanner(System.in);
    		
    		Deck accessDeck = new Deck();
    		System.out.println(accessDeck.getDeck());
    		
    		System.out.print("How many players?: ");
    		numOfPlayers = input.nextInt();		
    		
    		Player[] player = new Player[numOfPlayers];
    				
    		System.out.print("How many cards per hand?: ");
    		numOfCards = input.nextInt();
    		
    		for(int counter = 0; counter < numOfPlayers; counter++)
    		{
    			player[counter] = new Player();
    		}
    		
    		for(int counter = 0; counter < numOfCards; counter++)
    		{
    			for(int counter2 = 0; counter2 < player.length; counter2++)
    			{
    				player[counter2].setCard(accessDeck.getCard());
    			}
    		}
    		
    		for(int counter = 0; counter < numOfPlayers; counter++)
    		{
    			player[counter].showHand();
    		}
    		
    	}
    }
    Java Code:
    public class Card 
    {
    	private int rank;
    	private int suit;     
    	private String s;
    
    	public Card(int rank, int suit)
    	{
    		this.rank = rank;
    		this.suit = suit;		
    	}
    
    	public int getRank()
    	{
    		return rank;
    	}
    	
    	public int getSuit()
    	{
    		return suit;
    	}
    	
    	public String setSuitAsString()
    	{
    		switch(suit)
    		{
    		case 0: 
    			s = "D";
    			break;
    		case 1:
    			s = "C";
    			break;
    		case 2:
    			s = "S";
    			break;
    		case 3:
    			s = "H";
    			break;		
    		}
    		
    		return s;
    	}
    	
    	public String toString()
    	{
    		return (getRank() + setSuitAsString());
    	}	
    }
    Java Code:
    import java.util.*;
    
    public class Deck 
    {
    	private ArrayList<Card> deck = new ArrayList<Card>(52);
    	public Deck()
    	{
    		for(int s = 0; s < 4; s++){
    			for(int n = 0; n < 13; n++)
    			{
    				deck.add(new Card(n,s));
    			}
    		}
    		Collections.shuffle(deck);
    	}
    
    	public ArrayList<Card> getDeck() {
    		return deck;
    	}
    	
    	public ArrayList<Card> getCard()
    	{	
    		ArrayList<Card> tempCard = new ArrayList<Card>(0);
    		
    			tempCard.add(deck.remove(0));
    			
    		return tempCard;	
    	}
    }
    Java Code:
    import java.util.*;
    
    public class Player {
    
    	public ArrayList hand = new ArrayList(5);
    	int number;
    	
    	public void setCard(ArrayList newCard)
    	{
    		hand.add(newCard);		
    	}
    	
    	public void showHand()
    	{		
    		System.out.println(hand);	
    	}
    }

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    2. What is its complaint when you change hand to be ArrayList<Card>? Why is the setCard() method taking in an ArrayList rather than a Card by the way? Because I suspect that's your problem. That implies that the "hand" attribute is actually a List of Lists rather than a list of Cards.

    3. I think you've probably got a reasonable project on. And you seem to be taking on it in chunks, which is good. Each chunk is small enough (give or take) to be achievable. Expect to rework it over and over, though, as you pick up new techniques and better understanding of Java and OO in general. It's also something you can then attach other bits of Java to (ie Swing) once you feel confident.

    ETA: For the "hand" thing, I think the root is the getCard() method on Deck. That should return a Card, not a List of Cards.

Similar Threads

  1. Three classes help!!
    By arrech326 in forum New To Java
    Replies: 4
    Last Post: 11-24-2009, 06:34 AM
  2. Help with classes
    By gnarly hogie in forum New To Java
    Replies: 14
    Last Post: 10-10-2008, 02:29 PM
  3. Cant run my classes
    By Assaf A in forum Eclipse
    Replies: 1
    Last Post: 04-22-2008, 02:31 PM
  4. Help with classes
    By freswood in forum New To Java
    Replies: 5
    Last Post: 04-21-2008, 03:28 PM
  5. Using a JAR from other classes
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-02-2008, 07:08 PM

Tags for this Thread

Posting Permissions

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