gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Variables - I need some opinions on holding information

#98419 - psycorpse - Tue Aug 15, 2006 4:06 am

So the subject doesn't tell to much. I am creating a texas holdem game for the DS. I am running into some problems (programmers block).... Anyways I am getting to the point were I am evaluating the hands. I guess I need to hold the values of each persons hand and then determine who has the better hand. Currently I am checking for things like pairs, trips, quads, flushes. I have created a check for straights just yet. But I guess what I am trying to get at is what kind of variable should I use to hold this information. I would like to display all the hands that are in play at the end and spit out the winner. Well I know you will have questions so please ask!

Thanks,
Psycorpse (Novice C++ programmer)

#98671 - Primetime00 - Wed Aug 16, 2006 10:10 pm

I would use a struct to hold the a card's value and suit. (2 ints or chars)
then create an array of these structures for each hand.

Or create an array of size 52 for the whole deck and point each card in a players hand to one of these 52 array elements.

-Ryan

#98678 - keldon - Wed Aug 16, 2006 10:55 pm

Before you go about making a scoring scheme you might want to learn a little about the statistics involved. Search texas holdem statistics on wikipedia, my net connection is quite crappy right now for some reason.

preflop: You are dealt your pocket cards and you have no idea what cards may be flopped. But there are pre defined statistcs as the the probability that there will be a stronger hand than yours come the river.

flop: first three cards are turned over. Now you have more cards to factor into your statistics, although nothing is definite until the river.

But poker strategy is far more complex than simple statistics and the strength of your hand. But going back to the strength of your hand it is as 'simple' as calculating the possibility of a stronger hand than yours given the cards you know about and the cards you don't. I would suggest playing poker online for a few weeks whilst reading up on the strategy first, and then maybe you will have a much better idea of how to code ai and value hands.

#98681 - psycorpse - Wed Aug 16, 2006 11:02 pm

Primetime00 wrote:
I would use a struct to hold the a card's value and suit. (2 ints or chars)
then create an array of these structures for each hand.

Or create an array of size 52 for the whole deck and point each card in a players hand to one of these 52 array elements.

-Ryan


Thanks for your reply.

Your suggestion is very close to what I already have. I have a class called cards. This consists of 2 arrays (deck[52], deck_lookup[52][4])
My lookup table also holds gfx and suit ids for the oam along with a value and suit. I guess when posting the question it wasn't totally clear.

Ok let me try again.

Here is the players cards and flop turn and river
P1: As8c
P2: 2c2d
P3: Jh4s
P4: Ks5h

Flop:
Kd, Kh, 4c

Turn:
6h

River:
7d


I get this far and in a loop I can print out this
player 1 has one pair - Kings
player 2 has two pair - Kings and twos
player 3 has two pair - Kings and fours
player 4 has a three of a kind - Kings


The problem that I am still having is how to hold the current winner and then check to see if the next person has a better hand or not. If they do then update a winner variable.


Thanks,
Mike

#98687 - keldon - Wed Aug 16, 2006 11:26 pm

Create a hand comparison method.

Code:
currentWinner = null;
cur = firstPlayer;
for each player {
   if ( currentWinner == null ) currentWinner = cur;
   else if ( ! currentWinner.handIsBetter ( cur ) ) currentWinner = cur;
}

#98776 - psycorpse - Thu Aug 17, 2006 2:59 pm

Keldon,

Thanks for your replies. I guess we were posting at the same time. I play poker quite a bit and I have a good understanding of most of the strengths of hands. I will give the method idea a shot.

Thanks,
Mike

#99064 - king501 - Sat Aug 19, 2006 11:49 am

I have a question for a similar subject.

so far when I'm using for-loop I'm always declaring variables of int type. But in some contexts is it better to use shorter types like u8 to avoid waste of memory?

ex:

for(u8 i=0; i<256; i++)

also, is there a way to use a smaller type like u4 for instance?

sorry if my questions look stupid.

#99079 - Sausage Boy - Sat Aug 19, 2006 1:51 pm

Using u8's in a for loop makes it slower, or so I've heard. Stay with int's, the 3 bytes you save aren't really worth it are they? Besides, it probably gets optimized as a register anyways, which means it doesn't take any ram at all.

u4's are impossible to use, since most CPU's address their memory in bytes (u8). If you need a 4 bit value for some wierd purpose, you can use an u8 and mask it every time you use it (your_variable & 0x0f) so only the last four bytes are accessed.
_________________
"no offense, but this is the gayest game ever"

#99084 - Cearn - Sat Aug 19, 2006 2:16 pm

king501 wrote:
I have a question for a similar subject.

so far when I'm using for-loop I'm always declaring variables of int type. But in some contexts is it better to use shorter types like u8 to avoid waste of memory?

ex:

for(u8 i=0; i<256; i++)

also, is there a way to use a smaller type like u4 for instance?

sorry if my questions look stupid.


In nearly all cases, local variables will go directly into CPU registers (which are 32bit), not in memory, so there's no cost involved. In fact, not using ints will cost a little memory, because that's the only size the CPU can work with internally, and has to add instructions to keep them non-int sized. Depending on how good your compiler is, this could be two extra instructions for every operation you do with the non-int variable. The memory cost is actually not much of a problem because the total code-size is dwarfed by the total data-size, but the extra instructions can mean a 10% to 50% decrease in speed.

Now, it isn't quite true that ints will always be better (global variables come to mind), but it's a good starting point.