Python Forum
Dynamically chosing number of players in a "game"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dynamically chosing number of players in a "game"
#1
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()'''
Reply
#2
There may be cleverer answers, but could you create the 4 players up front,
and put them in a list as you do. (len will be 4)
Then you only need to loop through the number of players as necessary.
This could be a solution as max 4 is not big.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
You can use range to create a list with the required amount of players
player_list = [Player() for _ in range(player_count_input)]
Reply
#4
You should never have a variable name like player_x where x is a number used to differentiate the variable from other similar variables. Where there is more than one, use a collection. Your logic will be clearer and your code shorter. You have a lot of logic left to implement, but I bet your finished program will be about half as long as what you have now.

I think your scoring is incorrect. It's been a while, but I believe you don't score for hitting a number that is "closed out" (all players have three hits). If so, you'll need a test for number closed/open and only award points when a player has three hits and the number is open.
Reply
#5
(Jul-16-2020, 09:46 PM)deanhystad Wrote: You should never have a variable name like player_x where x is a number used to differentiate the variable from other similar variables. Where there is more than one, use a collection. Your logic will be clearer and your code shorter. You have a lot of logic left to implement, but I bet your finished program will be about half as long as what you have now.

I think your scoring is incorrect. It's been a while, but I believe you don't score for hitting a number that is "closed out" (all players have three hits). If so, you'll need a test for number closed/open and only award points when a player has three hits and the number is open.

Okay, thank you.

And yes the scoring is incorrect I still need to fix that. At the time I just wanted to make sure the scores would add up.

Okay so the way the objects are differentiated then is by using the list/collection index?

So if I needed to call the score for "player 1" I would just use
player_list[0].check_score()
?
Reply
#6
That is how you would get player 1, but your program should not care about player 1, or player 2. The program should be written treat players generically, and the only important player is the current player. If your code contains hard coded index numbers you are doing something wrong. Indices should always be varibles.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Program: Assign roles to players depending on priorities raquel86 0 1,134 May-16-2022, 11:10 AM
Last Post: raquel86
  Help - random number/letter guessing game juin22 1 3,208 Aug-16-2020, 06:36 AM
Last Post: DPaul
  Can someone help me optimize this game for large number of strings Emekadavid 13 4,968 Jul-06-2020, 06:16 PM
Last Post: deanhystad
  making a guessing number game blacklight 1 2,204 Jul-02-2020, 12:21 AM
Last Post: GOTO10
  Guess the number game jackthechampion 5 3,193 Mar-07-2020, 02:58 AM
Last Post: AKNL
  Asking for help in my code for a "Guess the number" game. Domz 5 3,819 Aug-14-2019, 12:35 PM
Last Post: perfringo
  Guess a number game Drone4four 4 5,270 Nov-16-2018, 03:56 AM
Last Post: Drone4four
  trying to get my random number guessing game to work! NEED HELP RaZrInSaNiTy 4 6,890 Oct-06-2016, 12:49 AM
Last Post: tinabina22

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020