class Player: def __init__(self, name, books): self.name = name self.books = books self.cards_asked = [] self.turn = Turn() self.ranks = Constants.RANK def play_turn(self, hands, draw_pile, player): while True: print("") self.turn.see_current_hand(hands, player) print("") cards_taken = 0 p_hand = list(hands[player]) if len(p_hand) > 0: while True: ask_card = input("Choose a card: ") #check if card is valid if ask_card in self.ranks: valid_cards = [y for y in p_hand if y.rank == ask_card] if not valid_cards: print("ERROR: card not in hand.") continue break else: print("ERROR: not a valid card.") continue ##set asked card to any of the tmp cards #(doesn't matter which one, we just want rank) crd = valid_cards[0] self.cards_asked.append(crd) table = self.turn.get_players(hands.keys()) #can't ask yourself table.remove(player) #pick a player to ask while True: print("") print(table) print("") ask_player = input("Choose player: ") if ask_player in table: pl = ask_player break else: print("") print("Not a valid player.") continue time.sleep(0.2) print("") print(pl + ", do you have any " + crd.rank + "'s?") #check if you made a catch, try to make a book cards_taken = self.turn.ask_for_card(hands, crd, player, pl) if cards_taken > 0: self.books = self.create_book(hands, player, self.books) continue else: break else: print("") print("Hand empty!") break ##check if the draw pile has cards, and draw #also do another check if a book can be made if len(draw_pile) > 0: fished_card = self.turn.go_fish(draw_pile, hands, player) self.books = self.create_book(hands, player, self.books) print("") print("Go fish!") time.sleep(0.2) print("") print("Draw card: ", fished_card.get_full_name()) else: print("") print("Draw pile empty!") def create_book(self, hands, player, books): p_hand = list(hands[player]) x = 0 ##make a tmp book with any matching ranks in the player's hand #if tmp book has all four suits of that rank, remove them and add to books count while x < len(self.ranks): temp_book = [y for y in p_hand if y.rank == self.ranks[x]] if len(temp_book) == 4: books += 1 hands[player] = [ z for z in hands[player] if z not in temp_book ] x += 1 temp_book = [] return books
def ai_turn(self, hands, draw_pile, player, players): print("") self.turn.see_current_hand(hands, player) previous_moves = [] cards_taken = 1 while cards_taken > 0: cards_taken = 0 ai_hand = list(hands[player]) if len(ai_hand) > 0: table = [] ##table contains the eligible players to ask # removes itself and any players who have 0 cards for x in players: if x.name == player: continue elif not hands[x.name]: continue else: table.append(x) # default move pl = self.__ai_choose_player(table, hands, player, players) crd = self.__ai_choose_card(ai_hand, player, pl, players) move = (pl, crd) # check if this is the AIs first turn, if yes do not evaluate previous cards if self.previous_player == None or self.previous_card == None: pl = self.__ai_choose_player(table, hands, player, players) crd = self.__ai_choose_card(ai_hand, player, pl, players) ##if the move is equal to any previous move this turn, # choose a different player to ask else: if move in previous_moves: table.remove(pl) pl = self.__ai_choose_player(table, hands, player, players) crd = self.__ai_choose_card(ai_hand, player, pl, players) self.cards_asked.append(crd) self.previous_player = pl self.previous_card = crd previous_move = (self.previous_player, self.previous_card) previous_moves.append(previous_move) # ask for cards as long as its hand has cards time.sleep(0.2) print("") print(pl.name + ", do you have any " + crd.rank + "'s?") cards_taken = self.turn.ask_for_card(hands, crd, player, pl.name) self.books = self.create_book(hands, player, self.books) else: print("") print("Hand empty!") break ##check if the draw pile has cards, and draw # also do another check if a book can be made if len(draw_pile) > 0: turn = Turn() fished_card = turn.go_fish(draw_pile, hands, player) self.books = self.create_book(hands, player, self.books) time.sleep(0.2) print("") print("Go fish!") else: print("") print("Draw pile empty!")