Poker

Hello,
Is there any Poker game written in Java that has source code along with for analysis.

google -> 2nd hit -> sourcecode

For your amusement: My Poker4k sourcecode (screens: click click click click, JAR, JNLP)

Not bad though…!! ;D
but still the example is quiet simple.
I was looking for a combination of a complex codes and a little bit of Shuffling + Distribution Algorithm to go along with.

Help required.

This should probably bein clueless newbies but…

Quick deck shuffling algorithym:


(1)Allocate an integer array with length 52.
(2) Loop from 0 to 51 adn set deck[i] = i
(3) Loop for some number of times (say 50) and do the following (swap pairs of cards):
   x = random from 0  to 51
   y=  random from 0 to 51
   temp = deck[x]
   deck[x]=deck[y]
   deck[y]=temp

Well, it could be much more efficient:


Random r = new Random();
for(int i=0; i<52; i++)
{
    int from = i;
    int to = (i + 1) + r.nextInt(52 - i - 1);

    swap(from, to);
}

It handles the remaining cards as a pool to take cards from.

If you are only drawing cards, not peeking ahead or inserting any, then an easy option is to implement the deck as a list of cards and yield a random one on draw. E.g.:


class Deck {
  private List<Card> theDeck;
  
  public Card draw() {
    if (theDeck.size()==0) throw NoSuchElementException();
    else return theDeck.remove(Math.random() * theDeck.size());
  }
}

From the client’s point of view (that is, according to the object’s interface), the deck is shuffled, and in an OO system, that’s all that matters :slight_smile:

heh by quick I meant “im dashing this off quick.”

Im sure there are more efficient means. Im not sure its worth a lot of effort to find them 8)

Collections.shuffle(deck);

try reading from page 17 for the whole shabang.

http://64.233.183.104/search?q=cache:Wq1IsrnfDNsJ:developers.sun.com/learning/javaoneonline/2004/corej2se/TS-2569.pdf+Java+5.0+deck&hl=en&ct=clnk&cd=3
http://developers.sun.com/learning/javaoneonline/2004/corej2se/TS-2569.pdf