diff --git a/__pycache__/cursor.cpython-312.pyc b/__pycache__/cursor.cpython-312.pyc index d0d3dc8..a657f0f 100644 Binary files a/__pycache__/cursor.cpython-312.pyc and b/__pycache__/cursor.cpython-312.pyc differ diff --git a/__pycache__/free_space.cpython-312.pyc b/__pycache__/free_space.cpython-312.pyc new file mode 100644 index 0000000..5677651 Binary files /dev/null and b/__pycache__/free_space.cpython-312.pyc differ diff --git a/__pycache__/mine.cpython-312.pyc b/__pycache__/mine.cpython-312.pyc index 8950b8b..e770c25 100644 Binary files a/__pycache__/mine.cpython-312.pyc and b/__pycache__/mine.cpython-312.pyc differ diff --git a/__pycache__/mine_spawner.cpython-312.pyc b/__pycache__/mine_spawner.cpython-312.pyc index 12e8251..39cb7b2 100644 Binary files a/__pycache__/mine_spawner.cpython-312.pyc and b/__pycache__/mine_spawner.cpython-312.pyc differ diff --git a/cursor.py b/cursor.py index c4b5765..3d152b7 100644 --- a/cursor.py +++ b/cursor.py @@ -6,12 +6,12 @@ class Cursor: name = "cursor" character = 'O' - def __init__(self, board_size): - board_width, board_height = board_size - self.position = (board_width // 2, board_height - 1) + def __init__(self, position): + self.position = position def handle_keystroke(self, keystroke, game): x, y = self.position + if keystroke.name in ("KEY_LEFT", "KEY_RIGHT", "KEY_UP", "KEY_DOWN"): if keystroke.name == "KEY_LEFT": new_position = (x - 1, y) @@ -24,4 +24,4 @@ class Cursor: if game.on_board(new_position): if game.is_empty(new_position): self.position = new_position - game.log(self.position) \ No newline at end of file + game.log("The cursor is at " + str(self.position)) \ No newline at end of file diff --git a/free_space.py b/free_space.py new file mode 100644 index 0000000..db53f85 --- /dev/null +++ b/free_space.py @@ -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 \ No newline at end of file diff --git a/mine.py b/mine.py index 679d829..b8eefc9 100644 --- a/mine.py +++ b/mine.py @@ -1,9 +1,9 @@ # mine.py # ------------ # 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: - display = False + character = '_' def __init__(self, position): self.position = position @@ -12,4 +12,7 @@ class Mine: if keystroke.name in ("KEY_ENTER"): if not game.is_empty(self.position): game.log("You hit a mine at " + str(self.position) + ".") - game.end() \ No newline at end of file + game.end() + + def hide(self): + self.display = False \ No newline at end of file diff --git a/mine_spawner.py b/mine_spawner.py index ecdceb3..3c25a27 100644 --- a/mine_spawner.py +++ b/mine_spawner.py @@ -1,12 +1,14 @@ # mine_spawner.py # ------------ # 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 mine import Mine +from free_space import FreeSpace +from cursor import Cursor -class MineSpawner: +class Spawner: display = False def __init__(self, board_size): @@ -14,7 +16,26 @@ class MineSpawner: 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: - mine = Mine(((randint(0, self.board_width - 1)),(randint(0, self.board_height - 1)))) - game.log(mine.position) - game.add_agent(mine) \ No newline at end of file + # First spawn free spaces everywhere + for i in range(self.board_width): + 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))) + diff --git a/minesweeper_game.py b/minesweeper_game.py index b6f99b4..92f3894 100644 --- a/minesweeper_game.py +++ b/minesweeper_game.py @@ -3,12 +3,10 @@ # By Cory # 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 +from mine_spawner import Spawner board_size = (9, 9) -cursor = Cursor(board_size) -spawner = MineSpawner(board_size) +spawner = Spawner(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() \ No newline at end of file