Jul-16-2020, 03:19 PM
Hey everyone I'm trying to write the game Cricket that you play on a darts board and I'm a little stuck on how to make a prompt that would take any number of players between 1 and 4. I used if statements however I feel that isn't the best or most elegant way. I'm trying to learn how to handle creating objects when the number of objects is unknown until a user specifies. Any help would be appreciated.
#This is the game of Cricket played on a darts board. #This program keeps track of the dart board progress and scores of each player. #Player object class Player: def __init__(self): self.hits = {"15": 0, "16": 0, "17": 0, "18": 0, "19": 0, "20": 0, "bullseye": 0} self.score = 0 def card(self, throw): if throw in self.hits: print("Great {0}!".format(throw)) if self.hits[throw] < 3: self.hits[throw] = self.hits.get(throw) + 1 else: self.add_score(throw) print(self.hits, " Points: {0}.".format(self.score), sep='\n') def game_checker(self): if self.hits["15"] == 3 and self.hits["16"] == 3 and self.hits["17"] == 3 and self.hits["18"] == 3 and self.hits["19"] == 3 and self.hits["20"] == 3 and self.hits["bullseye"] == 3: return True else: return False def add_score(self, throw): if throw == "bullseye": self.score = self.score + 50 else: self.score += int(throw) def check_score(self): return self.score #Number of players prompt player_count_input = int(input("Welcome to Cricket. How many players (up to 4)? ")) if player_count_input < 1 or player_count_input > 4: player_count_input = int(input("Invalid number of players, please select 1, 2, 3, or 4 players: ")) else: if player_count_input == 1: player_1 = Player() player_list = [player_1] if player_count_input == 2: player_1 = Player() player_2 = Player() player_list = [player_1, player_2] if player_count_input == 3: player_1 = Player() player_2 = Player() player_3 = Player() player_list = [player_1, player_2, player_3] if player_count_input == 4: player_1 = Player(), player_2 = Player(), player_3 = Player(), player_4 = Player() player_list = [player_1, player_2, player_3, player_4] game = True while game: score_list = [] for x in player_list: player_number = player_list.index(x) turn = 1 while turn < 4: print("Player {0}, let's throw!".format(x)) throw = input("Enter your throw: ") x.card(throw) turn += 1 check_winner = x.game_checker() if check_winner is True: for y in player_list: score_list.append(x.check_score()) ''' turn = 1 while turn < 4: print("Player 1, let's throw!") throw = input("Enter your throw: ") player_1.card(throw) turn += 1 check_winner = player_1.game_checker() p1 = player_1.check_score() p2 = player_2.check_score() if check_winner is True and p1 > p2: print("Winner!") exit() turn = 1 while turn < 4: print("Player 2, let's throw!") throw = input("Enter your throw: ") player_2.card(throw) turn += 1 check_winner = player_1.game_checker() p1 = player_1.check_score() p2 = player_2.check_score() if check_winner is True and p2 > p1: print("Winner!") exit()'''