Figured out how to spawn a mine and end the game

if the cursor is over the mine. We now have to
not end the game until the user SELECTS the space
the mine is at. I hadn't realized that is_empty
only works if display is True? So I moved the
game.end() to the mine class.
This commit is contained in:
Cory 2024-05-18 21:03:33 -04:00
parent cff4129336
commit 3334db0c8b
7 changed files with 41 additions and 6 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,9 +1,9 @@
# cursor.py
# ------------
# By MWC Contributors
# This module defines a spaceship agent class.
# By Cory
# This module defines a cursor agent class.
class Cursor:
name = "cursos"
name = "cursor"
character = 'O'
def __init__(self, board_size):
@ -24,5 +24,4 @@ class Cursor:
if game.on_board(new_position):
if game.is_empty(new_position):
self.position = new_position
else:
game.end()
game.log(self.position)

14
mine.py Normal file
View File

@ -0,0 +1,14 @@
# mine.py
# ------------
# By Cory
# This module defines a mine agent class. It doesn't do anything except exist in a position
class Mine:
display = False
def __init__(self, position):
self.position = position
def play_turn(self, game):
if not game.is_empty(self.position):
game.log("You hit a mine at " + str(self.position) + ".")
game.end()

20
mine_spawner.py Normal file
View File

@ -0,0 +1,20 @@
# mine_spawner.py
# ------------
# By Cory
# This module defines a mine spawner agent class. It spawns mines!
from random import randint
from mine import Mine
class MineSpawner:
display = False
def __init__(self, board_size):
width, height = board_size
self.board_width, self.board_height = width, height
def play_turn(self, game):
if game.turn_number == 1:
mine = Mine(((randint(0, self.board_width - 1)),(randint(0, self.board_height - 1))))
game.log(mine.position)
game.add_agent(mine)

View File

@ -4,9 +4,11 @@
# This class implements a simple minesweeper game on a 9x9 gird.
from retro.game import Game
from cursor import Cursor
from mine_spawner import MineSpawner
board_size = (9, 9)
cursor = Cursor(board_size)
spawner = MineSpawner(board_size)
# spawner = AsteroidSpawner(board_size)
game = Game([cursor], {"score": 0}, board_size=board_size)
game = Game([cursor,spawner], {"score": 0}, board_size=board_size,debug=True)
game.play()