Poker help
Author |
Message |
viper012289
|
Posted: Fri May 05, 2006 10:52 pm Post subject: Poker help |
|
|
i am making poker for a final project....i have a program that defines each card in an array and it randomly picking 5 cards using randint...and i was wondering how i would have it pick the same card only once |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
TheOneTrueGod
![](http://www.drmcninja.com/images/mcninjab3.jpg)
|
Posted: Fri May 05, 2006 11:09 pm Post subject: (No subject) |
|
|
Hello. It would greatly aid those trying to help you if you posted what you've tried so far, as well as some sample code of what you've done. I will try and do the best that I can without that, but in the future, post whatever code you've done so far. (Or people may be unwilling to help)
Theres several ways you can do this, but I will tell you my favourite way.
Basically, each card can be represented by a single integer, from 0 to 51.
If you equate the suit to a value from 0 to 3, you can get the suit by dividing the card's number by 13.
If you do something like
Spades = 3
Diamonds = 2
Clubs = 1
Hearts = 0
then you have an easy classification system as well, as spades are worth more than diamonds in this case.
Think about why this will work now,
any number from 0 to 12 divided by 13 (and rounded down, as div does) will give you the answer 0
any number from 13 to 25 will get you 1
etc...
The value can be derived by using the mod command. Any number from 0 to 12 mod 13 will give you that number. Any number from 13 to 25 will give you that number - 13 (Which will give you a number between 0 and 12)
If you code it as:
0 = 2
1 = 3
2 = 4
...
11 = King
12 = Ace
Then displaying these is easy.
Now, with this background info, you can get on to the actual randomizing thing (Assuming you havn't gotten bored with this post and given up).
Now you can create an array from 0 to 51, and store that element's number in that element. (Eg element zero will contain 0, element one will contain 1, etc...)
Now, you can go through this "virtual deck" and randomize where each card should be.
Once you have done this, you can simply "pop" off the top five values, and into the players hand. Keep track of what position you are at in the deck, and itll all work out fine.
Note: I'd post some sample code, but since you didn't give me any, it leads me to believe that you just want someone to do the work for you, which I'm not going to do. If you give me what you've done so far, then I can help you further. |
|
|
|
|
![](images/spacer.gif) |
richcash
|
Posted: Sat May 06, 2006 12:21 am Post subject: (No subject) |
|
|
WOW! That really is a good way of doing it. Never thought like that. This is how I do it :
var value : array 1 .. 13 of string := init ("2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace")
var suit : array 1 .. 4 of string := init ("Diamonds", "Clubs", "Hearts", "Spades")
var deck : array 1 .. 13, 1 .. 4 of int
var v, s : int
for m : 1 .. 13
for n : 1 .. 4
deck (m, n) := 1
end for
end for
proc draw
loop
randint (v, 1, 13)
randint (s, 1, 4)
exit when deck (v, s) = 1
end loop
deck (v, s) := 0
end draw
%actual program - draw five random non-repeating cards
for n : 1 .. 5
draw
put value (v), " ", suit (s)
end for |
|
|
|
|
![](images/spacer.gif) |
NikG
|
Posted: Sat May 06, 2006 1:52 am Post subject: (No subject) |
|
|
richcash, that way works too, but then you'd need extra code to find the value of each card because you've stored each value as a string.
TheOneTrueGod's helps with that because as he says, 2=0, 3=1... ace=12.
Another option is to create your own variable types. This can help you cleanly keep track of the value, the suit, and whether it's been drawn or not. code: | type Card :
record
Value : int
Suit : string
Drawn : boolean
end record |
So all you need is an array of 51 elements, each of type Card. |
|
|
|
|
![](images/spacer.gif) |
viper012289
|
Posted: Sat May 06, 2006 9:52 am Post subject: (No subject) |
|
|
hey thanks for the help so far but i would like to keep it somewhat similar to the way i have it so it would that it would be easy for me to check for hands like straight flushes etc.
here is the code i have so far
code: |
var cards : array 0 .. 52 of int
cards (0) := Pic.FileNew ("images/cardback.bmp")
cards (1) := Pic.FileNew ("images/2c.bmp")
cards (2) := Pic.FileNew ("images/2d.bmp")
cards (3) := Pic.FileNew ("images/2h.bmp")
cards (4) := Pic.FileNew ("images/2s.bmp")
cards (5) := Pic.FileNew ("images/3c.bmp")
cards (6) := Pic.FileNew ("images/3d.bmp")
cards (7) := Pic.FileNew ("images/3h.bmp")
cards (8) := Pic.FileNew ("images/3s.bmp")
cards (9) := Pic.FileNew ("images/4c.bmp")
cards (10) := Pic.FileNew ("images/4d.bmp")
cards (11) := Pic.FileNew ("images/4h.bmp")
cards (12) := Pic.FileNew ("images/4s.bmp")
cards (13) := Pic.FileNew ("images/5c.bmp")
cards (14) := Pic.FileNew ("images/5d.bmp")
cards (15) := Pic.FileNew ("images/5h.bmp")
cards (16) := Pic.FileNew ("images/5s.bmp")
cards (17) := Pic.FileNew ("images/6c.bmp")
cards (18) := Pic.FileNew ("images/6d.bmp")
cards (19) := Pic.FileNew ("images/6h.bmp")
cards (20) := Pic.FileNew ("images/6s.bmp")
cards (21) := Pic.FileNew ("images/7c.bmp")
cards (22) := Pic.FileNew ("images/7d.bmp")
cards (23) := Pic.FileNew ("images/7h.bmp")
cards (24) := Pic.FileNew ("images/7s.bmp")
cards (25) := Pic.FileNew ("images/8c.bmp")
cards (26) := Pic.FileNew ("images/8d.bmp")
cards (27) := Pic.FileNew ("images/8h.bmp")
cards (28) := Pic.FileNew ("images/8s.bmp")
cards (29) := Pic.FileNew ("images/9c.bmp")
cards (30) := Pic.FileNew ("images/9d.bmp")
cards (31) := Pic.FileNew ("images/9h.bmp")
cards (32) := Pic.FileNew ("images/9s.bmp")
cards (33) := Pic.FileNew ("images/Tc.bmp")
cards (34) := Pic.FileNew ("images/Td.bmp")
cards (35) := Pic.FileNew ("images/Th.bmp")
cards (36) := Pic.FileNew ("images/Ts.bmp")
cards (37) := Pic.FileNew ("images/Jc.bmp")
cards (38) := Pic.FileNew ("images/Jd.bmp")
cards (39) := Pic.FileNew ("images/Jh.bmp")
cards (40) := Pic.FileNew ("images/Js.bmp")
cards (41) := Pic.FileNew ("images/Qc.bmp")
cards (42) := Pic.FileNew ("images/Qd.bmp")
cards (43) := Pic.FileNew ("images/Qh.bmp")
cards (44) := Pic.FileNew ("images/Qs.bmp")
cards (45) := Pic.FileNew ("images/Kc.bmp")
cards (46) := Pic.FileNew ("images/Kd.bmp")
cards (47) := Pic.FileNew ("images/Kh.bmp")
cards (48) := Pic.FileNew ("images/Ks.bmp")
cards (49) := Pic.FileNew ("images/Ac.bmp")
cards (50) := Pic.FileNew ("images/Ad.bmp")
cards (51) := Pic.FileNew ("images/Ah.bmp")
cards (52) := Pic.FileNew ("images/As.bmp")
var card : array 1 .. 5 of int
for y : 1 .. 5
card (y) := Rand.Int (1, 52)
Pic.Draw (cards (card (y)), 104 * y, 240, picCopy)
end for |
|
|
|
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: Sat May 06, 2006 12:42 pm Post subject: (No subject) |
|
|
All those Pic.FileNew lines can be reduced to a single for loop. It's easiest if you name your files "0.bmp", "1.bmp", etc., so you can do something like this:
code: |
for i : 1 .. 52
cards (i) := Pic.FileNew ("images/" + intstr (i) + ".bmp")
end for
|
If you want to keep your current naming scheme, you'll have to do a little more work (if statements... mod would probably be useful). |
|
|
|
|
![](images/spacer.gif) |
|
|