class GoFishGame(object): """ """ ranks = ("two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace" ) # defines the order of the cards def __init__(self): """ """ self.player1 = Player() self.player2 = Player() self.players = deque([self.player1, self.player2]) self.players.rotate(randint(0, len(self.players))) self.asker = self.players[0] self.answerer = self.players[-1] #once players are initialized then we can deal self.fishing_pot = StandardDeck() self.fishing_pot.shuffle() self.deal_cards() def deal_cards(self): amt_to_deal = 5 for player in self.players: player.hand.accept_n_cards( self.fishing_pot.deal_n_cards(amt_to_deal)) #self.player1.accept_n_cards(self.deck.deal_n_cards(amt_to_deal)) #self.player2.accept_n_cards(self.deck.deal_n_cards(amt_to_deal)) def count_hand(self, player): org_hand = dict() for card in player.hand: times = org_hand.get(card.rank, 0) times += 1 org_hand[card.rank] = times return org_hand
class GoFishGame(object): """ """ ranks = ("two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace") # defines the order of the cards def __init__(self): """ """ self.player1 = Player() self.player2 = Player() self.players = deque([self.player1, self.player2]) self.players.rotate(randint(0, len(self.players))) self.asker = self.players[0] self.answerer = self.players[-1] #once players are initialized then we can deal self.fishing_pot = StandardDeck() self.fishing_pot.shuffle() self.deal_cards() def deal_cards(self): amt_to_deal = 5 for player in self.players: player.hand.accept_n_cards(self.fishing_pot.deal_n_cards(amt_to_deal)) #self.player1.accept_n_cards(self.deck.deal_n_cards(amt_to_deal)) #self.player2.accept_n_cards(self.deck.deal_n_cards(amt_to_deal)) def count_hand(self, player): org_hand = dict() for card in player.hand: times = org_hand.get(card.rank, 0) times += 1 org_hand[card.rank] = times return org_hand
class GoFishGame(object): """ """ ranks = ("two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace" ) # defines the order of the cards def __init__(self): """ """ self.fishing_pot = StandardDeck() self.player1 = Player() self.player2 = Player() self.table = Table() self.cur_players = self.table.cur_player_deq.extend( [self.player1, self.player2]) self.losers_list = list() self.fishing_pot.shuffle() self.deal_cards() #self.play_gofish() def deal_cards(self): """ """ amt_of_cards = 7 #this may vary with version of GoFish rules amt_dealt_init_hand1 = self.fishing_pot.deal_n_cards(amt_of_cards) amt_dealt_init_hand2 = self.fishing_pot.deal_n_cards( amt_of_cards) #redundant self.player1.stack.accept_n_cards(amt_dealt_init_hand1) self.player2.stack.accept_n_cards(amt_dealt_init_hand2) print "dealing {} cards to each player".format(amt_of_cards) def computer_guesser(self): #guess_card = random.random(len(self.fishing_pot)) cur_player = self.cur_players[0] mock_deck = StandardDeck() rand_cardrank_num_fromdeck = random.randint(0, len(mock_deck.ranks) - 1) rand_cardrank_num_fromhand = random.randint(0, len(cur_player.stack) - 1) #current player value? guessed_card_method_deck = list(mock_deck)[rand_cardrank_num_fromdeck] guessed_card_method_hand = list( cur_player.stack)[rand_cardrank_num_fromhand] return guessed_card_method_hand def compare_books(self): if self.player1.book > self.player2.book: return "player one wins!" elif self.player1.book < self.player2.book: return "player two wins!" else: "you both lose" # def run_round(self): if self.fishing_pot: #if there aren't any cards in the fishing pot compare books and game ends card_rank_guess = self.computer_guesser() not_cur_player = self.cur_player_deq[ -1] #starting to think about mult players if card_rank_guess in list(not_cur_player.stack.ranks): pass self.cur_player_deq.rotate() else: self.compare_books() #end TM 040615 def check_hand_for_books(self): #put logic in org_cards cur_player = self.cur_players[0] for card in cur_player.stack.deck: pass c = Counter(cur_player.stack.deck) lis = list() for i in c.itervalues(): if c[i] == 4: lis.append(c.pop[i]) def go_fish(self): cur_player = self.cur_players[0] cur_player.stack.accept_n_cards(self.fishing_pot.deal_n_cards()) def org_cards_in_hand(self): count = Counter(self.cur_players[0].stack.deck) for gr_rank in count: if gr_rank.value == 4: pass #deal to cur_player.book
class WarGame(object): """ """ ranks = ("two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace") # defines the order of the cards def __init__(self): """ """ self.deck = StandardDeck() self.player1 = Player() self.player2 = Player() self.table = Table() self.losers_list = list() self.deck.shuffle() self.deal_cards() self.play_war() def deal_cards(self): """ """ while len(self.deck.deck): deal_this_card = self.deck.deal_n_cards() if deal_this_card: self.player1.stack.accept_n_cards(deal_this_card) deal_this_card = self.deck.deal_n_cards() if deal_this_card: self.player2.stack.accept_n_cards(deal_this_card) def play_war(self): """ This run each round and checks if the game is over. """ # exit() when someone loses, for now (see finish_game method) while True: self.run_round() def finish_game(self): """ Resolve the game """ print self.losers_list exit() def run_round(self): """ Each player plays a card and the cards are compared. """ def compare_cards(card1, card2): """ What do we want to get out of compare_cards? 1. Player 1 wins (1>2): player 1 is given all the played cards 2. Player 2 wins (2>1): player 2 is given all the played cards 3. WAR!! (1=2): We do a war """ card1_rank = self.ranks.index(card1[0].rank) card2_rank = self.ranks.index(card2[0].rank) player1_decksize = len(self.player1.stack.deck) player2_decksize = len(self.player2.stack.deck) if card1_rank > card2_rank: print card1[0].rank, " > ", card2[0].rank, " Player 1 wins.", print "Player 1 deck: ", player1_decksize, " cards. Player 2 deck: ", player2_decksize, "cards." return self.player1 elif card1_rank < card2_rank: print card1[0].rank, " < ", card2[0].rank, " Player 2 wins." print "Player 1 deck: ", player1_decksize, " cards. Player 2 deck: ", player2_decksize, "cards." return self.player2 # WAR!! else: print "A war is on!" return None def draw_cards(num_to_draw, player): """ """ card = player.stack.deal_n_cards(num_to_draw) if len(card) == 0: self.losers_list.append(player) return card player1_card = draw_cards(1, self.player1) player2_card = draw_cards(1, self.player2) # Check if the game is done after all draws are complete. if len(self.losers_list) > 0: self.finish_game() winner = None while not winner: winner = compare_cards(player1_card, player2_card) # Give all played cards to the table. self.table.played_cards.accept_n_cards(player1_card) self.table.played_cards.accept_n_cards(player2_card) if not winner: # initialize a war self.table.played_cards.accept_n_cards(draw_cards(3, self.player1)) self.table.played_cards.accept_n_cards(draw_cards(3, self.player2)) player1_card = draw_cards(1, self.player1) player2_card = draw_cards(1, self.player2) # Check if the game is done after all draws are complete. if len(self.losers_list) > 0: self.finish_game() # Give all the cards in the played_cards to the winner winner.stack.accept_n_cards(self.table.played_cards.deal_n_cards(len(self.table.played_cards.deck)))
class WarGame(object): """ """ ranks = ("two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace" ) # defines the order of the cards def __init__(self): """ """ self.deck = StandardDeck() self.player1 = Player() self.player2 = Player() self.table = Table() self.losers_list = list() self.deck.shuffle() self.deal_cards() self.play_war() def deal_cards(self): """ """ while len(self.deck.deck): deal_this_card = self.deck.deal_n_cards() if deal_this_card: self.player1.stack.accept_n_cards(deal_this_card) deal_this_card = self.deck.deal_n_cards() if deal_this_card: self.player2.stack.accept_n_cards(deal_this_card) def play_war(self): """ This run each round and checks if the game is over. """ # exit() when someone loses, for now (see finish_game method) while True: self.run_round() def finish_game(self): """ Resolve the game """ print self.losers_list exit() def run_round(self): """ Each player plays a card and the cards are compared. """ def compare_cards(card1, card2): """ What do we want to get out of compare_cards? 1. Player 1 wins (1>2): player 1 is given all the played cards 2. Player 2 wins (2>1): player 2 is given all the played cards 3. WAR!! (1=2): We do a war """ card1_rank = self.ranks.index(card1[0].rank) card2_rank = self.ranks.index(card2[0].rank) player1_decksize = len(self.player1.stack.deck) player2_decksize = len(self.player2.stack.deck) if card1_rank > card2_rank: print card1[0].rank, " > ", card2[0].rank, " Player 1 wins.", print "Player 1 deck: ", player1_decksize, " cards. Player 2 deck: ", player2_decksize, "cards." return self.player1 elif card1_rank < card2_rank: print card1[0].rank, " < ", card2[0].rank, " Player 2 wins." print "Player 1 deck: ", player1_decksize, " cards. Player 2 deck: ", player2_decksize, "cards." return self.player2 # WAR!! else: print "A war is on!" return None def draw_cards(num_to_draw, player): """ """ card = player.stack.deal_n_cards(num_to_draw) if len(card) == 0: self.losers_list.append(player) return card player1_card = draw_cards(1, self.player1) player2_card = draw_cards(1, self.player2) # Check if the game is done after all draws are complete. if len(self.losers_list) > 0: self.finish_game() winner = None while not winner: winner = compare_cards(player1_card, player2_card) # Give all played cards to the table. self.table.played_cards.accept_n_cards(player1_card) self.table.played_cards.accept_n_cards(player2_card) if not winner: # initialize a war self.table.played_cards.accept_n_cards( draw_cards(3, self.player1)) self.table.played_cards.accept_n_cards( draw_cards(3, self.player2)) player1_card = draw_cards(1, self.player1) player2_card = draw_cards(1, self.player2) # Check if the game is done after all draws are complete. if len(self.losers_list) > 0: self.finish_game() # Give all the cards in the played_cards to the winner winner.stack.accept_n_cards( self.table.played_cards.deal_n_cards( len(self.table.played_cards.deck)))
class GoFishGame(object): """ """ ranks = ("two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace") # defines the order of the cards def __init__(self): """ """ self.fishing_pot = StandardDeck() self.player1 = Player() self.player2 = Player() self.table = Table() self.cur_players = self.table.cur_player_deq.extend([self.player1, self.player2]) self.losers_list = list() self.fishing_pot.shuffle() self.deal_cards() #self.play_gofish() def deal_cards(self): """ """ amt_of_cards = 7 #this may vary with version of GoFish rules amt_dealt_init_hand1 = self.fishing_pot.deal_n_cards(amt_of_cards) amt_dealt_init_hand2 = self.fishing_pot.deal_n_cards(amt_of_cards) #redundant self.player1.stack.accept_n_cards(amt_dealt_init_hand1) self.player2.stack.accept_n_cards(amt_dealt_init_hand2) print "dealing {} cards to each player".format(amt_of_cards) def computer_guesser(self): #guess_card = random.random(len(self.fishing_pot)) cur_player = self.cur_players[0] mock_deck = StandardDeck() rand_cardrank_num_fromdeck = random.randint(0, len(mock_deck.ranks) - 1) rand_cardrank_num_fromhand = random.randint(0, len(cur_player.stack) - 1) #current player value? guessed_card_method_deck = list(mock_deck)[rand_cardrank_num_fromdeck] guessed_card_method_hand = list(cur_player.stack)[rand_cardrank_num_fromhand] return guessed_card_method_hand def compare_books(self): if self.player1.book > self.player2.book: return "player one wins!" elif self.player1.book < self.player2.book: return "player two wins!" else: "you both lose" # TM 040615 def run_round(self): if self.fishing_pot: #if there aren't any cards in the fishing pot compare books and game ends card_rank_guess = self.computer_guesser() not_cur_player = self.cur_player_deq[-1] #starting to think about mult players if card_rank_guess in list(not_cur_player.stack.ranks): pass self.cur_player_deq.rotate() else: self.compare_books() #end TM 040615 def check_hand_for_books(self): #put logic in org_cards cur_player = self.cur_players[0] for card in cur_player.stack.deck: pass c = Counter(cur_player.stack.deck) lis = list() for i in c.itervalues(): if c[i] == 4: lis.append(c.pop[i]) def go_fish(self): cur_player = self.cur_players[0] cur_player.stack.accept_n_cards(self.fishing_pot.deal_n_cards()) def org_cards_in_hand(self): count = Counter(self.cur_players[0].stack.deck) for gr_rank in count: if gr_rank.value == 4: pass #deal to cur_player.book