project_game/mine_spawner.py

45 lines
2.0 KiB
Python

# mine_spawner.py
# ------------
# By Cory
# This module defines a spawner agent class. It spawns mines and then fills the remaining spots with free spaces
from random import randint
from mine import Mine
from free_space import FreeSpace
from cursor import Cursor
class Spawner:
display = False
def __init__(self, board_size):
width, height = board_size
self.board_width, self.board_height = width, height
def play_turn(self, game):
# On the first turn we will spawn everything.
if game.turn_number == 1:
# First spawn free spaces everywhere
for i in range(self.board_width):
for j in range(self.board_height):
free_space = FreeSpace((i,j))
free_space.name_me("freespace"+str(i)+str(j))
game.add_agent(free_space)
# Then spawn 10 mines
for i in range(10):
mine = Mine(((randint(0, self.board_width - 1)),(randint(0, self.board_height - 1))))
mine.name_me("mine"+str(i))
game.log(str(mine.name) + " is located at " + str(mine.position))
# If there is a free space where a mine is going to be, remove the free space and then add the mine.
if not game.is_empty(mine.position):
game.remove_agent(game.get_agents_by_position()[mine.position][0])
game.add_agent(mine)
# Now ask all free spaces to keep track of their neighbors that are mines
# and all of the mines and free spaces to hide themselves.
for i in range(self.board_width):
for j in range(self.board_height):
if len(game.get_agents_by_position()[(i,j)]) != 0:
game.get_agents_by_position()[(i,j)][0].check_neighbors(game)
game.get_agents_by_position()[(i,j)][0].hide()
# Now create a cursor.
game.add_agent(Cursor((self.board_width - 1, self.board_height - 1)))