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

    Default Help with arrays please.

    So I am trying to make a poker game and have already scrapped and starting rewriting my almost finished program because it was the first time coding a program and I did some pretty ugly things in there. But I am remaking it now and have a slight problem.

    I create my two hands and sort out the cards no problem, but when I try to trade a card and get a new one it isn't working out so well, my comments in the code should help explain a little better. I have been working in case 2 of my Switch statement.

    Java Code:
    import static java.lang.System.out;
    import java.util.*;
    
    public class Poker
    {
    	
    	public static void main(String args[])
    	{
    		
    		int player1Score = 0;  // Scores are given to each player and top score ultimately wins.
    		int player2Score = 0;  // Scores are given to each player and top score ultimately wins.
    		String[] player1Hand = new String[5];		
    		String[] player2Hand = new String[5];
    		Card card = new Card();
    		Scanner myScanner = new Scanner(System.in);
    	
    		
    		Random myRandom = new Random();
    		int randomCard;
    
    		boolean nothing,pair,twoPair,threeOfAKind,straight,flush,fullHouse,fourOfAKind,straightFlush;  //Used for special modifiers when determining the winning hand.
    		nothing=pair=twoPair=threeOfAKind=straight=flush=fullHouse=fourOfAKind=straightFlush = false;  //Used for special modifiers when determining the winning hand.
    		
    		for(int i = 0; i < 5; i++)
    		{
    			player1Hand[i] = card.deck[card.hand1[i]];
    			player2Hand[i] = card.deck[card.hand2[i]];			
    			
    		}
    		
    		for(int i = 0; i < 5; i++)
    		{
    		out.print(player1Hand[i] + " ");
    		}
    		out.println();
    		
    		out.println("Select card to trade in");
    		
    		int input = myScanner.nextInt();
    		
    		switch (input)
    		{
    		case 1: 
    			randomCard = myRandom.nextInt(52);
    			card.hand1[input - 1] = randomCard;
    			player1Hand[input - 1] = card.deck[card.hand1[input - 1]];
    			break;
    		case 2: 
    			randomCard = myRandom.nextInt(52);  //Currently not using for debugging purposes.
    			card.hand1[input - 1] = 51; //This makes card.hand1[1] = 51
    			System.out.println(card.hand1[input - 1]);  //This shows that card.hand1[1] does in fact = 51
    			System.out.println(card.deck[card.hand1[input - 1]]);  //This shows that card.deck[51] does in fact = KS
    			Arrays.sort(card.hand1);  //This SHOULD be sorting the 51 into cand.hand1[4] since 51 is the highest number it can be.
    			System.out.println(card.hand1[input - 1]);  //This proves that something went wrong.
    			player1Hand[input - 1] = card.deck[card.hand1[input - 1]];  //This is suppose make player1Hand[1] = KS  Which it obviously doesn't
    			break;
    		case 3: 
    			randomCard = myRandom.nextInt(52);
    			card.hand1[input - 1] = randomCard;
    			player1Hand[input - 1] = card.deck[card.hand1[input - 1]];
    			break;
    		case 4: 
    			randomCard = myRandom.nextInt(52);
    			card.hand1[input - 1] = randomCard;
    			player1Hand[input - 1] = card.deck[card.hand1[input - 1]];
    			break;
    		case 5: 
    			randomCard = myRandom.nextInt(52);
    			card.hand1[input - 1] = randomCard;
    			player1Hand[input - 1] = card.deck[card.hand1[input - 1]];
    			break;		
    		}
    		
    		for(int i = 0; i < 5; i++)
    		{
    		out.print(player1Hand[i] + " ");
    		}
    	}
    }
    Java Code:
    import java.util.Arrays;
    import java.util.Random;
    
    
    public class Card 
    {
    		String[] number = {"A","2","3","4","5","6","7","8","9","10","J","Q","K",};
    		String[] suit = {"D", "H", "C", "S"};
    		String[] deck = new String[52];	
    		int[] hand1 = new int[5];  //Hand for player 1
    		int[] hand2 = new int[5];  //Hand for player 2 
    		int randomCard;
    		int j = 0;
    		
    		Random myRandom = new Random();  //Setting up the random util
    			
    {		
    	    //Creating the deck int j is the counter
    		for(int n = 0; n < 13; n++)
    		{
    			int s = 0;
    			for(; s < 4; s++)
    			{
    				deck[j] = number[n] + suit[s];
    				j = j + 1;
    			}
    		}
    
    		for(int i = 0; i < 5; i++)
    		{
    			randomCard = myRandom.nextInt(52);
    			hand1[i] = randomCard;
    			
    			randomCard = myRandom.nextInt(52);
    			hand2[i] = randomCard;
    		}
    		
    		Arrays.sort(hand1);
    		Arrays.sort(hand2);	
    		
    }
    }
    //Example output, refer to comments at swtich case 2 for the explanation of what's going on here.
    4S 7D 7H 9D 9C
    Select card to trade in
    2
    51
    KS
    25
    4S 7H 7H 9D 9C

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

    Default

    Bump, anyone please?

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

    Default

    You do have some confusion here. Your Card class should know how to model one Card, and one Card only. It should not know anything about hands or decks. It should not have any randomness to it -- there is nothing random about a single card, after all. It is what it is from the moment it rolls off the printing press until the moment it is destroyed. It should have a constructor that requires a rank and a suit and it should have getter methods to return those.

    You should have a separate Deck class that models a deck of cards. It should have a shuffle() method and a deal() method that returns one Card off the top. It should know (and have methods to tell) how many total cards are in the deck, the index of the card that will be dealt next (which is the same as the number of cards that have already been dealt), and how many cards are remaining.

    You may find it useful to have a Hand class and a Player class, but a hand could also be a simple array or an ArrayList. But since you say it's a poker game you're writing, it could be very useful to have a PokerHand class that knows something about the rules of poker, and has methods that take those rules into account. A Player class would track a player's chip count, their current bet, and other such things.

    Don't keep all of your code crammed into the main() method. Since I am something of a fanatic about using singular nouns for class names, I would call your main class PokerGame, and my main() would look something like this:
    Java Code:
            public static void main(String[] args) {
                    new PokerGame().run();
            }
    Then my run() method would probably be something like this:
    Java Code:
            public void run() {
                    initializePlayers();
                    chooseStartingDealer();
                    while (!gameOver()) {
                            playHand();
                    }
            }
    Each of those methods would be further decomposed so that each method is simple and clear.

    Give it another try and show us what you come up with. Good luck!

    -Gary-

Similar Threads

  1. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  2. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  3. Help with arrays!
    By lilac87 in forum New To Java
    Replies: 2
    Last Post: 07-28-2009, 04:57 AM
  4. A little help with arrays..
    By zeppelin in forum New To Java
    Replies: 8
    Last Post: 01-05-2009, 12:33 PM
  5. Help on Arrays...
    By cuellar14 in forum New To Java
    Replies: 4
    Last Post: 07-25-2008, 08:16 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
  •