# free_space.py # ------------ # By Cory # This module defines a free space agent class. It displays itself and (eventually) # its neighbors if enter is pressed while the cursor is on it. class FreeSpace: character = '*' display = True revealed = False neighbors = 0 def __init__(self, position): self.position = position self.width , self.height = position def handle_keystroke(self, keystroke, game): if keystroke.name in ("KEY_ENTER"): if not game.is_empty(self.position): board_width,board_height = game.board_size for i in range(board_width): for j in range(board_height): try: game.get_agent_by_name("freespace"+str(i)+str(j)).show() except: pass self.reveal(game) for i in range(board_width): for j in range(board_height): try: game.get_agent_by_name("freespace"+str(i)+str(j)).hide() except: pass def hide(self): if self.revealed == False: self.display = False def show(self): self.display = True def play_turn(self, game): if (not game.is_empty(self.position)) and self.revealed and self.display == True: self.display = False elif game.is_empty(self.position) and self.revealed and self.neighbors > 0 and self.display == False: self.display = True def name_me(self, named): self.name = named def check_neighbors(self,game): names_of_mines = ["mine0","mine1","mine2","mine3","mine4","mine5","mine6","mine7","mine8","mine9"] if game.on_board((self.width + 1,self.height)): if len(game.get_agents_by_position()[(self.width + 1,self.height)]) != 0: if game.get_agents_by_position()[(self.width + 1,self.height)][0].name in names_of_mines: self.neighbors = self.neighbors + 1 if game.on_board((self.width - 1,self.height)): if len(game.get_agents_by_position()[(self.width - 1,self.height)]) != 0: if game.get_agents_by_position()[(self.width - 1,self.height)][0].name in names_of_mines: self.neighbors = self.neighbors + 1 if game.on_board((self.width,self.height + 1)): if len(game.get_agents_by_position()[(self.width,self.height + 1)]) != 0: if game.get_agents_by_position()[(self.width,self.height + 1)][0].name in names_of_mines: self.neighbors = self.neighbors + 1 if game.on_board((self.width,self.height - 1)): if len(game.get_agents_by_position()[(self.width,self.height - 1)]) != 0: if game.get_agents_by_position()[(self.width,self.height - 1)][0].name in names_of_mines: self.neighbors = self.neighbors + 1 if game.on_board((self.width + 1,self.height + 1)): if len(game.get_agents_by_position()[(self.width + 1,self.height + 1)]) != 0: if game.get_agents_by_position()[(self.width + 1,self.height + 1)][0].name in names_of_mines: self.neighbors = self.neighbors + 1 if game.on_board((self.width + 1,self.height - 1)): if len(game.get_agents_by_position()[(self.width + 1,self.height - 1)]) != 0: if game.get_agents_by_position()[(self.width + 1,self.height - 1)][0].name in names_of_mines: self.neighbors = self.neighbors + 1 if game.on_board((self.width - 1,self.height + 1)): if len(game.get_agents_by_position()[(self.width - 1,self.height + 1)]) != 0: if game.get_agents_by_position()[(self.width - 1,self.height + 1)][0].name in names_of_mines: self.neighbors = self.neighbors + 1 if game.on_board((self.width - 1,self.height - 1)): if len(game.get_agents_by_position()[(self.width - 1,self.height - 1)]) != 0: if game.get_agents_by_position()[(self.width - 1,self.height - 1)][0].name in names_of_mines: self.neighbors = self.neighbors + 1 if self.neighbors > 0: self.character = str(self.neighbors) def reveal(self,game): game.log("Revealing " + str(self.position)) self.revealed = True if self.neighbors == 0: if game.on_board((self.width + 1,self.height)): if len(game.get_agents_by_position()[(self.width + 1,self.height)]) != 0: if not game.get_agents_by_position()[(self.width + 1,self.height)][0].revealed: game.get_agents_by_position()[(self.width + 1,self.height)][0].reveal(game) if game.on_board((self.width - 1,self.height)): if len(game.get_agents_by_position()[(self.width - 1,self.height)]) != 0: if not game.get_agents_by_position()[(self.width - 1,self.height)][0].revealed: game.get_agents_by_position()[(self.width - 1,self.height)][0].reveal(game) if game.on_board((self.width,self.height + 1)): if len(game.get_agents_by_position()[(self.width,self.height + 1)]) != 0: if not game.get_agents_by_position()[(self.width,self.height + 1)][0].revealed: game.get_agents_by_position()[(self.width,self.height + 1)][0].reveal(game) if game.on_board((self.width,self.height - 1)): if len(game.get_agents_by_position()[(self.width,self.height - 1)]) != 0: if not game.get_agents_by_position()[(self.width,self.height - 1)][0].revealed: game.get_agents_by_position()[(self.width,self.height - 1)][0].reveal(game) if game.on_board((self.width + 1,self.height + 1)): if len(game.get_agents_by_position()[(self.width + 1,self.height + 1)]) != 0: if not game.get_agents_by_position()[(self.width + 1,self.height + 1)][0].revealed: game.get_agents_by_position()[(self.width + 1,self.height + 1)][0].reveal(game) if game.on_board((self.width + 1,self.height - 1)): if len(game.get_agents_by_position()[(self.width + 1,self.height - 1)]) != 0: if not game.get_agents_by_position()[(self.width + 1,self.height - 1)][0].revealed: game.get_agents_by_position()[(self.width + 1,self.height - 1)][0].reveal(game) if game.on_board((self.width - 1,self.height + 1)): if len(game.get_agents_by_position()[(self.width - 1,self.height + 1)]) != 0: if not game.get_agents_by_position()[(self.width - 1,self.height + 1)][0].revealed: game.get_agents_by_position()[(self.width - 1,self.height + 1)][0].reveal(game) if game.on_board((self.width - 1,self.height - 1)): if len(game.get_agents_by_position()[(self.width - 1,self.height - 1)]) != 0: if not game.get_agents_by_position()[(self.width - 1,self.height - 1)][0].revealed: game.get_agents_by_position()[(self.width - 1,self.height - 1)][0].reveal(game)