def __init__(self, players, number_of_cards, deck, starting_player=1): self.deck = deck self.first_play = True self.current_player = starting_player self.player_hands = deal_player_hands(number_of_cards, players, deck) self.players = players self.played_cards = PlayedCards() self.played_cards.add_card(self.deck.deal_card()) top_card = self.played_cards.top_card #set direction self.direction = GameDirection.clockwise if top_card.rank == Rank.jack: self.set_next_player(top_card, True) #set state machine self.accumulated_count = 0 self.update_state(top_card)
class Game(object): def __init__(self, players, number_of_cards, deck, starting_player=1): self.deck = deck self.first_play = True self.current_player = starting_player self.player_hands = deal_player_hands(number_of_cards, players, deck) self.players = players self.played_cards = PlayedCards() self.played_cards.add_card(self.deck.deal_card()) top_card = self.played_cards.top_card #set direction self.direction = GameDirection.clockwise if top_card.rank == Rank.jack: self.set_next_player(top_card, True) #set state machine self.accumulated_count = 0 self.update_state(top_card) def update_state(self, card): if card is None: self.state = GameState.NORMAL self.accumulated_count = 0 elif card.rank == Rank.two: self.state = GameState.PICK self.accumulated_count = self.accumulated_count + 2 elif card.rank == Rank.four: self.state = GameState.PICK self.accumulated_count = self.accumulated_count + 4 elif card.rank == Rank.eight: self.state = GameState.WAIT self.accumulated_count = 0 else: self.state = GameState.NORMAL self.accumulated_count = 0 def player_hand(self, player_name): """ Return a list of a players cards """ return self.player_hands[player_name].hand def status(self): """ Return the status of the game """ player_counts = [player_hand.hand.number_of_cards() for _, player_hand in self.player_hands.items()] return GameStatus(self.current_player, self.played_cards.top_card, player_counts) def play(self, player_name, move): """ Take a move, validate it and respond back with a response """ current_player = self.players[self.current_player-1] if current_player.name != player_name: return invalid_play_response(("Not player %s's turn" % (player_name,))) #is move valid hand = self.player_hand(current_player.name) if not self.valid_move(move, hand, self.played_cards.top_card): return invalid_play_response("Not a valid move") if move.move_type == MoveType.pick: card_count = 1 if self.state == GameState.PICK: card_count = self.accumulated_count self.accumulated_count = 0 self.state = GameState.NORMAL for _ in xrange(card_count): self.pick(hand) elif move.move_type == MoveType.play: hand.remove_card(move.card) self.played_cards.add_card(move.card, move.suit) self.update_state(move.card) elif move.move_type == MoveType.wait: self.update_state(move.card) if hand.number_of_cards() > 0: self.set_next_player(move.card) else: current_player.won = current_player.won + 1 for p in self.players: p.played = p.played + 1 self.state = GameState.FINISHED self.first_play = False return valid_play_response() def set_next_player(self, card, start_move=False): #in two player jack means play again, except on deal when other player should play if len(self.players) == 2 and card and card.rank == Rank.jack and not start_move: return if card and card.rank == Rank.jack: if self.direction == GameDirection.clockwise: self.direction = GameDirection.anticlockwise else: self.direction = GameDirection.clockwise if self.direction == GameDirection.clockwise: if self.current_player == len(self.players): self.current_player = 1 else: self.current_player = self.current_player+1 else: if self.current_player == 1: self.current_player = len(self.players) else: self.current_player = self.current_player-1 def pick(self, hand): if not self.deck.has_card(): cards = self.played_cards.return_played_cards() self.deck.add_cards(cards) self.deck.shuffle() card = self.deck.deal_card() hand.add_card(card) def valid_move(self, move, hand, top_card): #check pick vs play if move.move_type == MoveType.pick: return valid_pick(hand, top_card, self.state, self.first_play) return valid_play(move.card, hand, top_card, self.state, self.first_play)