Update #2, making some progress after being stuck

This commit is contained in:
Chris Mekelburg
2024-12-03 21:49:59 -05:00
parent b1541ddf77
commit cf8eefcb8b
15 changed files with 95 additions and 46 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

66
gamefiles/board.py Normal file
View File

@@ -0,0 +1,66 @@
from man import Man
from snack import Snack
from mine import Mine
from random import shuffle
from retro import game
class Board:
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()
shuffle(all_positions)
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
return agents
'''
def get_agents(self,num_snacks,num_mines):
man = self.get_man(num_snacks,num_mines)
snacks = self.get_snacks(num_snacks)
mines = self.get_mines(num_mines,num_snacks)
agents = snacks + mines + man
return agents
def get_man(self,num_snacks,num_mines):
#man_location = [Man(all_positions[0])]
#return man_location
shuffle(all_positions)
for position in all_positions:
#if position in self.get_snacks(num_snacks):
# all_positions.remove(position)
if position in self.get_mines(num_mines,num_snacks):
all_positions.remove(position)
man_location = [Man(all_positions[0])]
return man_location
def get_mines(self,num_mines, num_snacks):
all_positions = self.get_all_positions()
shuffle(all_positions)
for position in all_positions:
if position in self.get_snacks(num_snacks):
all_positions.remove(position)
mines = [Mine(p) for p in all_positions[1:num_mines+1]]
return mines
def get_snacks(self,num_snacks):
all_positions = self.get_all_positions()
shuffle(all_positions)
snacks = [Snack(p) for p in all_positions[1:num_snacks+1]]
return snacks
'''
def get_all_positions(self):
positions=[]
for i in range(self.width):
for j in range(self.height):
positions.append((i,j))
return positions

30
gamefiles/helpers.py Normal file
View File

@@ -0,0 +1,30 @@
#List of helper functions to move the man
def add(vec0, vec1):
"""Adds two vectors.
Got tired of doing this by hand.
"""
x0, y0 = vec0
x1, y1 = vec1
return (x0 + x1, y0 + y1)
def get_occupant(game, position):
"""Returns the agent at position, if there is one.
This function slightly simplifies the process of getting the
agent at a position: the game returns a list of agents at a position,
because some games allow more than one agent at a position.
"""
positions_with_agents = game.get_agents_by_position()
if position in positions_with_agents:
agents_at_position = positions_with_agents[position]
return agents_at_position[0]
def distance(vec0, vec1):
"""Returns the distance between two vectors, using the
"manhattan distance," or the distance if you can only
move in the x-direction or the y-direction, but not
diagonally. Just like walking blocks in Manhattan :)
"""
x0, y0 = vec0
x1, y1 = vec1
return abs(x1 - x0) + abs(y1 - y0)

86
gamefiles/man.py Normal file
View File

@@ -0,0 +1,86 @@
from retro.agent import ArrowKeyAgent
#from retro.game import Game
#from helpers import add, get_occupant
direction_vectors = {
"KEY_RIGHT": (1, 0),
"KEY_UP": (0, -1),
"KEY_LEFT": (-1, 0),
"KEY_DOWN": (0, 1),
}
class Man:
character = "&" #try google asci full table?,
color = "blue"
#name = "man"
def __init__(self,position):
self.position = position
'''Describes how a keystroke is received'''
def handle_keystroke(self, keystroke, game):
if keystroke.name in direction_vectors:
vector = direction_vectors[keystroke.name]
self.try_to_move(vector, game)
'''Checks if a space is avialable to move'''
def try_to_move(self, vector, game):
x,y = self.position
vx,vy = vector
future_position = (x +vx, y+vy)
on_board = game.on_board(future_position)
agents_by_position = game.get_agents_by_position()
agents = agents_by_position[future_position]
if len(agents)>0:
obstacle = agents[0]
else:
obstacle= None
if on_board:
if obstacle:
if obstacle.snack:
game.state['Score'] += 1
game.remove_agent(agents[0])
else:
game.state['Score'] -= 1
game.remove_agent(agents[0])
else:
self.position = future_position
def die(self,game):
game.state["message"] = "Game Over"
self.color = "black"
game.end()
'''
def __init__(self, position):
self.position = position
def handle_keystroke(self, keystroke, game):
if keystroke.name in direction_vectors:
vector = direction_vectors[keystroke.name]
self.try_to_move(vector, game)
def try_to_move(self, vector, game):
"""Tries to move the player in the direction of vector.
If the space is empty and it's on the board, then the move succeeds.
If the space is occupied, then if the occupant can be pushed, it gets
pushed and the move succeeds. Otherwise, the move fails.
"""
future_position = add(self.position, vector)
on_board = game.on_board(future_position)
obstacle = get_occupant(game, future_position)
if obstacle:
if obstacle.deadly:
self.die(game)
elif obstacle.handle_push(vector, game):
self.position = future_position
elif on_board:
self.position = future_position'''

8
gamefiles/mine.py Normal file
View File

@@ -0,0 +1,8 @@
class Mine:
character = "*"
color = "green"
snack=False
def __init__(self,position):
self.position = position

19
gamefiles/nav_game.py Normal file
View File

@@ -0,0 +1,19 @@
from retro.game import Game
from board import Board
width = 25
height = 25
num_snacks = 10
num_mines = 10
board = Board(width,height,num_snacks,num_mines)
score = 0
state= {"Score":0}
#man = Man(board_size)
game = Game(
board.get_agents(num_snacks, num_mines),
state,
board_size = (width, height)
)
#print(board.get_agents())
print(len(board.get_agents(10,10)))
game.play()

8
gamefiles/snack.py Normal file
View File

@@ -0,0 +1,8 @@
class Snack:
character = "o"
color = "red"
snack=True
def __init__(self,position):
self.position = position