I got all of the free spaces to determine the

number of mine neighbors they have and change
their characters correspondingly.
This commit is contained in:
Cory 2024-05-18 22:40:01 -04:00
parent 71e22504ab
commit e615e99e75
6 changed files with 68 additions and 6 deletions

Binary file not shown.

View File

@ -5,14 +5,64 @@
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):
game.log(str(self.position) + " is clear!")
self.revealed = True
def hide(self):
self.display = False
self.display = False
def play_turn(self, game):
if (not game.is_empty(self.position)) and self.revealed:
self.display = False
elif game.is_empty(self.position) and self.revealed and self.neighbors > 0:
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)

11
mine.py
View File

@ -15,4 +15,13 @@ class Mine:
game.end()
def hide(self):
self.display = False
self.display = False
def name_me(self, named):
self.name = named
def check_neighbors(self,game):
pass
def reveal():
pass

View File

@ -22,20 +22,23 @@ class Spawner:
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))))
game.log(mine.position)
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 of the mines and free spaces to hide themselves.
# 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)))
game.add_agent(Cursor((self.board_width - 1, self.board_height - 1)))