generated from mwc/project_game
Okay... I don't know why it doesn't work when
the mines can be hidden, so I have a janky workaround. You might have to try moving the cursor multiple times but at least now it actually works. Next step, add flagging!!!!
This commit is contained in:
parent
75618c1de1
commit
8e54c5fe12
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -26,4 +26,8 @@ class Cursor:
|
||||||
if game.is_empty(new_position):
|
if game.is_empty(new_position):
|
||||||
self.position = new_position
|
self.position = new_position
|
||||||
game.log("The cursor is at " + str(self.position))
|
game.log("The cursor is at " + str(self.position))
|
||||||
|
for i in range(10):
|
||||||
|
game.log("mine"+str(i) + " is located at " + str(game.get_agent_by_name("mine"+str(i)).position))
|
||||||
|
|
||||||
|
def hide(self):
|
||||||
|
pass
|
|
@ -17,21 +17,25 @@ class FreeSpace:
|
||||||
def handle_keystroke(self, keystroke, game):
|
def handle_keystroke(self, keystroke, game):
|
||||||
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):
|
||||||
board_width,board_height = game.board_size
|
if self.revealed == False:
|
||||||
for i in range(board_width):
|
board_width,board_height = game.board_size
|
||||||
for j in range(board_height):
|
for i in range(10):
|
||||||
try:
|
game.get_agent_by_name("mine"+str(i)).show()
|
||||||
game.get_agent_by_name("freespace"+str(i)+str(j)).show()
|
for i in range(board_width):
|
||||||
except:
|
for j in range(board_height):
|
||||||
pass
|
try:
|
||||||
self.reveal(game)
|
game.get_agent_by_name("freespace"+str(i)+str(j)).show()
|
||||||
for i in range(board_width):
|
except:
|
||||||
for j in range(board_height):
|
pass
|
||||||
try:
|
self.reveal(game)
|
||||||
game.get_agent_by_name("freespace"+str(i)+str(j)).hide()
|
for i in range(board_width):
|
||||||
except:
|
for j in range(board_height):
|
||||||
pass
|
try:
|
||||||
|
game.get_agent_by_name("freespace"+str(i)+str(j)).hide()
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
for i in range(10):
|
||||||
|
game.get_agent_by_name("mine"+str(i)).hide()
|
||||||
|
|
||||||
def hide(self):
|
def hide(self):
|
||||||
if self.revealed == False:
|
if self.revealed == False:
|
||||||
|
@ -87,7 +91,6 @@ class FreeSpace:
|
||||||
self.character = str(self.neighbors)
|
self.character = str(self.neighbors)
|
||||||
|
|
||||||
def reveal(self,game):
|
def reveal(self,game):
|
||||||
game.log("Revealing " + str(self.position))
|
|
||||||
self.revealed = True
|
self.revealed = True
|
||||||
if self.neighbors == 0:
|
if self.neighbors == 0:
|
||||||
if game.on_board((self.width + 1,self.height)):
|
if game.on_board((self.width + 1,self.height)):
|
||||||
|
@ -121,4 +124,4 @@ class FreeSpace:
|
||||||
if game.on_board((self.width - 1,self.height - 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 len(game.get_agents_by_position()[(self.width - 1,self.height - 1)]) != 0:
|
||||||
if not game.get_agents_by_position()[(self.width - 1,self.height - 1)][0].revealed:
|
if not game.get_agents_by_position()[(self.width - 1,self.height - 1)][0].revealed:
|
||||||
game.get_agents_by_position()[(self.width - 1,self.height - 1)][0].reveal(game)
|
game.get_agents_by_position()[(self.width - 1,self.height - 1)][0].reveal(game)
|
18
mine.py
18
mine.py
|
@ -4,7 +4,7 @@
|
||||||
# This module defines a mine agent class. It ends the game if enter is pressed while the cursor is over it.
|
# 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:
|
||||||
character = '_'
|
character = ''
|
||||||
revealed = True
|
revealed = True
|
||||||
|
|
||||||
def __init__(self, position):
|
def __init__(self, position):
|
||||||
|
@ -12,12 +12,18 @@ class Mine:
|
||||||
|
|
||||||
def handle_keystroke(self, keystroke, game):
|
def handle_keystroke(self, keystroke, game):
|
||||||
if keystroke.name in ("KEY_ENTER"):
|
if keystroke.name in ("KEY_ENTER"):
|
||||||
if not game.is_empty(self.position):
|
if game.get_agent_by_name("cursor").position == self.position:
|
||||||
game.log("You hit a mine at " + str(self.position) + ".")
|
game.log("You hit a mine! Game over.")
|
||||||
game.end()
|
game.end()
|
||||||
|
|
||||||
|
def play_turn(self, game):
|
||||||
|
if (not game.is_empty(self.position)):
|
||||||
|
self.display = False
|
||||||
|
elif game.is_empty(self.position):
|
||||||
|
self.display = True
|
||||||
|
|
||||||
def hide(self):
|
def hide(self):
|
||||||
self.display = False
|
pass
|
||||||
|
|
||||||
def name_me(self, named):
|
def name_me(self, named):
|
||||||
self.name = named
|
self.name = named
|
||||||
|
@ -25,8 +31,8 @@ class Mine:
|
||||||
def check_neighbors(self,game):
|
def check_neighbors(self,game):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def reveal():
|
def reveal(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def show():
|
def show(self):
|
||||||
pass
|
pass
|
|
@ -4,10 +4,9 @@
|
||||||
# This class implements a simple minesweeper game on a 9x9 grid.
|
# This class implements a simple minesweeper game on a 9x9 grid.
|
||||||
|
|
||||||
from retro.game import Game
|
from retro.game import Game
|
||||||
from spawner import Spawner
|
from spawner import Spawner
|
||||||
|
|
||||||
board_size = (9, 9)
|
board_size = (9, 9)
|
||||||
spawner = Spawner(board_size)
|
spawner = Spawner(board_size)
|
||||||
# spawner = AsteroidSpawner(board_size)
|
|
||||||
game = Game([spawner], {"score": 0}, board_size=board_size,debug=True)
|
game = Game([spawner], {"score": 0}, board_size=board_size,debug=True)
|
||||||
game.play()
|
game.play()
|
24
spawner.py
24
spawner.py
|
@ -18,21 +18,22 @@ class Spawner:
|
||||||
def play_turn(self, game):
|
def play_turn(self, game):
|
||||||
# On the first turn we will spawn everything.
|
# On the first turn we will spawn everything.
|
||||||
if game.turn_number == 1:
|
if game.turn_number == 1:
|
||||||
# First spawn free spaces everywhere
|
# First spawn 10 mines
|
||||||
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):
|
for i in range(10):
|
||||||
mine = Mine(((randint(0, self.board_width - 1)),(randint(0, self.board_height - 1))))
|
mine = Mine(((randint(0, self.board_width - 1)),(randint(0, self.board_height - 1))))
|
||||||
mine.name_me("mine"+str(i))
|
mine.name_me("mine"+str(i))
|
||||||
|
while not game.is_empty(mine.position):
|
||||||
|
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))
|
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)
|
game.add_agent(mine)
|
||||||
|
# Then spawn free spaces everywhere
|
||||||
|
for i in range(self.board_width):
|
||||||
|
for j in range(self.board_height):
|
||||||
|
if game.is_empty((i,j)):
|
||||||
|
free_space = FreeSpace((i,j))
|
||||||
|
free_space.name_me("freespace"+str(i)+str(j))
|
||||||
|
game.add_agent(free_space)
|
||||||
# Now ask all free spaces to keep track of their neighbors that are mines
|
# 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.
|
# and all of the mines and free spaces to hide themselves.
|
||||||
for i in range(self.board_width):
|
for i in range(self.board_width):
|
||||||
|
@ -41,4 +42,5 @@ class Spawner:
|
||||||
game.get_agents_by_position()[(i,j)][0].check_neighbors(game)
|
game.get_agents_by_position()[(i,j)][0].check_neighbors(game)
|
||||||
game.get_agents_by_position()[(i,j)][0].hide()
|
game.get_agents_by_position()[(i,j)][0].hide()
|
||||||
# Now create a cursor.
|
# Now create a cursor.
|
||||||
game.add_agent(Cursor((self.board_width - 1, self.board_height - 1)))
|
cursor = Cursor((self.board_width - 1, self.board_height - 1))
|
||||||
|
game.add_agent(cursor)
|
||||||
|
|
Loading…
Reference in New Issue