# Module GM2: Game Master # Defines a class handling several players for a # text-based D&D style game. # Author: Dana Vrajitoru # Class: C463/B551/I400 Fall 2025 from Player2 import * class GM: # constructor with a number of players. It generates a list # containing references to the players and stores it in # self.players. The first nhuman players are assumed to be human # and the others are NPCs. def __init__(self, nplayers = 5, nhuman = 1): self.players = [] for i in range(nhuman): self.players.append(Player(True)) # human player for i in range(1, nplayers - nhuman + 1): self.players.append(Player(False, i)) # NPC self.nplayers = nplayers self.nhuman = nhuman self.quit = False def display(self): print("Situation:") print(f"Human: wood {self.players[0].wood} brick {self.players[0].brick} food {self.players[0].food}") for i in range(len(self.players)): print(i, self.players[i]) def count_alive(self): alive = 0 for pc in self.players: if pc.health > 0: alive += 1 return alive # Main function of the game that has a loop continuing while the user # still wants to play. def play(self): self.display() while not self.quit: for player in self.players: player.play(self.players) self.display() if self.count_alive() <= 1: self.quit = True if self.players[0].health > 0: print("Game over: you won!!") else: print("Game over: you lost!") else: answer = input("Another round? [y/n] ") if 'n' in answer or 'N' in answer: self.quit = True print("Bye!") # main part of the program in place of a main function. # Testing code for the game. if __name__ == '__main__': gm = GM() gm.play() # Formatting a string" # s = "Strength: %d Intelligence %d Home: %d." %(4 5 6) # SyntaxError: invalid syntax. Perhaps you forgot a comma? # s = "Strength: %d Intelligence %d Home: %d." %(4, 5, 6) # s # 'Strength: 4 Intelligence 5 Home: 6.'