Python Forum
Dynamically chosing number of players in a "game" - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Dynamically chosing number of players in a "game" (/thread-28379.html)



Dynamically chosing number of players in a "game" - nsadams87xx - Jul-16-2020

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()'''



RE: Dynamically chosing number of players in a "game" - DPaul - Jul-16-2020

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


RE: Dynamically chosing number of players in a "game" - Yoriz - Jul-16-2020

You can use range to create a list with the required amount of players
player_list = [Player() for _ in range(player_count_input)]



RE: Dynamically chosing number of players in a "game" - deanhystad - Jul-16-2020

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.


RE: Dynamically chosing number of players in a "game" - nsadams87xx - Jul-17-2020

(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()
?


RE: Dynamically chosing number of players in a "game" - deanhystad - Jul-17-2020

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.