I was wondering if anyone could give a critique of my code. We are supposed to be using inheritance and OOP principles. All the functions but the PlayerSet
class were given, so really I just need someone to critique that class.
The full code is here.
class PlayerSet:
'''A group of players to play
the Pass The Pigs game.'''
def __init__(self,playerCount):
'''PlayerSet(int) -> PlayerSet
Contstructs a group of players,
given how many there are and asks the
name of each player.'''
self.playerCount = playerCount # initialize count of players attribute
self.playersList = [] # initialize list of players attrbute
for i in range(1,self.playerCount+1): # for every i between 1 and plyerCount
name = input('Player ' + str(i) + ', please enter your name:') # ask player i's name
player = Player(name) # create a player with that name
self.playersList.append(player) # add the player to the list of players
self.currentPlayer = self.playersList[0] # initialize current player attribute
def __str__(self):
'''print(PlayerSet) -> str
Returns all of the player's
name and score.'''
string='' # will contain output
for player in self.playersList: # for every player in the list of players
string+= player.get_name() + ' has ' + str(player.get_score()) + ' points \n' # add a string saying the player's name and current score
return string # return the string
def get_curr_name(self):
'''PlayerSet.get_curr_name() -> str
Returns the name of the current player.'''
return self.currentPlayer.get_name() # return the name of the player
def go_to_next_player(self):
'''PlayerSet.go_to_next_player()
Moves the current player to the next
player.'''
index = 0 # will contain index of the current player
for i in range(len(self.playersList)): # for every index in list of players
if self.playersList[i] == self.currentPlayer: # if that index is correct
index = i # assign index to the desired index
index += 1 # add 1 to the desired index, we want to move to next player
index %= self.playerCount # if the number of the player goes over, take the residue mod playerCount
self.currentPlayer=self.playersList[index] # make the current player equal to the new index of the list of players
def is_curr_winner(self):
'''PlayerSet.is_curr_winner() -> bool
Returns if the current player has won.'''
if self.currentPlayer.get_score() >= 100: # if the current player has won
return True
else: # otherwise
return False
def take_turn(self):
'''PlayerSet.take_turn()
Takes the player's turn.'''
self.currentPlayer.play_turn() # use player's method - play_turn() which plays a turn
def play_game(self):
'''PlayerSet.play_game()
Plays the real Pass The Pigs game! Woohoo!'''
while self.is_curr_winner() == False: # until a player has won
print(self) # print the status of the players
print(self.get_curr_name() + ", it's your turn") # print whose turn it is
self.take_turn() # take the player's turn
if self.is_curr_winner() == True: # if the current player has won
print(self) # print the status of the players
return self.get_curr_name() + ' has won!' # return which player has won
self.go_to_next_player() # go to the next player