Welcome to Dream.In.Code
Become a Java Expert!

Join 150,195 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,004 people online right now. Registration is fast and FREE... Join Now!




Determining Poker Hands - Conceptually

 
Reply to this topicStart new topic

Determining Poker Hands - Conceptually

prelic
13 Sep, 2008 - 11:46 AM
Post #1

New D.I.C Head
*

Joined: 13 Feb, 2007
Posts: 15


My Contributions
Alright, I have to write a program that parses playing cards from a file into 2 5-card poker hands, and determine which hand is better, and what it contains (flush, pairs, straight, etc). My original plan was to have a Hand class and a Card class. The card class was going to have a constructor that looked like Card(int rank, Suit suit), where suit was an enumerated type probably. Then the Hand class would be Hand(Card c1, Card c2, Card c3, Card c4, Card c5). Then, I could instantiate 2 Hand objects and invoke methods such as: Hand sort(), bool getPairs(), bool getStraight(). However, my problem is getting the classes started right, I'm not sure how to setup the classes so that it would let me setup hands like that. For example, this is how I wanted to set it up, but it doesn't like how I do this, any advice?

CODE
public class Card {
    
    Card(int the_rank, Suit the_suit)
    {
        rank = the_rank;
        suit = the_suit;
    }
    
    public enum Suit {CLUBS, HEARTS, SPADES, DIAMONDS }
    Suit suit;
    int rank;
}

public class Hand {

    Hand(Card first, Card second, Card third, Card fourth, Card fifth)
    {
        card1=first;
        card2=second;
        card3=third;
        card4=fourth;
        card5=fifth;
    }
    
    
    public static void main(String[] args)
    {
        Card card1=new Card(5, DIAMONDS);
    }
    
    Card card1, card2, card3, card4, card5;
}


User is offlineProfile CardPM
+Quote Post

pbl
RE: Determining Poker Hands - Conceptually
13 Sep, 2008 - 12:53 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Don't complicate your life for nothing

java

import java.util.*;

public class Poker {

static final String[] name = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
static final String[] color = {"Spade", "Heart", "Cub", "Diamond"};
Random r;
Poker() {
r = new Random();
Deck deck = new Deck();

ArrayList<Card> array = deck.getArray();

Hand hand1 = new Hand(array);
Hand hand2 = new Hand(array);

System.out.println("Hand1: \n" + hand1);
System.out.println("Hand2: \n" + hand2);
}
public static void main(String[] arg) {
new Poker();
}

// defines a card
class Card {
int rank;
int suit;

Card(int rank, int suit) {
this.rank = rank;
this.suit = suit;
}

public String toString() {
return "Rank: " + name[rank] + " Suit: " + color[suit];
}
}

// create a deck of card
class Deck {
// the 52 cards
Card[] cards;
// an arrayList to hold them for random retreiving
ArrayList<Card> array;

Deck() {
cards = new Card[52];
int k = 0;
for(int rank = 0; rank < 13; rank++) {
for(int suit = 0; suit < 4; suit++) {
cards[k++] = new Card(rank, suit);
}
}
}

// get a fresh arrayList of 52 cards
ArrayList<Card> getArray() {
ArrayList<Card> array = new ArrayList<Card>();
for(int i = 0; i < 52; i++)
array.add(cards[i]);
return array;
}
}

// a hand of 5 cards randomly selected
class Hand {
Card[] cards;

Hand(ArrayList<Card> array) {
cards = new Card[5];
// build deck of 5 from random
for(int i = 0; i < 5; i++)
cards[i] = array.remove(r.nextInt(array.size()));
}

public String toString() {
String str = "";
for(int i = 0; i < 5; i++) {
str += "Card " + (i+1) + " " + cards[i] + "\n";
}
return str;
}

}
}



This post has been edited by pbl: 14 Sep, 2008 - 06:07 PM
User is offlineProfile CardPM
+Quote Post

prelic
RE: Determining Poker Hands - Conceptually
14 Sep, 2008 - 06:09 PM
Post #3

New D.I.C Head
*

Joined: 13 Feb, 2007
Posts: 15


My Contributions
Thanks a ton for the help...I spent a few hours today trying to stick to your design, and I think I came up with a pretty good model, but I'm getting an error: "ParseSuit muse return a type of Poker.Suit". I've been staring at this code for a while, and as far as I can tell, all control paths do return a suit? Idk..
CODE



import java.io.*;
import java.util.*;

public class Poker {

    public enum Suit {
        Clubs, Diamonds, Hearts, Spades, None
    }

    Poker() {
    }

    Poker(Card hand[]) {

    }

    public static Suit parseSuit(String line) {
        if (line == "" + Suit.Clubs)
            return Suit.Clubs;
        else if (line == "" + Suit.Diamonds)
            return Suit.Diamonds;
        else if (line == "" + Suit.Hearts)
            return Suit.Hearts;
        else if (line == "" + Suit.Spades)
        ;
        else
            return Suit.None;
    }

    public static void main(String[] args) throws IOException {
        Poker pokerGame = new Poker();
        Card hand[] = new Card[5];
        File in = new File(
        "/Users/philrelic/Documents/workspace/PokerHand/src/Hands.txt");
        BufferedReader reader = new BufferedReader(new FileReader(in));

        String line = reader.readLine();
        String name = line.substring(0, line.indexOf(" ") - 1);

        line = line.substring(line.indexOf(" "));
        line = line.replaceAll(" of ", "");
        line = line.replaceAll(",", "");

        for (int x = 0; x < 1; x++) {
            int rank = Integer.parseInt("" + line.charAt(1));
            Suit suit = parseSuit(line.substring(1, line.indexOf(" ")));
            hand[x] = pokerGame.new Card(rank, suit);
        }
    }

    // defines a card
    class Card {
        int rank;
        Suit suit;

        Card(int rank, Suit suit) {
            this.rank = rank;
            this.suit = suit;

        }

    }
}



This post has been edited by prelic: 14 Sep, 2008 - 06:12 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Determining Poker Hands - Conceptually
14 Sep, 2008 - 06:49 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
You have a return missing for Spades
If the line is not correct I would rather throw an exception.... you are in deep trouble

CODE

   public static Suit parseSuit(String line) {
        if (line == "" + Suit.Clubs)
            return Suit.Clubs;
        else if (line == "" + Suit.Diamonds)
            return Suit.Diamonds;
        else if (line == "" + Suit.Hearts)
            return Suit.Hearts;
        else if (line == "" + Suit.Spades)
            return Suit.Spades;
        else
            throw new IllegalStateException("Parse suit called with: " + line);
    }


This post has been edited by pbl: 14 Sep, 2008 - 06:51 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:33AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month