Solved

Poker game in VB 2008

Posted on 2010-09-10
2
1,034 Views
Last Modified: 2012-05-10
Hi everyone,

I am planning to make a Texas Holdem Poker game in VB. I am looking forward to some basic application where five card are selected at random and displayed on the screen and then users will have their two card in their hand (physically). Then the application will ask the users to enter their cards in hand and then the system calculated if they have won or not.

I have some idea about the basics like I will need 2 classes one for cards and one for the deck and two methods, one for shuffle and one for deal.
I also have all the card images etc available.

But before starting I was wonder if I could look at some sample codes. I am not sure where to start from.

Any help to guide me in the right direction will be highly appreciated.

Thanks
0
Comment
Question by:justind39
2 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 33649699
If you're going to do this right, you're going to want to have multiple classes.

Table:  holds people, public cards, etc.

People:  account info, username, private cards, etc

Deck - 52 cards.  shuffle will be a randomize function, put them on a stack or in a list randomly so that all 52 are contained.

Deal as per the rules, just taking off the front card and allocating it to the person.

If you are going to give them physical cards . . . what's to say I don't have aces all the time?  What's the finaly application?


           
0
 
LVL 20

Accepted Solution

by:
alainbryden earned 500 total points
ID: 33649850
I was helping a friend create a texas holdem game the other day so I'll help you out. The included code should be incredibly easy to translate from java pseudocode to VBA:

  • Define a deck of cards as a 4*13 2D boolean array, and a card object as a two integer pair.

  • As you randomly generate a card (S=rand(1,4) for suit and  V=rand(2,14) for value), you mark the appropriate array index  Deck[S-1][V-2] = true, and on future random generations, ensure that the  card has not already been drawn)

  • Have a function that sorts your hand as you did, and then checks for  hands from highest to lowest value based on flush, straight, pair  determinations. As soon as a winning is found, you can break and return  the appropriate multiplier.
  • When you create a hand, you should sort it in ascending order. A hand can be easily defined using two 5 element arrays. One array of face values (where 2=2, 3=3, ..., Jack=11, Queen=12, King=13, Ace=14). The other array is the suits (which can just be a single character or a number from 0 to 3)
Based on the above infrastructure, the following segments of code can be used to computer a hand:
hasFlush:
bool flush = true;
for( int i = 0; i < 4, flush == true; i++)
   if( suitArray[i] != suitArray[i+1] ) flush = false;
//We assume a flush until a test tells us otherwise.

hasStraight (requires valArray to be in ascending order):
bool straight = true;
for( int i = 0; i < 3, straight == true; i++)
   if( (valArray[i]+1) != valArray[i+1] ) straight = false;
if( straight == true && (valArray[3]+1) != valArray[4] && (valArray[3]+9) != valArray[4]) straight = false;
//Because there's a special case for a wheel straight the final test is done after the loop.

Count pairs, 3 of a kinds, and 4 of a kinds (requires valArray to be in ascending order):
int pairCount = 0, matchCount = 0, maxMatch = 0, maxPair = 0;
for( int i = 0; i < 4; i++)
{
   if( valArray[i] == valArray[i+1] )
   {
      if( matchCount == 0 ) pairCount++;
      matchCount++;
      maxMatch = matchCount > maxMatch ? matchCount : maxMatch;
      maxPair = valArray[i] > maxPair ? valArray[i] : maxPair;
   }
   else
      matchCount = 0;
}


Final Result:
if( straight == true && flush == true && valArray[0] == 10) //return Royal Flush
if( straight == true && flush == true) //return Straight Flush
if( maxMatch == 4) //return Four of a Kind
if( pairCount == 2 && maxMatch == 3) //return Full House
if( flush == true ) //return Flush
if( straight == true ) //return Straight
if( pairCount == 1 && maxMatch == 3) //return 3 of a Kind
if( pairCount == 2 && maxMatch == 2) //return 2 Pair
if( pairCount == 1 && maxMatch == 2 && maxPair >= 10) //return High Pair


Hope that's a solid start. Good Luck.

--
Alain
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now