Spaces:
Runtime error
Runtime error
Sasha Rush
commited on
Commit
•
b33ab01
1
Parent(s):
0fddd3d
Create world.py
Browse files
world.py
ADDED
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List, Tuple, Optional
|
2 |
+
from enum import Enum
|
3 |
+
|
4 |
+
class Actions(Enum):
|
5 |
+
UPRIGHT = "UR"
|
6 |
+
RIGHT = "R"
|
7 |
+
DOWNRIGHT = "DR"
|
8 |
+
DOWNLEFT = "DL"
|
9 |
+
LEFT = "L"
|
10 |
+
UPLEFT = "UL"
|
11 |
+
PICKUP = "Pickup"
|
12 |
+
# Add more actions here if needed
|
13 |
+
|
14 |
+
class Board:
|
15 |
+
def __init__(self, grid: List[str], player_pos: Tuple[int, int],
|
16 |
+
flag_pos: Tuple[int, int], wall_pos:List[Tuple[int, int]],
|
17 |
+
key_pos:Optional[Tuple[int, int]]):
|
18 |
+
self.grid = grid
|
19 |
+
self.player_pos = player_pos
|
20 |
+
self.flag_pos = flag_pos
|
21 |
+
self.wall_pos = wall_pos
|
22 |
+
self.key_pos = key_pos
|
23 |
+
|
24 |
+
@staticmethod
|
25 |
+
def change(action):
|
26 |
+
if action == Actions.UPRIGHT:
|
27 |
+
dx, dy = -1, 1
|
28 |
+
elif action == Actions.UPLEFT:
|
29 |
+
dx, dy = -1, -1
|
30 |
+
elif action == Actions.DOWNLEFT:
|
31 |
+
dx, dy = 1, -1
|
32 |
+
elif action == Actions.DOWNRIGHT:
|
33 |
+
dx, dy = 1, 1
|
34 |
+
elif action == Actions.LEFT:
|
35 |
+
dx, dy = 0, -2
|
36 |
+
elif action == Actions.RIGHT:
|
37 |
+
dx, dy = 0, 2
|
38 |
+
elif action == Actions.PICKUP:
|
39 |
+
dx, dy = 0, 0
|
40 |
+
return dx, dy
|
41 |
+
|
42 |
+
def move(self, action: Actions) -> 'Board':
|
43 |
+
dx, dy = 0, 0
|
44 |
+
if action == Actions.UPRIGHT:
|
45 |
+
dx, dy = -1, 1
|
46 |
+
elif action == Actions.UPLEFT:
|
47 |
+
dx, dy = -1, -1
|
48 |
+
elif action == Actions.DOWNLEFT:
|
49 |
+
dx, dy = 1, -1
|
50 |
+
elif action == Actions.DOWNRIGHT:
|
51 |
+
dx, dy = 1, 1
|
52 |
+
elif action == Actions.LEFT:
|
53 |
+
dx, dy = 0, -2
|
54 |
+
elif action == Actions.RIGHT:
|
55 |
+
dx, dy = 0, 2
|
56 |
+
elif action == Actions.PICKUP:
|
57 |
+
dx, dy = 0, 0
|
58 |
+
if self.player_pos[0] == self.key_pos[0] and self.player_pos[1] == self.key_pos[1]:
|
59 |
+
return Board(self.grid, self.player_pos, self.flag_pos, self.wall_pos, None), "pickup"
|
60 |
+
else:
|
61 |
+
return self, "nokey"
|
62 |
+
|
63 |
+
else:
|
64 |
+
# Handle other actions here if needed
|
65 |
+
print("fail")
|
66 |
+
|
67 |
+
new_player_pos = (self.player_pos[0] + dx, self.player_pos[1] + dy)
|
68 |
+
if self.grid[new_player_pos[0]][new_player_pos[1]] == 'W':
|
69 |
+
# Can't move through walls
|
70 |
+
return self, "WALL"
|
71 |
+
|
72 |
+
new_grid = [row[:] for row in self.grid] # Create a copy of the grid
|
73 |
+
new_grid[self.player_pos[0]][self.player_pos[1]] = '.'
|
74 |
+
new_grid[new_player_pos[0]][new_player_pos[1]] = '@'
|
75 |
+
return Board(new_grid, new_player_pos, self.flag_pos, self.wall_pos, self.key_pos), "Good"
|
76 |
+
|
77 |
+
def create_wall(self, pos: Tuple[int, int]) -> 'Board':
|
78 |
+
if self.grid[pos[0]][pos[1]] in ( '@', 'P'):
|
79 |
+
# Can't place a wall on top of another object
|
80 |
+
return self
|
81 |
+
|
82 |
+
new_grid = [row[:] for row in self.grid] # Create a copy of the grid
|
83 |
+
new_grid[pos[0]][pos[1]] = "W"
|
84 |
+
return Board(new_grid, self.player_pos, self.flag_pos, self.wall_pos + [pos], self.key_pos)
|
85 |
+
|
86 |
+
|
87 |
+
def __str__(self) -> str:
|
88 |
+
# return '\n'.join((' ' * i %2) + ' '.join(row) for i, row in enumerate(self.grid))
|
89 |
+
return '\n'.join(''.join(row) for i, row in enumerate(self.grid))
|
90 |
+
|
91 |
+
def illegal_moves(self):
|
92 |
+
for action in Actions:
|
93 |
+
dx, dy = self.change(action)
|
94 |
+
new_player_pos = (self.player_pos[0] + dx, self.player_pos[1] + dy)
|
95 |
+
if new_player_pos[0] < 0 or new_player_pos[0] > self.flag_pos[0]:
|
96 |
+
#yield action, f"assert not 0 <= {new_player_pos[0]} < {self.flag_pos[0] + 1}"
|
97 |
+
continue
|
98 |
+
if new_player_pos[1] < 0 or new_player_pos[1] > self.flag_pos[1]:
|
99 |
+
#yield action, f"assert not 0 <= {new_player_pos[1]} < {self.flag_pos[0] + 1}"
|
100 |
+
continue
|
101 |
+
if action == Actions.PICKUP and not(self.key_pos is not None and self.player_pos[0] == self.key_pos[0] and self.player_pos[1] == self.key_pos[1]):
|
102 |
+
yield action, f"assert {new_player_pos} != board.key"
|
103 |
+
continue
|
104 |
+
if self.grid[new_player_pos[0]][new_player_pos[1]] == 'W':
|
105 |
+
yield action, f"assert {new_player_pos} in board.walls"
|
106 |
+
continue
|
107 |
+
continue
|
108 |
+
|
109 |
+
def board_state(self) -> str:
|
110 |
+
walls = ",".join(map(str, self.wall_pos))
|
111 |
+
return f"Flag: {self.flag_pos} | Walls (illegal): {walls} | Boundary: {add(self.flag_pos, (1, 1))} | Key: {self.key_pos}"
|
112 |
+
def board_state2(self) -> str:
|
113 |
+
walls = ",".join(map(str, self.wall_pos))
|
114 |
+
return f"init={self.player_pos}, flag={self.flag_pos}, walls= {self.wall_pos}, boundary= {add(self.flag_pos, (1, 1))}, key= {self.key_pos}"
|
115 |
+
|
116 |
+
def player_state(self) -> str:
|
117 |
+
msg = " K" if self.key_pos is None else ""
|
118 |
+
return f"{self.player_pos}{msg}"
|
119 |
+
|
120 |
+
@classmethod
|
121 |
+
def create_empty_board(cls, size: Tuple[int, int], key_pos, flag_pos, init) -> 'Board':
|
122 |
+
grid = [['.' if i % 2 == j % 2 else " " for i in range(size[1])] for j in range(size[0])]
|
123 |
+
player_pos = init
|
124 |
+
flag_pos = flag_pos
|
125 |
+
grid[player_pos[0]][player_pos[1]] = '@'
|
126 |
+
grid[flag_pos[0]][flag_pos[1]] = 'P'
|
127 |
+
grid[key_pos[0]][key_pos[1]] = 'K'
|
128 |
+
return cls(grid, player_pos, flag_pos, [], key_pos)
|
129 |
+
def add(a, b):
|
130 |
+
return a[0] + b[0], a[1] + b[1]
|
131 |
+
|
132 |
+
class GameBoard:
|
133 |
+
def __init__(self, init, flag, walls, key, boundary):
|
134 |
+
self.board = Board.create_empty_board(boundary, key, flag, init)
|
135 |
+
for wall in walls:
|
136 |
+
self.board = self.board.create_wall(wall)
|
137 |
+
self.original = self.board
|
138 |
+
|
139 |
+
self.actions = []
|
140 |
+
def move(self, action):
|
141 |
+
self.board, _ = self.board.move(action)
|
142 |
+
self.actions.append(action)
|
143 |
+
|
144 |
+
@property
|
145 |
+
def walls(self):
|
146 |
+
return self.board.wall_pos
|