-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_state.py
88 lines (58 loc) · 1.89 KB
/
class_state.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
import random
import class_stack
import copy
#stav
class State:
#int n... rozměr
#int g... hloubka
#State parent... rodič
#array[][]... hrací pole
#konstr.
def __init__(self, n, g, par):
self.parent = par
self.n = n
self.g = g
self.array = [[0 for i in range(n)] for i in range(n)]
#náhodný stav
numbers = class_stack.Stack(n * n)
for i in range(n * n):
numbers.push(i)
random.shuffle(numbers.stack)
for i in range(n):
for j in range(n):
self.array[j][i] = numbers.pop()
def print(self):
row = ""
for i in range(self.n):
for j in range(self.n):
if self.array[i][j] >= 10:
row = row + " " + str(self.array[i][j])
else:
row = row + " " + str(self.array[i][j])
print(row)
row = ""
##############################################################################
#porovnání stavů (==)
def statecmp(state1, state2):
if state1 == None or state2 == None:
return False
if state1.n != state2.n:
print('Incomparable')
return False
for i in range(state1.n):
for j in range(state1.n):
if state1.array[i][j] != state2.array[i][j]:
return False
else:
return True
#seřazení
def order(state):
cc = copy.deepcopy(state)
numbers = class_stack.Stack(cc.n * cc.n)
for i in range(cc.n * cc.n):
numbers.push(cc.n * cc.n - i)
for i in range(cc.n):
for j in range(cc.n):
cc.array[i][j] = numbers.pop()
cc.array[cc.n - 1][cc.n - 1] = 0
return cc