Solved

Poker game in VB 2008

Posted on 2010-09-10
2
1,039 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

713 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