Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Poker Game
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
msoccer




PostPosted: Fri Nov 28, 2003 4:58 pm   Post subject: Poker Game

Hi all. I decided to begin a Poker game, but have reached a few forks in the road. First off, this is a simple variant of Poker, where 5 random cards are drawn, and you are assessed which type of hand you have.

Once I have this concept down, I will then later move on to including different draws (exchanging cards).

For this program I need to know if the user has the following: One Pair, Two Pair, 3 of a Kind, Full House, or 4 of a kind.

Here's the breakdown code by code.


var deck: array 1..52 of string % Deck of Cards
var hand: array 1..5 of int % Hand of 5 Cards
var tmp:int
var pairs:int
randomize



Just intializing the variables, nothing special here.


deck(1):="A"+ chr(3) % Suit of Hearts
deck(2):="2"+ chr(3)
deck(3):="3"+ chr(3)
deck(4):="4"+ chr(3)
deck(5):="5"+ chr(3)
deck(6):="6"+ chr(3)
deck(7):="7"+ chr(3)
deck(8):="8"+ chr(3)
deck(9):="9"+ chr(3)
deck(10):="10"+ chr(3)
deck(11):="J"+ chr(3)
deck(12):="Q"+ chr(3)
deck(13):="K"+ chr(3)
deck(14):="A"+ chr(4) % Suit of Diamonds
deck(15):="2"+ chr(4)
deck(16):="3"+ chr(4)
deck(17):="4"+ chr(4)
deck(18):="5"+ chr(4)
deck(19):="6"+ chr(4)
deck(20):="7"+ chr(4)
deck(21):="8"+ chr(4)
deck(22):="9"+ chr(4)
deck(23):="10"+ chr(4)
deck(24):="J"+ chr(4)
deck(25):="Q"+ chr(4)
deck(26):="K"+ chr(4)
deck(27):="A"+ chr(5) % Suit of Clubs
deck(28):="2"+ chr(5)
deck(29):="3"+ chr(5)
deck(30):="4"+ chr(5)
deck(31):="5"+ chr(5)
deck(32):="6"+ chr(5)
deck(33):="7"+ chr(5)
deck(34):="8"+ chr(5)
deck(35):="9"+ chr(5)
deck(36):="10"+ chr(5)
deck(37):="J"+ chr(5)
deck(38):="Q"+ chr(5)
deck(39):="K"+ chr(5)
deck(40):="A"+ chr(6) % Suit of Spades
deck(41):="2"+ chr(6)
deck(42):="3"+ chr(6)
deck(43):="4"+ chr(6)
deck(44):="5"+ chr(6)
deck(45):="6"+ chr(6)
deck(46):="7"+ chr(6)
deck(47):="8"+ chr(6)
deck(48):="9"+ chr(6)
deck(49):="10"+ chr(6)
deck(50):="J"+ chr(6)
deck(51):="Q"+ chr(6)
deck(52):="K"+ chr(6)




Now, I'm sure there is a more simple way of doing this, but for now I am content doing it this way. This simply creates the 52 cards, accompanied with the different suits.


loop
for i:1..5
randint (hand (i), 1, 52)
end for
for i:1..4
for j:i+1..5

if hand (i) > hand (j) then
tmp:=hand (i)
hand (i):= hand (j)
hand (j):= tmp
end if
end for
end for
exit when hand(1) not= hand(2) and hand(2) not= hand(3) and hand(3) not= hand(4) and hand(4) not= hand(5)
end loop



Here is your classic bubble sort. This section will select five random numbers and this will make up your hand. After this it will then sort the cards based on their numbers of intializing (i.e. The King of Hearts is 13, while the Ace of Diamonds is 14).


pairs:=0
for j:1..5
for m:1..5
for o:1..3
if hand(j)+13*o=hand(m) then
pairs:=pairs+1
end if
end for
end for
end for
if pairs=1 then
put "One Pair"
elsif pairs=2 then
put "Two Pair"
end if




This section took me a few minutes to think up. At first I considered that I would have to go thru, bit by bit to try to discern if there was a pair. This took me way too many lines and found that this is much more effecient. It uses three loops to check every possible scenario. I added the *13, because each number repeats every 13 times (ie. 4 of Hearts is 4, of Diamonds is 17, of Clubs is 30, of Spades is 43). Every time it finds a pair, it adds 1 to the var pair, and hence will display whether or not you have one pair or two pair.

--------------------------------------------------------------

In conclusion, what has me stuck is how would I go about finding out if I have

* 3 of a Kind
* Full House
* Four of a Kind.

If you have any suggestions for all three, or even one any input would be greatly appreciated.
Sponsor
Sponsor
Sponsor
sponsor
AsianSensation




PostPosted: Fri Nov 28, 2003 5:43 pm   Post subject: (No subject)

3 of a kind:

well, you hand is sorted is it not? And if I'm correct, they are sorted by their value? So just run a for loop of 3, and check to see if that card and two of the card after it is the same, then you can check for 3 in a row.

Full House:

again, your hand is already sorted, so you check for three in a row, then remove that triplet, then check to see if you have another pair, and then add the triplet back on

4 of a kind:

use the same method as 3 of the kind, except now you run a for loop of 2.
AsianSensation




PostPosted: Fri Nov 28, 2003 5:43 pm   Post subject: (No subject)

btw, you can take out the same card more than once, so try and change that.
msoccer




PostPosted: Fri Nov 28, 2003 6:20 pm   Post subject: (No subject)

Thank you for your input, AsianSenation...

However, the cards are not sorted...

AAAA, 2222...they are sorted by set...so they incremnet by 13...which means after being sorted both your first and last numbers could be 4. Which wouldn't work with your idea, but do you think I should change the way i numbered them?
msoccer




PostPosted: Fri Nov 28, 2003 7:16 pm   Post subject: (No subject)

Smile

I took your advice Sensation, and re-engineered the program, and finally got my 3-of a kind working.

Thanks
AsianSensation




PostPosted: Fri Nov 28, 2003 8:26 pm   Post subject: (No subject)

cool, I also failed to notice that you did do the checking about taking cards out more than once.

anyways, once you get three of a kind working, four of a kind and full house will be easy.

The only one where it might get a bit harder is when doing straight, flush, or royal flush.
Tony




PostPosted: Fri Nov 28, 2003 10:30 pm   Post subject: (No subject)

thats why you should assign numeric values to cards instead of strings.

This way you can just check something like if all cards of same suit and
card(2) = card(1)+1
card(3) = card(2)+1
etc

would be easier to detect straights.

Also another important point - you should first check if the hand is unique or not, THEN sort it. It would be more efficient that way (pointless to sort the hand if its not unique)

And if you plan on expanding the game to have a deck (draw cards) then I advice you to use my [random list generator] available in [turing source code] to "shuffle" your deck.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
msoccer




PostPosted: Sat Nov 29, 2003 10:15 am   Post subject: (No subject)

Wow, thanks for all the help. Compsci looks like me new home Smile
Sponsor
Sponsor
Sponsor
sponsor
msoccer




PostPosted: Sat Nov 29, 2003 10:27 am   Post subject: (No subject)

Quick question Tony...

I tried to use your code but testing out gave m this error.

code:
var randN:int %just to hold the number
var numbers:array 1..10 of int
for i:1..10
numbers(i):=i
end for

for decreasing i:10..1
randN := [b]Rand[/b].Int(1,i)
put numbers(randN)
    if randN not=i then
    for a:randN+1..i
    numbers(a-1):=numbers(a)
    end for
    end if
end for


It said Rand had not been declared.
santabruzer




PostPosted: Sat Nov 29, 2003 10:45 am   Post subject: (No subject)

what's the [b] used for.. because that's the only think that's keeping it back...
msoccer




PostPosted: Sat Nov 29, 2003 10:48 am   Post subject: (No subject)

Actually, I only meant BOLD on this BBCODE to highlight which part of the code I was talking about.
McKenzie




PostPosted: Sat Nov 29, 2003 11:34 am   Post subject: (No subject)

What version of Turing are you using? If it is one of the older ones (sound like it is) go back to using randint. It works the exact same (well once you put your variable as the first parameter)
Tony




PostPosted: Sat Nov 29, 2003 1:53 pm   Post subject: (No subject)

McKenzie is right. Rand.Int() is the new way of of using random in Turing v4.x+. If you have v3.x or below, you should use randint(). Same thing really.

Rolling Eyes you should be able to figure such things out on your own. Or just get a newer compiler so that everything (but sprites) work Laughing
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Andy




PostPosted: Sun Nov 30, 2003 8:27 pm   Post subject: (No subject)

Hmmm"¦ looks like the infamous Ronald Mickey is helping Compsci out"¦
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: