-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
93 lines (80 loc) · 3.86 KB
/
board.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import copy
import actions
MOVES = ["U", "D", "L", "R"]
def gen_default_board(board_file_selection):
with open(board_file_selection) as board_file:
board = []
for line in board_file:
board.append(line.strip().split(","))
return board
def check_goal(sokoban_board):
for row in range(0,len(sokoban_board)):
for column in range(0,len(sokoban_board[row])):
# in these cases, we have an agent on a goal or a goal without
# anything on it, indicating a failure
# the goal state is when there are no goal squares without a box
if sokoban_board[row][column] == "1":
return False
if sokoban_board[row][column] == "0":
return False
return True
def draw_board(sokoban_board):
for i in range(0,len(sokoban_board)):
print(sokoban_board[i])
def draw_frontier(sokoban_frontier):
for i in range(0, len(sokoban_frontier), 2):
print("Direction: " + str(sokoban_frontier[i]))
draw_board(sokoban_frontier[i+1])
def gen_frontier(frontier_board):
current_frontier = []
for i in range(0, len(MOVES)):
board_frontier = copy.deepcopy(frontier_board)
result = []
if board_frontier != True and board_frontier != False:
result = actions.move_agent(board_frontier, MOVES[i])
if result:
current_frontier.append(MOVES[i])
current_frontier.append(result)
return current_frontier
# for MCTS, find out if our board is in a loss state:
# a loss is when we have a box in a corner, which means it cannot be moved
# so we cannot recover the box, and our current state is unsolvable
def check_loss(board):
for row in range(0, len(board)):
for column in range(0, len(board[row])):
if board[row][column] == "3":
# we have a box, let's see if it is permanently stuck
# we check simple deadlock
if board[row+1][column] == "-1" and board[row][column-1] == "-1":
return True
if board[row+1][column] == "-1" and board[row][column+1] == "-1":
return True
if board[row-1][column] == "-1" and board[row][column-1] == "-1":
return True
if board[row-1][column] == "-1" and board[row][column+1] == "-1":
return True
'''if board[row][column-1] == "-1" and board[row+1][column] == "-1":
# corner blocking from left and bottom
return True
if board[row][column-1] == "-1" and board[row-1][column] == "-1":
# corner blocking from left and above
return True
if board[row-1][column] == "-1" and board[row][column+1] == "-1":
# corner blocking from right and above
return True
if board[row+1][column] == "-1" and board[row][column+1] == "-1":
# corner blocking from right and below
return True'''
return False
# we check freeze deadlock
def freeze_deadlock(board):
for row in range(0, len(board)):
for column in range(0, len(board[row])):
if board[row][column] == "3":
if board[row][column+1] == "3" and ((board[row-1][column] == "-1" or "2") or (board[row+1][column] == "-1" or "2")):
if board[row-1][column+1] == "-1" or board[row+1][column+1] == "-1":
return True
if board[row+1][column] == "3" and ((board[row][column-1] == "-1" or "2") or (board[row][column+1] == "-1" or "2")):
if board[row+1][column-1] == "-1" or board[row+1][column+1] == "-1":
return True
return False