forked from lambdamai/Tower_of_Hanoi_in_Term_AI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AI.py
65 lines (50 loc) · 1.8 KB
/
AI.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
prev_move = []
def check_can_move(pyr1, pyr2, i, j ):
global prev_move
if pyr1 == pyr2:
raise(Exception("Trying to move to same pyramid dumbass"))
try:
pyr1_top = pyr1.get_top()
except:
pyr1_top = 0
try:
pyr2_top = pyr2.get_top()
except:
pyr2_top = 0
#print("Attempting move", [i, j])
if pyr1_top > pyr2_top and pyr2_top != 0:
return False #can't move bigger to smaller
if pyr1_top != 0 and pyr2_top != 0:
if (pyr1_top % 2 == 0 and pyr2_top % 2 == 0) or (pyr1_top % 2 != 0 and pyr2_top % 2 != 0):
return False #can't move even on even, odd on odd
prv_reversed = list(prev_move)
prv_reversed.reverse()
if [x-1 for x in prv_reversed] == [i, j]:
return False #can't undo previous move
return True
def AI(game):
global prev_move
#iterative solution https://en.wikipedia.org/wiki/Tower_of_Hanoi#Simpler_statement_of_iterative_solution
if game.count == 0:
even_disks = game.pyramids[0].get_len()%2 == 0
if even_disks:
move = [1, 2]
else:
move = [1, 3]
prev_move = list(move)
return move
possible_moves = []
#if can move 1 2 add it
for i in range(len(game.pyramids)):
pyramid = game.pyramids[i]
if pyramid.get_len() == 0:
continue #pyramid is empty, we can't move
for j in range(len(game.pyramids)):
if i == j:
continue
if check_can_move(game.pyramids[i], game.pyramids[j], i, j):
possible_moves.append([i+1, j+1])
# for move in possible_moves:
# print(str(move))
prev_move = list(possible_moves[0])
return possible_moves[0]