I made everything spawn on turn 1 using the

Spawner class. When you press enter, you're told
if the space is clear or is a mine, in which
case you lose. Next step is to reveal the numbers.
This commit is contained in:
Cory 2024-05-18 21:59:57 -04:00
parent cb8b28b68c
commit 71e22504ab
9 changed files with 57 additions and 17 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -6,12 +6,12 @@ class Cursor:
name = "cursor" name = "cursor"
character = 'O' character = 'O'
def __init__(self, board_size): def __init__(self, position):
board_width, board_height = board_size self.position = position
self.position = (board_width // 2, board_height - 1)
def handle_keystroke(self, keystroke, game): def handle_keystroke(self, keystroke, game):
x, y = self.position x, y = self.position
if keystroke.name in ("KEY_LEFT", "KEY_RIGHT", "KEY_UP", "KEY_DOWN"): if keystroke.name in ("KEY_LEFT", "KEY_RIGHT", "KEY_UP", "KEY_DOWN"):
if keystroke.name == "KEY_LEFT": if keystroke.name == "KEY_LEFT":
new_position = (x - 1, y) new_position = (x - 1, y)
@ -24,4 +24,4 @@ class Cursor:
if game.on_board(new_position): if game.on_board(new_position):
if game.is_empty(new_position): if game.is_empty(new_position):
self.position = new_position self.position = new_position
game.log(self.position) game.log("The cursor is at " + str(self.position))

18
free_space.py Normal file
View File

@ -0,0 +1,18 @@
# free_space.py
# ------------
# By Cory
# This module defines a free space agent class. It displays if enter is pressed while the cursor is on it.
class FreeSpace:
character = '*'
display = True
def __init__(self, position):
self.position = 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!")
def hide(self):
self.display = False

View File

@ -1,9 +1,9 @@
# mine.py # mine.py
# ------------ # ------------
# By Cory # By Cory
# This module defines a mine agent class. It doesn't do anything except exist in a position # This module defines a mine agent class. It ends the game if enter is pressed while the cursor is over it.
class Mine: class Mine:
display = False character = '_'
def __init__(self, position): def __init__(self, position):
self.position = position self.position = position
@ -12,4 +12,7 @@ class Mine:
if keystroke.name in ("KEY_ENTER"): if keystroke.name in ("KEY_ENTER"):
if not game.is_empty(self.position): if not game.is_empty(self.position):
game.log("You hit a mine at " + str(self.position) + ".") game.log("You hit a mine at " + str(self.position) + ".")
game.end() game.end()
def hide(self):
self.display = False

View File

@ -1,12 +1,14 @@
# mine_spawner.py # mine_spawner.py
# ------------ # ------------
# By Cory # By Cory
# This module defines a mine spawner agent class. It spawns mines! # This module defines a spawner agent class. It spawns mines and then fills the remaining spots with free spaces
from random import randint from random import randint
from mine import Mine from mine import Mine
from free_space import FreeSpace
from cursor import Cursor
class MineSpawner: class Spawner:
display = False display = False
def __init__(self, board_size): def __init__(self, board_size):
@ -14,7 +16,26 @@ class MineSpawner:
self.board_width, self.board_height = width, height self.board_width, self.board_height = width, height
def play_turn(self, game): def play_turn(self, game):
# On the first turn we will spawn everything.
if game.turn_number == 1: if game.turn_number == 1:
mine = Mine(((randint(0, self.board_width - 1)),(randint(0, self.board_height - 1)))) # First spawn free spaces everywhere
game.log(mine.position) for i in range(self.board_width):
game.add_agent(mine) for j in range(self.board_height):
free_space = FreeSpace((i,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)
# 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.
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].hide()
# Now create a cursor.
game.add_agent(Cursor((self.board_width - 1, self.board_height - 1)))

View File

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