-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.py
150 lines (129 loc) · 5.2 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
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
import Helper
class Board:
def __init__(self, Dimensions, KingPosition, CastlePosition, Walls, filename=None, path=None):
if path is None:
path = []
self.Dimensions = None
self.KingPosition = None
self.CastlePosition = None
self.Walls = None
self.path = path
if filename is not None:
self.file_constructor(filename)
else:
self.deepCopy(Dimensions, KingPosition, CastlePosition, Walls, path)
def not_parent(self, x):
return x not in self.path
def file_constructor(self, filename):
extracted = Helper.readJson(filename)
if not isinstance(extracted, str):
self.Dimensions = tuple(extracted['Dimensions'])
self.KingPosition = tuple(extracted['KingPosition'])
self.CastlePosition = tuple(extracted['CastlePosition'])
self.Walls = [tuple(x) for x in extracted['Walls']]
else:
raise ValueError(extracted)
def deepCopy(self, Dimensions, KingPosition, CastlePosition, Walls, path):
self.Dimensions = Dimensions
self.KingPosition = KingPosition
self.CastlePosition = CastlePosition
self.Walls = Walls
self.path = path[:]
def Equals(self, obj):
b = [
set(obj.path) == set(self.path),
self.CastlePosition == obj.CastlePosition
]
return all(b)
def print(self):
board = Helper.generateBoard(self.Dimensions, self.Walls,
self.CastlePosition, self.KingPosition)
valid_moves = self.check_moves() + self.check_moves_up()
for i, row in enumerate(board):
for j, cell in enumerate(row):
if cell == 'X':
print(f"{Helper.bcolors.MAGENTA}{cell}{Helper.bcolors.ENDC}", end=' ')
elif cell == 'O':
if (i + 1, j + 1) in valid_moves:
print(f"{Helper.bcolors.GREEN}{cell}{Helper.bcolors.ENDC}", end=' ')
else:
print(f"{Helper.bcolors.YELLOW}{cell}{Helper.bcolors.ENDC}", end=' ')
elif cell == 'C':
print(f"{Helper.bcolors.CYAN}{cell}{Helper.bcolors.ENDC}", end=' ')
elif cell == 'K':
print(f"{Helper.bcolors.BLUE}{cell}{Helper.bcolors.ENDC}", end=' ')
print()
def inside_board(self, position):
if not (self.Dimensions[0] >= position[0] >= 1 and self.Dimensions[1] >= position[1] >= 1):
return False
if position in self.Walls:
return False
else:
return True
def Solved(self):
if self.CastlePosition == self.KingPosition:
return True
else:
return False
def check_moves_left(self):
result = []
(i, j) = self.CastlePosition
while True: # checking the left
j -= 1
if not (self.inside_board((i, j)) and not (i, j) in self.Walls):
break
else:
result.append((i, j))
return result
def check_moves_right(self):
result = []
(i, j) = self.CastlePosition
while True: # checking the right
j += 1
if not (self.inside_board((i, j)) and not (i, j) in self.Walls):
break
else:
result.append((i, j))
return result
def check_moves_up(self):
result = []
(i, j) = self.CastlePosition
while self.inside_board((i, j)): # checking up
i -= 1
if not (self.inside_board((i, j)) and not (i, j) in self.Walls):
break
else:
result.append((i, j))
return result
def check_moves_down(self, position):
result = []
(i, j) = position
while self.inside_board((i, j)): # checking down
i += 1
if not (self.inside_board((i, j)) and not (i, j) in self.Walls):
break
else:
result.append((i, j))
return result
def check_moves(self):
return self.check_moves_left() + self.check_moves_right() + self.check_moves_up()
def move(self, position):
moves = self.check_moves()
if position in moves:
down = self.check_moves_down(position)
if not down:
return Board(self.Dimensions, self.KingPosition, position, self.Walls, None, self.path)
else:
if position == self.KingPosition:
return Board(self.Dimensions, self.KingPosition, position, self.Walls, None, self.path)
destination = down.pop()
return Board(self.Dimensions, self.KingPosition, (destination[0], destination[1]), self.Walls, None,
self.path)
else:
print(f"{position} is an Illegal Move.")
return None, []
def get_next_states(self):
next_states = []
for move in self.check_moves():
next_states.append(self.move(move)) # move function return a tuple consisting of the board
return next_states # object and the path it took to get where it went