Results 1 to 7 of 7

Thread: Poker Game

  1. #1
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    64
    Rep Power
    0

    Default Poker Game

    I found this website https://projecteuler.net/problem=54 and I decided to tackle a few problems for fun. And I need a little advice. I am reading a txt file that has the following format:
    Java Code:
    TH 8H 5C QS TC 9H 4D JC KS JS
    7C 5H KC QH JD AS KH 4C AD 4S
    5H KS 9C 7D 9H 8D 3S 5D 5C AH
    6H 4H 5C 3H 2H 3S QH 5S 6S AS
    TD 8C 4H 7C TC KC 4C 3H 7S KS
    Each line represents two set of hands and 5 cards in each hand. At the moment I am using two inner classes and trying to split them up. I read the file and place all 1000 values into a ArrayList then I do the following to split them up.
    Java Code:
    void getHands()
    	{
    		playerOne p1=new playerOne();//inner class
    		playerTwo p2=new playerTwo();//inner class
    		
    		int p2Count=0;
    		int p1Count=0;
    		
    		//breaking up ArrayList to playOne and PlayerTwo each should have
    		//5 cards in each hand
    		for(String s: pokerHands)
    		{
    
    			//used to split up the array to five cards per player
    			//we iterate giving each player five cards
    			p1Count++;
    			p2Count++;
    			
    			if(p1Count < 5)
    			{
    			p1.setHands(s);
    			}
    			else if(p2Count > 5 && p2Count < 10)
    			{
    			p2.setHands(s);
    			}
    			else if(p1Count ==4 && p2Count==9)
    			{
    				p1Count =0;
    				p2Count =0;
    			}
    			
    		}
    		System.out.println("Player one Cards");
    		p1.testArrays();
    		System.out.println("Player Two Cards");
    		p2.testArrays();
    	}
    A example of one of the inner classes are here:
    Java Code:
    class playerOne 
    	{
    		ArrayList<String> p1Hand = new ArrayList<String>();
    		
    		void setHands(String s)
    		{
    			if(p1Hand.size() < 5)
    			{
    			p1Hand.add(s);
    			}
    			else
    			{
    				p1Hand = new ArrayList<String>();
    				p1Hand.add(s);
    			}
    		}
    		
    		void testArrays()
    		{
    			for(String s: p1Hand)
    			{
    				System.out.println(s);
    			}
    
    		}
    	}
    However they are not being split up. Actually I am not even getting all the values into the array. This is the output I am getting when I run testArrays for player1 and 2:
    Java Code:
    Player one Cards
    
    8C TS KC 9H 4S 7D 2S 5D 3S AC
    5C AD 5D AC 9C 7C 5H 8D TD KS
    3H 7H 6S KC JS QH TD JC 2D 8S
    Player Two Cards
    TH 8H 5C QS TC 9H 4D JC KS JS
    7C 5H KC QH JD AS KH 4C AD 4S
    5H KS 9C 7D 9H 8D 3S 5D 5C AH
    6H 4H 5C 3H 2H 3S QH 5S 6S AS
    TD 8C 4H 7C TC KC 4C 3H 7S KS
    Any suggestions?
    Last edited by jocdrew21; 06-08-2014 at 02:07 PM.

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

    Default Re: Poker Game

    Why all that juggling in the playerOne (and playerTwo?) class; a player is dealt five cards maximum; no need to write defensive code for that in the playerOne class again; the dealer took care of it already ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    64
    Rep Power
    0

    Default Re: Poker Game

    Yea I was over engineering that one a little bit. I looked closer at the problem and thought I could try something like this:
    Java Code:
    if(loop % 5==0)// this could get the condition for making a new array every time this is true

  4. #4
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    64
    Rep Power
    0

    Default Re: Poker Game

    I tried this and it is still not working. Am I making a new array correctly?

    Java Code:
    	void separateHand()
    	{
    		//ArrayList<String> hands = (ArrayList<String>) pokerHands.subList(0,5);
    		ArrayList<String>hands = new ArrayList<String>();
    		int count=0;
    		/*pokerHands has all the variables from the file in lines of ten
    		strings reading like:
    		QC KC 3S JC KD 2C 8D AH QS TS
            AS KD 3D JD 8H 7C 8C 5C QD 6C 
            Below should break them up into separate arrays of 5 cards
    		 * */
    		 
    		for(String s : pokerHands)
    		{
    			count++;
    			if(count % 5 == 0)
    			{
    				hands = new ArrayList<String>();
    				hands.add(s);
    			}
    			else
    			{
    				hands.add(s);
    			}
    			
    		}
    		
    		for(String list: hands)//Testing if it worked
    		{
    			System.out.println(list);
    		}
    	}

  5. #5
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: Poker Game

    on line 18,19 each time you point your hands list to a new empty list and you add one element and thus lose any elements it contained before.

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

    Default Re: Poker Game

    Sure, you're creating a new 'hands' ArrayList correctly but you're forgetting the old one. If 'pokerHands' is an array of 10 Strings (made bythe split() method?) why not deal them per hand, like this:

    Java Code:
    private void deal(ArrayList<String> hand, int index, String[] cards) {
       for (int i= 0; i < 5; i++, index++) hand.add(cards[index]);
    }
    You can call the method above twice; once for each player; all you need is the player's hand.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    64
    Rep Power
    0

    Default Re: Poker Game

    This is a little embarrassing but I do not quite get it.

    Java Code:
    for (int i= 0; i < 5; i++, index++)
    The index++ at the end made me scratch my head, I am intrigued. I understand what you did with your parameters but why did my method not work?

    Sure, you're creating a new 'hands' ArrayList correctly but you're forgetting the old one
    How? Was I over writing or dereferencing my array?

    I am more familiar with c++ and I did this program in using that language using the boost library and I was a little shocked that I am having a hard time dealing with this in Java. So far I have found Java has a library or method for pretty much everything (pretty cool)....

    That being said I noticed the format of the txt file was the following:

    8C TS KC 9H 4S 7D 2S 5D 3S AC

    I checked to make sure it was not a mega string. Therefore 8C is its own string and it must have a \n at the end. Which made me think I would try this:

    Java Code:
    while((hand=buffer.readLine()) != null)
    		{
    			//read from file and sets all the poker cards types
    			//to an ArrayList, the file reads (TD 8C 4H 7C TC KC 4C 3H 7S KS)
    			//on each line showing player 1 and player 2's cards
    			array = hand.split("\n");
    			pokerHands.add(hand);
    			
    		}
    		buffer.close();
    The output was one single line, puzzled me too. It is amazing how programming can make me feel completely inadequate and intrigued all at the same time.

Similar Threads

  1. dice poker
    By saina in forum New To Java
    Replies: 1
    Last Post: 04-04-2012, 10:01 AM
  2. Trying to make a Poker Dice game..!
    By karibo in forum New To Java
    Replies: 1
    Last Post: 12-08-2011, 04:06 PM
  3. Texas Hold'em Poker
    By JohnPringle83 in forum Java Gaming
    Replies: 0
    Last Post: 05-05-2011, 05:40 AM
  4. Please help Me out with this poker Problem
    By ckrisasa in forum Networking
    Replies: 8
    Last Post: 12-03-2009, 01:59 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
  •