Skip to content

Instantly share code, notes, and snippets.

@drvinceknight
Last active August 29, 2015 14:11
Show Gist options
  • Save drvinceknight/ecf43666c92a2cde1950 to your computer and use it in GitHub Desktop.
Save drvinceknight/ecf43666c92a2cde1950 to your computer and use it in GitHub Desktop.
from __future__ import division
import random
from csv import reader
class Player:
"""
A class for a player
"""
def __init__(self, name):
self.name = name
self.hand = []
#deck = ['1S','2S','3S','4S', '1H', '2H', '3H', '4H']
f = open('cards', 'r')
deck = [row[0] for row in reader(f)]
f.close()
p1 = Player(1)
p2 = Player(2)
random.shuffle(deck)
while len(deck) > 0:
p1.hand.append(deck.pop())
p2.hand.append(deck.pop())
print p1.hand
print p2.hand
slam_prob = .5
table = [p1.hand.pop(), p2.hand.pop()]
count = 0
while len(p1.hand) > 0 and len(p2.hand) > 0:
count += 1
if len(p1.hand) > 0: # P1 plays
table.append(p1.hand.pop())
if len(p2.hand) > 0: # P2 plays
table.append(p2.hand.pop())
if len(table) > 1: # Both players check for slam
if table[-1][0] == table[-2][0]:
if random.random() < slam_prob:
print 'p1 slams'
while len(table) > 0:
p1.hand.append(table.pop())
else:
while len(table) > 0:
p2.hand.append(table.pop())
print 'p2 slams'
print 'Game is over'
if len(p1.hand) == len(p2.hand) == 0:
print 'Draw'
elif len(p1.hand) == 0:
print 'P1 wins'
else:
print 'P2 wins'
print 'Number of turns: ', count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment