-
Notifications
You must be signed in to change notification settings - Fork 3
/
8puzzle.py
112 lines (84 loc) · 2.42 KB
/
8puzzle.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
goal = [[1, 2, 3],
[4, 5, 6],
[7, 8, 0]]
input0 = [[3, 2, 5],
[1, 7, 6],
[8, 0, 4]]
def up(input0):
for i in range(3):
for j in range(3):
if (input0[i][j] == 0):
if (i == 0):
print("INVALID MOVES")
else:
temp = input0[i-1][j]
input0[i-1][j] = 0
input0[i][j] = temp
break
for i in range(3):
for j in range(3):
print(input0[i][j], end=" ")
print()
return input0
def down(input0):
for i in range(3):
for j in range(3):
if (input0[i][j] == 0):
if (i == 2):
print("INVALID MOVES")
else:
temp = input0[i+1][j]
input0[i+1][j] = 0
input0[i][j] = temp
break
for i in range(3):
for j in range(3):
print(input0[i][j], end=" ")
print()
return input0
def left(input0):
for i in range(3):
for j in range(3):
if (input0[i][j] == 0):
if (j == 0):
print("INVALID MOVES")
else:
temp = input0[i][j-1]
input0[i][j-1] = 0
input0[i][j] = temp
break
for i in range(3):
for j in range(3):
print(input0[i][j], end=" ")
print()
return input0
def right(input0):
for i in range(3):
for j in range(3):
if (input0[i][j] == 0):
if (j == 2):
print("INVALID MOVES")
else:
temp = input0[i][j+1]
input0[i][j+1] = 0
input0[i][j] = temp
break
for i in range(3):
for j in range(3):
print(input0[i][j], end=" ")
print()
return input0
for i in range(3):
for j in range(3):
print(input0[i][j], end=" ")
print()
while (input0 != goal):
userinput = int(input("Enter production:"))
if (userinput == 1):
up(input0)
elif (userinput == 2):
down(input0)
elif (userinput == 3):
left(input0)
elif (userinput == 4):
right(input0)