-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpegSolitaireUtils.py
executable file
·189 lines (143 loc) · 6.68 KB
/
pegSolitaireUtils.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
import readGame
class game:
def __init__(self, filePath):
self.gameState = readGame.readGameState(filePath)
self.nodesExpanded = 0
self.trace = []
def get_gameState(self):
return self.gameState
def manhattan_distance_heuristic(self):
"""This heuristic calculates the sum of manhattan distances
of all the pegs from the centre peg (3,3)
"""
value = 0
for i in range(0,7):
for j in range(0,7):
if self.gameState[i][j] == 1:
value += abs(i - 3) + abs(j - 3)
return value
def possible_moves_heuristic(self):
"""This heuristic increments the cost by 1 for every move which
is possible from that given state.
"""
value = 0
for i in range(0,7):
for j in range(0,7):
if self.gameState[i][j] == 1:
if self.is_validMove((i,j),'S'):
value = value + 1
if self.is_validMove((i,j),'N'):
value = value + 1
if self.is_validMove((i,j),'E'):
value = value + 1
if self.is_validMove((i,j),'W'):
value = value + 1
return value
def is_corner(self, pos):
# Checking if the given position is out of the board.
if pos[0] < 0 or pos[0] > 6 or pos[1] < 0 or pos[1] > 6:
return True
# Checking if the given position is in top left 2 x 2 square.
for i in range(0,2):
for j in range(0,2):
if pos == (i,j):
return True
# Checking if the given position is in bottom left 2 x 2 square.
for i in range(5,7):
for j in range(0,2):
if pos == (i,j):
return True
# Checking if the given position is in top right 2 x 2 square.
for i in range(0,2):
for j in range(5,7):
if pos == (i,j):
return True
# Checking if the given position is in bottom right 2 x 2 square.
for i in range(5,7):
for j in range(5,7):
if pos == (i,j):
return True
return False
def getNextPosition(self, oldPos, direction):
# This function just returns the next position from the current
# position in the given direction.
# It does not check the validity of the position.
newPos = list(oldPos)
if(direction == 'N'):
newPos[0] = oldPos[0] - 2
newPos[1] = oldPos[1]
if(direction == 'S'):
newPos[0] = oldPos[0] + 2
newPos[1] = oldPos[1]
if(direction == 'E'):
newPos[0] = oldPos[0]
newPos[1] = oldPos[1] + 2
if(direction == 'W'):
newPos[0] = oldPos[0]
newPos[1] = oldPos[1] - 2
return tuple(newPos)
def is_validMove(self, oldPos, direction):
#########################################
# In this we have got the next peg position and
# below lines check for if the new move is a corner
newPos = self.getNextPosition(oldPos, direction)
if self.is_corner(newPos):
return False
#########################################
#If the new position is out of the board return False
if newPos[0] < 0 or newPos[0] > 6 or newPos[1] < 0 or newPos[1] > 6:
return False
#If there is already a marble in new position or the old position does not have a marble, return false
if self.gameState[newPos[0]][newPos[1]] == 1 or self.gameState[oldPos[0]][oldPos[1]] == 0:
return False
midPos = list(oldPos)
#If there is no marble in the intermediate position while
# reaching new position from old position, return False
if(direction == 'N'):
midPos[0] = midPos[0] - 1
if self.gameState[midPos[0]][midPos[1]] == 0:
return False
elif(direction == 'S'):
midPos[0] = midPos[0] + 1
if self.gameState[midPos[0]][midPos[1]] == 0:
return False
elif(direction == 'E'):
midPos[1] = midPos[1] + 1
if self.gameState[midPos[0]][midPos[1]] == 0:
return False
elif(direction == 'W'):
midPos[1] = midPos[1] - 1
if self.gameState[midPos[0]][midPos[1]] == 0:
return False
#If all checks are passed, then it is a valid move
return True
def getNextState(self, oldPos, direction):
# This function actually modifies the game state given the current position
# and the direction to move.
# This function cross verifies if it is valid to move in the direction mention.
###############################################
self.nodesExpanded += 1
if not self.is_validMove(oldPos, direction):
print "Error, You are not checking for valid move"
exit(0)
###############################################
# If it is valid to move from current position in the given direction,
# update the game state by actually moving the marbles.
newPos = self.getNextPosition(oldPos, direction)
self.gameState[oldPos[0]][oldPos[1]] = 0 #Remove marble from current position
self.gameState[newPos[0]][newPos[1]] = 1 #Put the removed marble to the position
# Also remove the marble which is in between current position and new position.
midPos = list(oldPos)
if(direction == 'N'):
midPos[0] = midPos[0] - 1
self.gameState[midPos[0]][midPos[1]] = 0
elif(direction == 'S'):
midPos[0] = midPos[0] + 1
self.gameState[midPos[0]][midPos[1]] = 0
elif(direction == 'E'):
midPos[1] = midPos[1] + 1
self.gameState[midPos[0]][midPos[1]] = 0
elif(direction == 'W'):
midPos[1] = midPos[1] - 1
self.gameState[midPos[0]][midPos[1]] = 0
return self.gameState