wersja bez przeszukiwania stanow
This commit is contained in:
parent
6c9bda39e6
commit
077f1720d0
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -40,40 +40,8 @@ class Board:
|
||||
x_y = self.free_spaces.pop()
|
||||
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2), True)
|
||||
|
||||
#generowanie przestrzeni stanów
|
||||
# dla każdego pola (nie będącego drzewem) tworzę 4 stany dla 4 pozycji obrotu
|
||||
# potem dla każdego stanu dodaję listę stanów sąsiadujących i połączenia te nazywam "rotate_left" "rotate_right" "move"
|
||||
self.set_of_states = []
|
||||
id_couunt = 0
|
||||
for x in range(self.col):
|
||||
for y in range(self.row):
|
||||
if self.board[x][y] == False or self.board[x][y].name != "Tree":
|
||||
for angle in range(4):
|
||||
self.set_of_states.append(State(id_couunt, x, y, angle))
|
||||
id_couunt += 1
|
||||
|
||||
self.set_of_states[id_couunt-4].adjacent_states.append((id_couunt-3, "rotate_left"))
|
||||
self.set_of_states[id_couunt-4].adjacent_states.append((id_couunt-1, "rotate_right"))
|
||||
|
||||
self.set_of_states[id_couunt-3].adjacent_states.append((id_couunt-4, "rotate_right"))
|
||||
self.set_of_states[id_couunt-3].adjacent_states.append((id_couunt-2, "rotate_left"))
|
||||
|
||||
self.set_of_states[id_couunt-2].adjacent_states.append((id_couunt-3, "rotate_right"))
|
||||
self.set_of_states[id_couunt-2].adjacent_states.append((id_couunt-1, "rotate_left"))
|
||||
|
||||
self.set_of_states[id_couunt-1].adjacent_states.append((id_couunt-4, "rotate_left"))
|
||||
self.set_of_states[id_couunt-1].adjacent_states.append((id_couunt-2, "rotate_right"))
|
||||
|
||||
for state in self.set_of_states:
|
||||
for s in self.set_of_states:
|
||||
if state.angle == s.angle == 0 and state.x == s.x and state.y - 1 == s.y: state.adjacent_states.append((s.id, "move"))
|
||||
elif state.angle == s.angle == 1 and state.x - 1 == s.x and state.y == s.y: state.adjacent_states.append((s.id, "move"))
|
||||
elif state.angle == s.angle == 2 and state.x == s.x and state.y + 1 == s.y: state.adjacent_states.append((s.id, "move"))
|
||||
elif state.angle == s.angle == 3 and state.x + 1 == s.x and state.y == s.y: state.adjacent_states.append((s.id, "move"))
|
||||
|
||||
|
||||
self.graph = None
|
||||
self.path = []
|
||||
self.grow_next = 0
|
||||
|
||||
def update_free_spaces(self):
|
||||
self.free_spaces = []
|
||||
@ -121,74 +89,43 @@ class Board:
|
||||
textsurface = myfont.render("a: "+str(self.agent.angle), False, (0, 0, 0))
|
||||
win.blit(textsurface,(self.x+200,self.y-40))
|
||||
|
||||
# id stanu agenta
|
||||
state = 0
|
||||
for s in self.set_of_states:
|
||||
if self.agent.col == s.x and self.agent.row == s.y and self.agent.angle == s.angle: state = s.id
|
||||
|
||||
textsurface = myfont.render("s: "+str(state), False, (0, 0, 0))
|
||||
win.blit(textsurface,(self.x+300,self.y-40))
|
||||
|
||||
|
||||
def update_agent(self):
|
||||
self.agent.update_animation()
|
||||
|
||||
if self.board[self.agent.col][self.agent.row] != False and self.board[self.agent.col][self.agent.row].name == "Mushroom":
|
||||
#zebranie grzyba
|
||||
if self.board[self.agent.col][self.agent.row] != False and self.board[self.agent.col][self.agent.row].name == "Mushroom" and self.board[self.agent.col][self.agent.row].grow == 100:
|
||||
self.agent.points += self.board[self.agent.col][self.agent.row].points
|
||||
self.board[self.agent.col][self.agent.row] = False
|
||||
self.free_spaces.append((self.agent.col, self.agent.row))
|
||||
|
||||
# wykonaj następny ruch na ścieżce
|
||||
if len(self.path) > 0:
|
||||
if self.agent.moving == self.agent.rotating == 0:
|
||||
move = self.path.pop(0)
|
||||
def grow_mushrooms(self):
|
||||
numer = 0
|
||||
numer_poison = 0
|
||||
|
||||
for x in range(self.col):
|
||||
for y in range(self.row):
|
||||
if self.board[x][y] != False and self.board[x][y].name == "Mushroom":
|
||||
if self.board[x][y].grow < 100: self.board[x][y].grow += 1
|
||||
|
||||
if self.board[x][y].poison: numer_poison += 1
|
||||
else: numer += 1
|
||||
|
||||
if self.grow_next < 100: self.grow_next += 1
|
||||
elif random.random() > 0.5 and numer < MUSHROOMS_MAX:
|
||||
self.grow_next = 0
|
||||
self.update_free_spaces()
|
||||
x_y = self.free_spaces.pop()
|
||||
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2), False, 0)
|
||||
elif numer_poison < POISON_MUSHROOMS_MAX:
|
||||
self.grow_next = 0
|
||||
self.update_free_spaces()
|
||||
x_y = self.free_spaces.pop()
|
||||
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2), True, 0)
|
||||
|
||||
|
||||
print(f"execute {move}")
|
||||
|
||||
if move[1] == "move": self.agent.move()
|
||||
elif move[1] == "rotate_left": self.agent.rotate(1)
|
||||
elif move[1] == "rotate_right": self.agent.rotate(-1)
|
||||
|
||||
def next_move(self):
|
||||
start_state = 0
|
||||
|
||||
for state in self.set_of_states:
|
||||
if state.x == self.agent.col and state.y == self.agent.row and state.angle == self.agent.angle: start_state = state.id
|
||||
|
||||
self.graph = Graph(start_state)
|
||||
|
||||
state_to_add = [start_state]
|
||||
|
||||
self.path = []
|
||||
|
||||
# BFS
|
||||
while len(state_to_add) > 0:
|
||||
state = state_to_add.pop(0)
|
||||
print(f"checking {state}, x:{self.set_of_states[state].x}, y:{self.set_of_states[state].y}, a:{self.set_of_states[state].angle}")
|
||||
|
||||
self.graph.visit(state)
|
||||
|
||||
# test czy znaleźliśmy grzyba
|
||||
if self.board[self.set_of_states[state].x][self.set_of_states[state].y] != False and self.board[self.set_of_states[state].x][self.set_of_states[state].y].name == "Mushroom" and self.board[self.set_of_states[state].x][self.set_of_states[state].y].poison == False:
|
||||
self.path = self.graph.get_path(state)
|
||||
break
|
||||
|
||||
# dodawanie kolejnych stanów
|
||||
for s in self.set_of_states[state].adjacent_states:
|
||||
print(f"\t checking {s[0]}, x:{self.set_of_states[s[0]].x}, y:{self.set_of_states[s[0]].y}, a:{self.set_of_states[s[0]].angle}, s:{s[1]}")
|
||||
if self.graph.is_visited(s[0]) == False:
|
||||
self.graph.add(s[0], state, s[1])
|
||||
state_to_add.append(s[0])
|
||||
print(f"\t add {s[0]} {s[1]}")
|
||||
|
||||
#print(self.path)
|
||||
print("-------------------------")
|
||||
|
||||
#for v in self.graph.vertices:
|
||||
# print(f" id:{v.id} parent:{v.parent} move:{v.move}, visited:{v.visited}")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -11,8 +11,8 @@ ROWS = 14
|
||||
MUSHROOMS_START = 5
|
||||
MUSHROOMS_MAX = 10
|
||||
POISON_MUSHROOMS_START = 5
|
||||
POISON_MUSHROOMS_MAX = 10
|
||||
TREES = 50
|
||||
POISON_MUSHROOMS_MAX = 40
|
||||
TREES = 80
|
||||
|
||||
|
||||
#colors in game in rgb
|
||||
|
@ -1,55 +0,0 @@
|
||||
class Vertex():
|
||||
def __init__(self, id, parent, depth, move):
|
||||
self.id = id
|
||||
self.parent = parent
|
||||
self.child = []
|
||||
self.visited = False
|
||||
self.depth = depth
|
||||
self.move = move
|
||||
|
||||
|
||||
class Graph():
|
||||
def __init__(self, root):
|
||||
self.root = root
|
||||
self.vertices = [Vertex(root, None, 0, None)]
|
||||
|
||||
def add(self, id, parent, move):
|
||||
for v in self.vertices:
|
||||
if v.id == id and v.parent == parent:
|
||||
return
|
||||
|
||||
for v in self.vertices:
|
||||
if v.id == parent:
|
||||
self.vertices.append(Vertex(id, parent, v.depth+1, move))
|
||||
v.child.append(id)
|
||||
return
|
||||
|
||||
def visit(self, id):
|
||||
for v in self.vertices:
|
||||
if v.id == id: v.visited = True
|
||||
|
||||
def is_visited(self, id):
|
||||
for v in self.vertices:
|
||||
if v.id == id: return v.visited
|
||||
return False
|
||||
|
||||
def get_path(self, id):
|
||||
result = []
|
||||
next = None
|
||||
for v in self.vertices:
|
||||
if v.id == id:
|
||||
result.append([v.id, v.move])
|
||||
next = v.parent
|
||||
|
||||
while next != None:
|
||||
for v in self.vertices:
|
||||
if v.id == next:
|
||||
result.append([v.id, v.move])
|
||||
next = v.parent
|
||||
return result[::-1]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -2,12 +2,12 @@ from container.piece import Piece
|
||||
import pygame
|
||||
|
||||
class Mushroom(Piece):
|
||||
def __init__(self,x_y, img = 0, poison = False):
|
||||
def __init__(self,x_y, img = 0, poison = False, grow = 100):
|
||||
self.name = "Mushroom"
|
||||
self.col = x_y[0]
|
||||
self.row = x_y[1]
|
||||
self.poison = poison
|
||||
|
||||
self.grow = grow
|
||||
|
||||
|
||||
self.img = pygame.image.load(r'container\mushrooms\m'+str(img)+'.png')
|
||||
@ -16,6 +16,10 @@ class Mushroom(Piece):
|
||||
self.points = img+1
|
||||
if poison: self.points *= -1
|
||||
|
||||
def draw(self, win, square_size):
|
||||
x = (self.col+0.5*(100-self.grow)/100)*square_size[0]
|
||||
y = (self.row+0.5*(100-self.grow)/100)*square_size[1]
|
||||
win.blit(pygame.transform.scale(self.img, (int(square_size[0]/100*self.grow), int(square_size[1]/100*self.grow))), ((x, y)))
|
||||
|
||||
|
||||
|
||||
|
@ -7,12 +7,6 @@ class Piece:
|
||||
self.col = x_y[0]
|
||||
self.row = x_y[1]
|
||||
self.img = ''
|
||||
self.mushroom = False
|
||||
self.isSbThere = False
|
||||
|
||||
def change_status(self):
|
||||
print(self.isSbThere, self.row, self.col)
|
||||
self.isSbThere = not self.isSbThere
|
||||
|
||||
def draw(self, win, square_size):
|
||||
win.blit(pygame.transform.scale(self.img, (int(square_size[0]), int(square_size[1]))), (self.col*square_size[0], self.row*square_size[1]))
|
||||
|
@ -1,9 +0,0 @@
|
||||
class State():
|
||||
def __init__(self, id, x, y, angle):
|
||||
self.id = id
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.angle = angle
|
||||
|
||||
self.adjacent_states = []
|
||||
|
4
main.py
4
main.py
@ -26,8 +26,6 @@ def main():
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
run = False
|
||||
if event.type == pygame.KEYUP:
|
||||
if event.key == pygame.K_SPACE: board.next_move()
|
||||
|
||||
|
||||
#managing arrow click
|
||||
@ -40,6 +38,8 @@ def main():
|
||||
#drawing map and detective
|
||||
WIN.fill(GREEN)
|
||||
board.update_agent()
|
||||
board.grow_mushrooms()
|
||||
|
||||
board.draw_squares(WIN)
|
||||
board.draw_pieces(WIN)
|
||||
board.draw_agent(WIN)
|
||||
|
Loading…
Reference in New Issue
Block a user