regenerates mines and snacks

may still try to add more difficulty and update any docstrings and old commented code
This commit is contained in:
Chris Mekelburg
2024-12-08 21:31:20 -05:00
parent 729e1085ee
commit 947f9647ba
16 changed files with 208 additions and 27 deletions

Binary file not shown.

View File

@@ -2,16 +2,19 @@ from man import Man
from snack import Snack
from mine import Mine
from random import shuffle
from retro import game
class Board:
display = False
snack=False
mine=False
def __init__(self,width, height,num_snacks,num_mines):
self.width = width
self.height = height
self.num_snacks = num_snacks
self.num_mines = num_mines
def get_agents(self,num_snacks,num_mines):
all_positions = self.get_all_positions()
@@ -19,8 +22,51 @@ class Board:
man= [Man(all_positions[0])]
snacks = [Snack(p) for p in all_positions[1:(num_snacks+1)]]
mines = [Mine(p) for p in all_positions[(num_snacks + 2):(num_snacks + num_mines +2)]]
agents = man + snacks + mines
agents = man + snacks + mines + [self]
return agents
def play_turn(self,game):
game.log(game.turn_number)
while self.game_needs_snacks(game):
self.add_snack(game)
while self.game_needs_mines(game):
self.add_mine(game)
def add_mine(self,game):
all_positions=self.get_all_positions()
shuffle(all_positions)
index =0
while not game.is_empty(all_positions[index]):
index = index + 1
mine = Mine(all_positions[index])
game.add_agent(mine)
def count_mines(self,game):
mines= [a for a in game.agents if a.mine]
return len(mines)
def game_needs_mines(self,game):
return self.count_mines(game) < self.num_mines
def add_snack(self,game):
all_positions=self.get_all_positions()
shuffle(all_positions)
index =0
while not game.is_empty(all_positions[index]):
index = index + 1
snack = Snack(all_positions[index])
game.add_agent(snack)
def count_snacks(self,game):
snacks= [a for a in game.agents if a.snack]
return len(snacks)
def game_needs_snacks(self,game):
return self.count_snacks(game) < self.num_snacks
'''
def get_agents(self,num_snacks,num_mines):
man = self.get_man(num_snacks,num_mines)

View File

@@ -17,7 +17,8 @@ direction_vectors = {
class Man:
character = "&" #try google asci full table?,
color = "blue"
#name = "man"
snack=False
mine=False
def __init__(self,position):
self.position = position
@@ -50,7 +51,6 @@ class Man:
if obstacle.snack:
game.state['Score'] += 1
game.remove_agent(agents[0])
self.generate_new_snack()
else:
game.state['Score'] -= 10
game.remove_agent(agents[0])
@@ -61,28 +61,16 @@ class Man:
else:
self.position = future_position
'''Indicates what happens when the game is over.'''
def die(self,game):
'''Indicates what happens when the game is over.'''
game.state["message"] = "Game Over"
self.color = "black"
game.end()
def get_all_positions(self):
positions=[]
for i in range(width):
for j in range(height):
positions.append((i,j))
return positions
'''issue here, is_empty is not generating a coordinate positon,
but I am not sure why it is not'''
def generate_new_snack(self):
all_positions=self.get_all_positions()
shuffle(all_positions)
index =0
while Game.is_empty(all_positions[index]) == False:
index = index + 1
Snack(all_positions[index])
'''
def __init__(self, position):
self.position = position

28
gamefiles/mine counter.py Normal file
View File

@@ -0,0 +1,28 @@
MAX_MINES = 50
LEVELS = [
[5, 10],
[50, 20],
[75, 30],
[100, 40]
]
def num_mines(score):
for limit, n in LEVELS:
if score < limit:
return n
return MAX_MINES
'''
if score < 5:
num_mines = 10
if 10 <= score <50:
num_mines = 20
if 50 <= score <75:
num_mines = 30
if 75 <= score <100:
num_mines = 40
#else:
#num_mines = 50
return num_mines
'''

View File

@@ -2,6 +2,7 @@ class Mine:
character = "*"
color = "green"
snack=False
mine=True
def __init__(self,position):

28
gamefiles/minecounter.py Normal file
View File

@@ -0,0 +1,28 @@
MAX_MINES = 50
LEVELS = [
[5, 10],
[50, 20],
[75, 30],
[100, 40]
]
def num_mines(score):
for limit, n in LEVELS:
if score < limit:
return n
return MAX_MINES
'''
if score < 5:
num_mines = 10
if 10 <= score <50:
num_mines = 20
if 50 <= score <75:
num_mines = 30
if 75 <= score <100:
num_mines = 40
#else:
#num_mines = 50
return num_mines
'''

View File

@@ -1,7 +1,6 @@
from retro.game import Game
from board import Board
from score import num_mines
from minecounter import num_mines
width = 25
height = 25
@@ -14,6 +13,6 @@ board = Board(width,height,num_snacks,num_mines)
game = Game(
board.get_agents(num_snacks, num_mines),
state,
board_size = (width, height)
board_size = (width, height),debug=True
)
game.play()

View File

@@ -1,6 +1,19 @@
MAX_MINES = 50
LEVELS = [
[5, 10],
[50, 20],
[75, 30],
[100, 40]
]
def num_mines(score):
for limit, n in LEVELS:
if score < limit:
return n
return MAX_MINES
'''
if score < 5:
num_mines = 10
if 10 <= score <50:
@@ -12,4 +25,4 @@ def num_mines(score):
#else:
#num_mines = 50
return num_mines
'''

View File

@@ -2,6 +2,7 @@ class Snack:
character = "o"
color = "red"
snack=True
mine=False
def __init__(self,position):