Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / card game logic

Author
Message
badrock
17
Years of Service
User Offline
Joined: 1st Mar 2007
Location: Missouri, USA
Posted: 31st Dec 2007 06:45 Edited at: 31st Dec 2007 06:47
Hi, I was just wondering if anyone had any examples or tips for creating the logic for a card game. Mainly I am trying to find the best way to create the 52 cards and a way to select random cards with no duplicates.

My idea so far is to create 4 arrays (one for each suit), then each array be the 13 possible cards. I could have a function select a random suit then a random card from that suit. However I am stumped when I start thinking about how to do this without duplicates. Maybe keep drawn cards in a separate array that I could check against every time I try to draw a random card?

Any pointers would really be appreciated!

badrock
badrock
17
Years of Service
User Offline
Joined: 1st Mar 2007
Location: Missouri, USA
Posted: 31st Dec 2007 07:12
Just wanted to add what I have come up with so far.



badrock
badrock
17
Years of Service
User Offline
Joined: 1st Mar 2007
Location: Missouri, USA
Posted: 31st Dec 2007 08:48
Well I could not sleep so I spent some time working on this and came up with a solution that seems to do the trick.



Any suggestions on tweaking this would be welcomed. Is there a better way for me to reset the drawn() array to all 0's? Setting each value individually looks so ugly, but it's the only way I knew.

badrock
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 31st Dec 2007 12:08 Edited at: 31st Dec 2007 12:09
look into for ... next loops and save your fingers some wear and tear.

like this to set your deck up:



if you want to set each to zero, change the line:

deck(x,y) = y

to read:

deck(x,y) = 0

Virtual Nomad
AMD XP 1800+ (~1.6 Ghz) / 1.5 GB RAM
ATI Radeon 8700LE 128 MB / Windows XP
Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 2nd Jan 2008 17:12 Edited at: 2nd Jan 2008 17:14
In general:
I'm not saying that your method can't be used in any way, but you might consider to use a one dimensional array and types(DBP) to determine which card is at which place easily.

For example:
if you have (DBP):
(remember there are 52 card in one deck)
The Deck(nr).suit will hold the suit, and Deck(nr).index will hold the index. That way, it's easy to determine the 9th card, or the 1st card either way.

Or in DBC just:


In DBC, we cannot use the types, so we will have to give each card a number.
1 - 13: Spade (ace through king)
14 - 26: Heart (ace through king)
27 - 39: Diamond (ace through king)
40 - 52: Club (ace through king)

Conversion between them would be:




If we now give Deck(30).suit a value of 3 and Deck(30).index a value of 13, that position would hold the king of Diamond.
In the case of DBC, we would have to store 39 ((3-1)*13 + 13) in Deck(30).

Shuffling:
The problem with your code is that you can't predict when the randomized value actually will be one that isn't drawn yet.

When shuffling, I'd advice to use another method for better results.

The algorithm is simple

As many times as you want:
swap the place of two times.

I won't give any code, since this is probably a learning experience.

It's the programmer's life:
Have a problem, solve the problem, and have a new problem to solve.
badrock
17
Years of Service
User Offline
Joined: 1st Mar 2007
Location: Missouri, USA
Posted: 3rd Jan 2008 00:33 Edited at: 3rd Jan 2008 00:34
Thank you very much for taking the time reply Sven & Nomad, I really do appreciate it.

@sven: I agree that your logic is much easier to work with then my first solution, I will work on going through your points and see how I do with them.

badrock
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 3rd Jan 2008 06:22 Edited at: 3rd Jan 2008 06:24
i had done a bit of searching on card logic here when i saw this post and most examples showed naming cards in the following convention:

AH = ace of hearts, 2H = of hearts, 3H and so on...

but i'm not sure why it was a benefit to do it that way, but you might want to look into it further. depending on the card game, or games, you wanted to make, i suspect you'd have to do some (albeit quick) parsing to get suit and numerical values anyway.

point is, do you have a direction for this particular card application? it seems you're going to use a traditional card deck, that much is known. if it will involve comparing and matching suits (ie, a "flush") and/or sequences (like straights, "2,3,4"), and/or numerical value matching (ie, a pair of 10's or a set of 5's), or combinations thereof, as i'm pretty sure it will, then that should be accounted for early on in the development. will "jokers" be an option? will you draw 1 card? 3 at a time? how about trumps? re-shuffle options (mid-game), etc.

anyway, just making sure you're putting all your cards on the table and looking at them before really digging in (pun intended, and almost appropriately ).

good luck!

Virtual Nomad
AMD XP 1800+ (~1.6 Ghz) / 1.5 GB RAM
ATI Radeon 8700LE 128 MB / Windows XP
badrock
17
Years of Service
User Offline
Joined: 1st Mar 2007
Location: Missouri, USA
Posted: 3rd Jan 2008 06:40
Hi Nomad, thank you for the insightful post.

Quote: "point is, do you have a direction for this particular card application? it seems you're going to use a traditional card deck, that much is known. if it will involve comparing and matching suits (ie, a "flush") and/or sequences (like straights, "2,3,4"), and/or numerical value matching (ie, a pair of 10's or a set of 5's), or combinations thereof, as i'm pretty sure it will, then that should be accounted for early on in the development. will "jokers" be an option? will you draw 1 card?"


Yeah I do have a general direction for the game. The gist is it is a arcade-action game where you will collect cards along the way, and for every 5 cards you collect you will get a bonus score based on the grade of the poker hand (i.e. 1000 points for a full house or 100 points for two pair). There will not be any jokers or drawing involved. I was thinking that every time five cards have been collected, the deck would be "shuffled" and all cards would be available until the next bonus is collected.

I have been thinking about this quite a bit also and both naming methods seem to have pros and cons. No matter which I choose I think it will be fairly complex to evaluate 5 cards to determine the highest qualifying poker hand.

I will try some more forum searching and see if I can find anything regarding the AH, 2H, 3H etc naming system you mentioned. I am still in the planning stage so it is by no means to early to change around how I will be doing this I'm glad you brought this up though, I really do need to make sure I have this straightened out before starting.

Thanks again for your input, greatly appreciated.

Dark Basic PRO / CodeSurge
Windows 2000 SP4 / PIII 1Ghz / 256mb
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 3rd Jan 2008 09:11
Quote: "arcade-action game where you will collect cards along the way...bonus score based on the grade of the poker hand "


excellent!

Virtual Nomad
AMD XP 1800+ (~1.6 Ghz) / 1.5 GB RAM
ATI Radeon 8700LE 128 MB / Windows XP
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 11th Jan 2008 17:07 Edited at: 11th Jan 2008 17:08
pcRaider
17
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 11th Jan 2008 17:13

Login to post a reply

Server time is: 2024-11-25 10:48:49
Your offset time is: 2024-11-25 10:48:49