2015-11-01 22:48:39

so I've been messing around with playing cards just to see what i can do with them as an experiment. I'm not sure if this is going to amount to anything substantial, but we'll see. one thought that occurred to me with the likes of a poker game or something similar where you have a hierarchy of objects, how might you create that? specifically with the like of a poker game, how might you implement different hands beating each other? this doesn't have to do with card or poker games specifically. you guys can take it anyway you want.
just note that my language of choice is python so either specific examples in that language would be nice, but even general word examples that aren't language specific would be good for all to see if they could implement that in their own language of choice.

I don’t believe in fighting unnecessarily.  But if something is worth fighting for, then its always a fight worth winning.
check me out on Twitter and on GitHub

2015-11-02 00:21:21

If I understand correctly, I'd probably go with a list, then use pattern matching to add up cards values and search for specific types of hands according to these rules. Example:

import random
#types of cards reference: Heart, Spade, Diamond, and Club.
cards = ['1h','2h','3h','4h','5h','6h','7h','8h','9h','10h','jackh','queenh','kingh','aceh',
        '1s','2s','3s','4s','5s','6s','7s','8s','9s','10s','jacks','queens','kings','aces',
        '1d','2d','3d','4d','5d','6d','7d','8d','9d','10d','jackd','queend','kingd','aced',
        '1c','2c','3c','4c','5c','6c','7c','8c','9c','10c','jackc','queenc','kingc','acec']
#card values
value = [1,2,3,4,5,6,7,8,9,10,13,12,11,14,
        1,2,3,4,5,6,7,8,9,10,13,12,11,14,
        1,2,3,4,5,6,7,8,9,10,13,12,11,14,
        1,2,3,4,5,6,7,8,9,10,13,12,11,14,]
#shuffled deck of cards
deck = ['1h','2h','3h','4h','5h','6h','7h','8h','9h','10h','jackh','queenh','kingh','aceh',
        '1s','2s','3s','4s','5s','6s','7s','8s','9s','10s','jacks','queens','kings','aces',
        '1d','2d','3d','4d','5d','6d','7d','8d','9d','10d','jackd','queend','kingd','aced',
        '1c','2c','3c','4c','5c','6c','7c','8c','9c','10c','jackc','queenc','kingc','acec']

#shuffle the deck
random.shuffle(deck)

#player 1's hand
hand1 = []
#player 2's hand
hand2 = []

#pass out 5 cards to players
for a in xrange(0,5,1):
    hand1.append(deck.pop(0))
    hand2.append(deck.pop(0))

#player 1's score
player1 = 0
#player 2's score
player2 = 0

#add up card values to calculate scores
for a in xrange(0,5,1):
    player1 += value[cards.index(hand1[a])]
    player2 += value[cards.index(hand2[a])]

print player1,player2
if player1 > player2:
    print "player 1 wins!"
elif player1 < player2:
    print "player 2 wins!"
elif player1 == player2:
    print "its a draw!"
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2015-11-02 01:35:39

this is good. and this has potential, but it has issues.
first, i'm assuming the 1s, 1d, 1h, and 1c are referring to aces like in a 5 high straight? if so, the following would be incorrect.
15 64
['1h', '2d', '3h', '4c', '5h'] ['aces', 'jackd', 'aceh', 'jackc', '10s']
player 2 wins!
i made it to print out the hands the first one is player one the second one is player two.
player one has a 5 high straight. where player two has two pairs, aces over jacks.
this is always going to be the case with anything low never beating a hand of higher cards even though the hand that contains lower cards actually beats the hand with higher cards. hopefully that makes sense.

I don’t believe in fighting unnecessarily.  But if something is worth fighting for, then its always a fight worth winning.
check me out on Twitter and on GitHub

2015-11-02 02:52:23

Oops, I forgot aces were actually one cards in decks. Derp.

Anyway, this was more a basic example that just adds up card values without specific hand type parsing, there are better ways of putting it together, but heres an addition that checks for whether players get a 'flush'.

#add up card values to calculate scores
for a in xrange(0,5,1):
    player1 += value[cards.index(hand1[a])]
    player2 += value[cards.index(hand2[a])]


#count card types and check for flush
count = []
for a in hand1:
    if a[len(a)-1] == 's':
        count.append('s')
    elif a[len(a)-1] == 'h':
        count.append('h')
    elif a[len(a)-1] == 'c':
        count.append('c')
    elif a[len(a)-1] == 'd':
        count.append('d')

    if count.count('s') == 5:
        flush1 = True
    elif count.count('h') == 5:
        flush1 = True
    elif count.count('c') == 5:
        flush1 = True
    elif count.count('d') == 5:
        flush1 = True
    else:
        flush1 = False

count = []
for a in hand2:
    if a[len(a)-1] == 's':
        count.append('s')
    elif a[len(a)-1] == 'h':
        count.append('h')
    elif a[len(a)-1] == 'c':
        count.append('c')
    elif a[len(a)-1] == 'd':
        count.append('d')

    if count.count('s') == 5:
        flush2 = True
    elif count.count('h') == 5:
        flush2 = True
    elif count.count('c') == 5:
        flush2 = True
    elif count.count('d') == 5:
        flush2 = True
    else:
        flush2 = False

print 'player 1 hand:',hand1,'score:',player1
print 'player 2 hand:',hand2,'score:',player2
if flush1 == True and flush2 == False:
    print "player 1 has a flush and wins!"
elif flush1 == False and flush2 == True:
    print "player 2 has a flush and wins!"
elif flush1 == True and flush2 == True:
    print "both players have flush and draw!"
elif player1 > player2:
    print "Player 1 Wins!"
elif player1 < player2:
    print "player 2 wins!"
elif player1 == player2:
    print "its a draw!"
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2015-11-05 17:24:51

Perhaps for the pack, list comprehension is your friend:
ranks = [str(i) for i in range(2,11)] + ["jack", "queen", "king", "ace"]
suits = ["clubs", "diamonds", "hearts", "spades"]
pack = [(r,s) for r in ranks for s in suits]

Try my free games and software at www.rockywaters.co.uk

2015-11-05 17:35:18

You can shuffle the deck, but it's not actually necessary:
I like the simple:
hand1 = []
hand2 = []
for i in range(0,5):
    card = random.choice(pack)
    hand1.append(card)
    pack.remove(card)
    card = random.choice(pack)
    hand2.append(card)
    pack.remove(card)
But perhaps the shortest way is?:
hand1 = []
hand2 = []
for i in range(0,5):
    hand1.append(pack.pop(random.randint(0,len(pack))))
    hand2.append(pack.pop(random.randint(0,len(pack))))

Try my free games and software at www.rockywaters.co.uk

2015-11-05 17:42:10

As an aside, the number of different 5 card hands is given by the formula:
hands = (52!*51!*50!*49!*48!)/(2!*3!*4!*5!)
where the symbol ! is pythons math.factorial
This equates to 2,598,960 different hands.

Try my free games and software at www.rockywaters.co.uk

2015-11-05 18:16:36

Given the number of hands, ranking them looks tricky. Perhaps as suggested, test for the 9 differing hand types then assign a value on the hand.  this value will differ for each hand type.
perhaps:
def testFlush(hand):
    if hand[0][1] == hand[1][1] and hand[0][1] == hand[2][1] and hand[0][1] == hand[3][1] and hand[0][1] == hand[4][1]
        return True
Using a dict, perhaps inefficient, but you could assign the value poe(10, 12) to an ace down to pow(10,0) for a 2.  Thus a flush with an ace would always outrank a hand with a king etc.
I would be interested to see an efficient test for a full house.

Try my free games and software at www.rockywaters.co.uk

2015-11-05 19:01:12

Hi people!
It'll be a great to create such engine, like creating a card storry games, like story nexus games.
But, specially for the blind.

Ja volim samo kafu sa Rakijom.