from man import Man from snack import Snack from mine import Mine from random import shuffle from chaser import Man_Chaser """This class sets up the board initially, but also is responsible for adding snacks and mines after they are consumed. Also increases mines as game gets more difficult.""" class Board: display = False snack=False mine=False chaser=False def __init__(self,width, height,num_snacks,num_chasers,MAX_MINES,LEVELS,state): self.width = width self.height = height self.num_snacks = num_snacks self.num_chasers = num_chasers self.MAX_MINES = MAX_MINES self.LEVELS = LEVELS self.state = state def get_agents(self,num_snacks,num_chasers,state): """Sets up all the agents (snacks, mines, man, and chasers) in the game. First, determines mines needed by calling the mine_counter function. Then generates a list of all posiitons, randomizes them, and assigns the man to the first random position, the snacks to the next num_snack positions, the mines to the required number of posiitons as specified in mine_counter, and then the chasers. Returns these agents so they can be positioned on the board.""" num_mines = self.mine_counter(state) all_positions = self.get_all_positions() shuffle(all_positions) man= [Man(all_positions[0])] snacks = [Snack(p) for p in all_positions[1:(num_snacks+1)]] mines = [Mine(p) for p in all_positions[(num_snacks + 2):(num_snacks + num_mines +2)]] chaser = [Man_Chaser(p) for p in all_positions[(num_snacks + num_mines + 2):(num_snacks + num_mines + num_chasers + 2)]] agents = man + snacks + mines + chaser + [self] return agents def play_turn(self,game): """Checks after every turn (turn is length of time specified in retro) if the game needs snacks or mines or a chaser.""" game.log(game.state['Score']) while self.game_needs_snacks(game): self.add_snack(game) while self.game_needs_mines(game): self.add_mine(game) while self.game_needs_chaser(game): self.add_chaser(game) def add_chaser(self,game): """Adds a chaser by finidng all the positions, randomizing them, and adding the chaser to the first random position.""" all_positions=self.get_all_positions() shuffle(all_positions) index =0 while not game.is_empty(all_positions[index]): index = index + 1 chaser = Man_Chaser(all_positions[index]) game.add_agent(chaser) def game_needs_chaser(self,game): """Returns true when the number of chasers in the game is less than the number of chasers specified in nav_game.py.""" return self.count_chaser(game) < self.num_chasers def count_chaser(self,game): """Counts the number of chases currently in the game.""" chasers = [a for a in game.agents if a.chaser] return len(chasers) def mine_counter(self,state): """Determines how many mines should be in the game, based on the score of the game. Refers to the LEVELS list above which contains the score and a correspponding number of mines. This function puts a certain number of mines down based on the score. Currently, does not reduce the number of mines when the score lowers after hitting a mine. Returns the number of mines needed.""" score = state['Score'] for limit, n in self.LEVELS: if score < limit: return n return self.MAX_MINES def add_mine(self,game): """Adds a mine by finding all positions, randomizing them, and then adding a mine to the first non-occupied position.""" all_positions=self.get_all_positions() shuffle(all_positions) index =0 while not game.is_empty(all_positions[index]): index = index + 1 mine = Mine(all_positions[index]) game.add_agent(mine) def count_mines(self,game): """Counts the number of mines currently in the game.""" mines= [a for a in game.agents if a.mine] return len(mines) def game_needs_mines(self,game): """Returns true when the number of mines in the game is less than the number of mines required based on the score.""" return self.count_mines(game) < self.mine_counter(game.state) def add_snack(self,game): """Adds a snack by finding all positions, randomizing them, and then adding a snack to the first non-occupied position.""" all_positions=self.get_all_positions() shuffle(all_positions) index =0 while not game.is_empty(all_positions[index]): index = index + 1 snack = Snack(all_positions[index]) game.add_agent(snack) def count_snacks(self,game): """Counts the number of snacks currently in the game.""" snacks= [a for a in game.agents if a.snack] return len(snacks) def game_needs_snacks(self,game): """Returns true when the number of snacks in the game is less than the number of snacks specified in nav-game.py. In a future version, this number of snacks could vary as the game gets more difficult.""" return self.count_snacks(game) < self.num_snacks def get_all_positions(self): """Finds all coordinate positions in the game and adds them to a list of positions.""" positions=[] for i in range(self.width): for j in range(self.height): positions.append((i,j)) return positions