-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSB_environment.py
206 lines (158 loc) · 5.56 KB
/
SB_environment.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import sys
from tabulate import tabulate
import copy
from os import system
class Sokoban:
# workerPos -> (Tuple) initial position of Worker
# boxPos -> (List of tuples) initial position of all boxes
# goal -> (List of tuples) position of all goals
def __init__(self,board,boxPos,goal,workerPos):
self.board=board
self.goal=goal
self.root=Node(None,boxPos,workerPos[0],workerPos[1])
def moves(self,node):
u=(-1,0)
d=(1,0)
l=(0,-1)
r=(0,1)
# moves=[(-1,0),(0,-1),(1,0),(0,1)]
moves=[
r,u,d,l
]
result=[]
for mv in moves:
newWPX=node.workerPosX +mv[0]
newWPY=node.workerPosY +mv[1]
if self.board[newWPX][newWPY]=="#":
continue
elif self.board[newWPX][newWPY]==" ":
boxIdx=-1
#Finding which box will move
for i in range(len(node.boxPos)):
if node.boxPos[i]==(newWPX,newWPY):
boxIdx=i
if boxIdx==-1:
result.append(Node(node,node.boxPos,newWPX,newWPY))
continue
newBPX=newWPX+mv[0]
newBPY=newWPY+mv[1]
if self.board[newBPX][newBPY]==" " and (newBPX,newBPY) not in node.boxPos:
newBoxPos=copy.deepcopy(node.boxPos)
newBoxPos[boxIdx]=(newBPX,newBPY)
result.append(Node(node,newBoxPos,newWPX,newWPY))
else:
continue
return result
# def is_valid_value(self,char):
# if (
# char == ' ' or # floor
# char == '#' or # wall
# char == '@' or # worker on floor
# char == '.' or # goal
# char == '*' or # box on goal
# char == '$' or # box
# char == '+' ): # worker on goal
# return True
# else:
# return False
# Level 1
# # # #
# # # #
# #
# $ #
# # # # # #
# $ $ #
# . . @ . . #
# $ #
# # # # #
# # # #
class Node:
def __init__(self,parent,boxPos,workerPosX,workerPosY):
self.parent=parent
self.boxPos=boxPos
self.workerPosX=workerPosX
self.workerPosY=workerPosY
try:
self.level=parent.level+1
except:
self.level=0
self.cost=0
# def __eq__(self,other):
# if (self.workerPosX,self.workerPosY)==(other.workerPosX,other.workerPosY) and set(self.boxPos)==set(self.boxPos):
# return True
# else:
# return False
def __lt__(self,other):
return self.cost<other.cost
# workerPos -> (Tuple) initial position of Worker
# boxPos -> (List of tuples) initial position of all boxes
# goal -> (List of tuples) position of all goals
def Print(self,SBobj,filename):
cls = lambda: system('cls')
board=copy.deepcopy(SBobj.board)
row=len(SBobj.board)
col=len(SBobj.board[0])
for i in range(row):
for j in range(col):
if (i,j) in self.boxPos :
board[i][j]="$"
if (i,j) in SBobj.goal :
board[i][j]="."
if (i,j) in self.boxPos :
board[i][j]="*"
board[self.workerPosX][self.workerPosY]="@"
if (self.workerPosX,self.workerPosY) in SBobj.goal:
board[self.workerPosX][self.workerPosY]="+"
config = board
table = tabulate(config, tablefmt="fancy_grid")
fp=open(filename,'a+',encoding="utf-8")
fp.write(table)
fp.write("\n")
fp.close()
if __name__=="__main__":
board=[
['#','#','#','#',' ',' ',' '],
['#',' ',' ','#','#','#',' '],
['#',' ',' ',' ',' ','#',' '],
['#',' ',' ',' ',' ','#',' '],
['#','#','#',' ','#','#','#'],
['#',' ',' ',' ',' ',' ','#'],
['#',' ',' ',' ',' ',' ','#'],
['#',' ',' ',' ',' ',' ','#'],
['#','#','#',' ',' ','#','#'],
[' ',' ','#','#','#','#',' ']]
boxPos=[(3,2) , (5,2) , (5,4) , (7,3),(8,3)] # write box position here
goalPos=[(6,1) , (6,2) , (6,4) , (6,5)] # write goal position here
workerPos=(6,3)
SBobj=Sokoban(board,boxPos,goalPos,workerPos)
children=SBobj.moves(SBobj.root)
for c in children:
c.Print(SBobj)
# mat=[
# ['0','1','2','3','4','5','6']
# 0 ['#','#','#','#',' ',' ',' '],
# 1 ['#',' ',' ','#','#','#',' '],
# 2 ['#',' ',' ',' ',' ','#',' '],
# 3 ['#',' ','$',' ',' ','#',' '],
# 4 ['#','#','#',' ','#','#','#'],
# 5 ['#',' ','$',' ','$',' ','#'],
# 6 ['#','.','.','@','.','.','#'],
# 7 ['#',' ',' ','$',' ',' ','#'],
# 8 ['#','#','#',' ',' ','#','#'],
# 9 [' ',' ','#','#','#','#',' '],
# ]
# temp=[
# ['0','1','2','3','4','5','6']
# 0 ['#','#','#','#',' ',' ',' '],
# 1 ['#',' ','$',' ',' ',' ',' '],
# 2 ['#',' ',' ',' ',' ',' ',' '],
# 3 ['#',' ',' ','$','$',' ',' '],
# 4 ['#',' ',' ','$','$',' ','#'],
# 5 ['#',' ',' ',' ',' ',' ','#'],
# 6 ['#',' ',' ','$','$',' ','#'],
# 7 ['#',' ','$','$',' ',' ','#'],
# 8 ['#','#','#',' ',' ','#','#'],
# 9 [' ',' ','#','#','#','#',' ']]
'''
i-1 & j-1 , i+1 & j-1 , i+1 & j+1 , i-1 & j+1
'''