game logic for advancing the round

This commit is contained in:
berryman 2012-12-12 23:31:35 -05:00
parent 3f028cbea3
commit 65f96be425
2 changed files with 48 additions and 28 deletions

42
cah.py
View File

@ -60,6 +60,9 @@ class Deck(object):
type.remove(card)
return card
def __repr__(self):
return json.dumps({'questions': len(self.questionDb), 'answers': len(self.answerDb)})
class Card(object):
def __init__(self, id, type, text, **kwargs):
self.id = id
@ -67,15 +70,14 @@ class Card(object):
self.text = text
for key, value in kwargs.iteritems():
setattr(self, key, value)
def __repr__(self):
return json.dumps(self.__dict__)
class Game(object):
def __init__(self, players, round_limit = 5, rule_set="house"):
self.round_limit = round_limit
self.deck = Deck()
self.players = self.build_player_list(players)
self.round = None
self.question = None
def build_player_list(self, players):
player_list = {}
@ -83,11 +85,16 @@ class Game(object):
player_list[player] = PlayerHand(self.deck)
return player_list
def control_round(self):
for round in range(self.round_limit):
current_round = Round()
current_round.give_question_and_hands()
current_round.show_winner()
def next_round(self):
if self.round is None:
self.round = 0
if self.round < self.round_limit:
self.round = self.round + 1
else:
raise IndexError
self.question = deck.drawCard('question')
return {'question': self.question, 'hands': self.players}
def displayAnswer(self):
pass
@ -103,24 +110,18 @@ class Game(object):
pass
class Round(object):
def show_question(self):
pass
def get_answers(self):
pass
def get_votes(self):
pass
def show_winner(self):
pass
def __init__(self, deck, players):
self.question = deck.drawCard('question')
self.players = players
class PlayerHand(object):
def __init__(self, deck):
self.deck = deck
self.card_list = self.dealHand()
self.card_list = self.dealHand(deck)
def dealHand(self):
def dealHand(self, deck):
hand = []
while len(hand) < 5:
card = self.deck.drawCard('answer')
card = deck.drawCard('answer')
hand.append(card)
return hand
@ -130,6 +131,7 @@ class PlayerHand(object):
if __name__=="__main__":
deck = Deck()
print 'Current Question: %s' % deck.drawCard('question').text

34
test.py
View File

@ -22,14 +22,7 @@ def test_card_parsing(deck=None):
deck = Deck()
for deck_type in [deck.answerDb, deck.questionDb]:
for card in deck_type:
assert type(card) is Card
assert type(card.id) is int
assert type(card.type) is str
assert card.type in ['answer', 'question']
assert type(card.text) is str
assert card.text.find('\n') is -1
if card.type is 'question':
assert type(card.answers) is int
test_card(card)
def test_game():
game = Game(['Bear','Swim', 'Jazz'])
@ -39,6 +32,19 @@ def test_game():
test_player_hand(hand)
def test_round_advancement(game=None):
if game is None:
game = Game(['Bear','Swim', 'Jazz'])
assert game.round is None
assert game.question is None
while round < game.round_limit:
bot_gets = game.next_round()
assert isinstance(bot_gets, dict)
assert bot_gets.has_key('question')
assert bot_gets.has_key('hands')
def test_player_hand(hand=None):
if hand is None:
hand = PlayerHand(Deck())
@ -46,3 +52,15 @@ def test_player_hand(hand=None):
for count, card in enumerate(hand.card_list):
assert count < 5
assert type(card) is Card
def test_card(card=None):
if card is None:
card = Deck().drawCard('question')
assert type(card) is Card
assert type(card.id) is int
assert type(card.type) is str
assert card.type in ['answer', 'question']
assert type(card.text) is str
assert card.text.find('\n') is -1
if card.type is 'question':
assert type(card.answers) is int