-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipuz_Helper.py
235 lines (227 loc) · 8.21 KB
/
ipuz_Helper.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import sys
import struct
import functools
import operator
import math
import string
Encoding_2 = "ISO-8859-1"
def iso_conv(data_s):
temp_s=data_s.replace("\u201c","\"")
temp_s=temp_s.replace("\u201d","\"")
temp_s=temp_s.replace("\u2018","\'")
temp_s=temp_s.replace("\u2019","\'")
return temp_s
# finds the checksum for the data given as argument
def cksum_region(data, cksum=0):
for c in data:
if (cksum & 0x0001):
cksum = ((cksum >> 1) | 0x8000)
else:
cksum = (cksum >> 1)
cksum = (cksum + c) & 0xffff
return cksum
# calculates cib checksum
def cib_cksum(cksum=0):
cksum = cksum_region(cib_text,cksum)
return cksum
# calculates checksum for the string section
def text_cksum(cksum=0):
global cktrial
if(f.title!=""):
temp_s=iso_conv(f.title)
cksum = cksum_region(temp_s.encode(Encoding_2)+b'\0',cksum)
if(f.author!=""):
temp_s=iso_conv(f.author)
cksum = cksum_region(temp_s.encode(Encoding_2)+b'\0',cksum)
if(f.cpyrt!=""):
temp_s=iso_conv(f.cpyrt)
cksum = cksum_region(temp_s.encode(Encoding_2)+b'\0',cksum)
for clue in cl:
if(clue):
cksum = cksum_region(clue, cksum)
if(f.notes!=""):
temp_s=iso_conv(f.notes)
cksum = cksum_region(temp_s.encode(Encoding_2)+b'\0',cksum)
return cksum
# calculates overall checksum : cib + solution + current state + string section
def overall_checksum():
cksum = cib_cksum()
cksum = cksum_region(new_soln, cksum)
cksum = cksum_region(new_curn, cksum)
cksum = text_cksum(cksum)
return cksum
# checksum of cib, solution, current state, string section XOR masked against the magic string 'ICHEATED'
def magic_low_cksum():
m1= ord('I') ^ (cib_cksum() & 0x00ff)
m2= ord('C') ^ (cksum_region(new_soln) & 0x00ff)
m3= ord('H') ^ (cksum_region(new_curn) & 0x00ff)
m4= ord('E') ^ (text_cksum() & 0x00ff)
calc_temp=struct.pack('4B',m1,m2,m3,m4)
calc_magiclow_temp=struct.unpack('I',calc_temp)
calc_magic_low=calc_magiclow_temp[0]
return calc_magic_low
def magic_high_cksum():
m5= ord('A') ^ ((cib_cksum() & 0xff00) >> 8)
m6= ord('T') ^ ((cksum_region(new_soln) & 0xff00) >> 8)
m7= ord('E') ^ ((cksum_region(new_curn) & 0xff00) >> 8)
m8= ord('D') ^ ((text_cksum() & 0xff00) >> 8)
calc_temp=struct.pack('4B',m5,m6,m7,m8)
calc_magichigh_temp=struct.unpack('I',calc_temp)
calc_magic_high=calc_magichigh_temp[0]
return calc_magic_high
# stores the current state of the puzzle in a binary file
def filewrite(t):
global f,cib_text,new_soln,new_curn,cluelist,cl,copy_sym
f=t
ofile=open(f.loc,mode='wb')
rebus_usr_entry=False
rebus=[]
new_soln=('').encode(Encoding_2)
# stores solution of the puzzle in new_soln
for i in range(0,f.height):
for j in range(0,f.width):
if(len(f.solnblock[i][j])>1):
rebus_usr_entry=True
temp=f.solnblock[i][j][0]
else:
temp=f.solnblock[i][j]
new_soln=new_soln+temp.encode(Encoding_2)
j=j+1
i=i+1
new_curn=('').encode(Encoding_2)
# stores current state of the grid in new_curn
for i in range(0,f.height):
for j in range(0,f.width):
if(len(f.cellblock[i][j])>1):
rebus_usr_entry=True
temp=f.cellblock[i][j][0]
else:
temp=f.cellblock[i][j]
new_curn=new_curn+temp.encode(Encoding_2)
j=j+1
i=i+1
temp_acc=0
temp_dwn=0
cl=[]
cluelist=('').encode(Encoding_2)
while(temp_acc!=f.acc and temp_dwn!=f.dwn):
if(f.across[temp_acc][0]<=f.down[temp_dwn][0]):
temp_s=iso_conv(f.across[temp_acc][1])
cl.append(temp_s.encode(Encoding_2))
cluelist=cluelist+(temp_s.encode(Encoding_2))+b'\0'
temp_acc=temp_acc+1
else:
temp_s=iso_conv(f.down[temp_dwn][1])
cl.append(temp_s.encode(Encoding_2))
cluelist=cluelist+(temp_s.encode(Encoding_2))+b'\0'
temp_dwn=temp_dwn+1
if(temp_acc==f.acc and temp_dwn!=f.dwn):
while(temp_dwn!=f.dwn):
temp_s=iso_conv(f.down[temp_dwn][1])
cl.append(temp_s.encode(Encoding_2))
cluelist=cluelist+(temp_s.encode(Encoding_2))+b'\0'
temp_dwn=temp_dwn+1
if(temp_acc!=f.acc and temp_dwn==f.dwn):
while(temp_acc!=f.acc):
temp_s=iso_conv(f.across[temp_acc][1])
cl.append(temp_s.encode(Encoding_2))
cluelist=cluelist+(temp_s.encode(Encoding_2))+b'\0'
temp_acc=temp_acc+1
if(rebus_usr_entry==True):
count=1
is_present=False
new_rtbl=('').encode(Encoding_2)
# stores the rebus entries in the solution in the 'rebus' list
for i in range(0,f.height):
for j in range(0,f.width):
temp=9999
if(len(f.solnblock[i][j])>1):
for k in range(0,len(rebus)):
if(rebus[k]==f.solnblock[i][j]):
temp=k+1
break
if(temp==9999):
temp=count
rebus.append(f.solnblock[i][j])
count=count+1
else:
temp=0
# 'new_grbs' stores the number corresponding to the rebus entries
if(i==0 and j==0):
new_grbs=struct.pack('B',temp)
else:
new_grbs=new_grbs+struct.pack('B',temp)
j=j+1
i=i+1
# 'new_rtbl' stores the rebus entry corresponding to each rebus number present in 'new_grbs'
for i in range (0,len(rebus)):
temp_str=""
if(i<10):
temp_str=" "+str(i)
else:
temp_str=str(i)
temp_str=temp_str+":"+rebus[i]+";"
new_rtbl=new_rtbl+temp_str.encode(Encoding_2)
no_clues=f.acc+f.dwn
byte_width=struct.pack('B',f.width)
byte_height=struct.pack('B',f.height)
byte_clues=struct.pack('H',no_clues)
scrambled=struct.pack('H',0x0000)
diagramless=struct.pack('H',0x0001)
copy_sym=struct.pack("B",0xa9)
copy_sym=copy_sym+struct.pack("B",0x20)
cib_text=byte_width+byte_height+byte_clues+diagramless+scrambled
gib=overall_checksum()
cib=cib_cksum()
# header section
ofile.write(struct.pack('H',gib))
ofile.write(("ACROSS&DOWN").encode(Encoding_2))
ofile.write(b'\0')
ofile.write(struct.pack('H',cib))
ml=magic_low_cksum()
mh=magic_high_cksum()
ofile.write(struct.pack('I',ml))
ofile.write(struct.pack('I',mh))
ofile.write(("1.3").encode(Encoding_2))
ofile.write(b'\0')
ofile.write(b'\0\0')
ofile.write(struct.pack('H',0x0000))
ofile.write(bytes(12))
ofile.write(struct.pack('B',f.width))
ofile.write(struct.pack('B',f.height))
ofile.write(struct.pack('H',no_clues))
ofile.write(diagramless)
ofile.write(scrambled)
ofile.write(new_soln)
ofile.write(new_curn)
# string section
temp_s=iso_conv(f.title)
ofile.write((temp_s).encode(Encoding_2))
ofile.write(b'\0')
temp_s=iso_conv(f.author)
ofile.write((temp_s).encode(Encoding_2))
ofile.write(b'\0')
temp_s=iso_conv(f.cpyrt)
ofile.write((temp_s).encode(Encoding_2))
ofile.write(b'\0')
ofile.write(cluelist)
if(f.notes!=""):
temp_s=iso_conv(f.notes)
ofile.write((temp_s).encode(Encoding_2))
ofile.write(b'\0')
if(rebus_usr_entry==True):
new_length=len(new_grbs)
new_cksum=cksum_region(new_grbs)
ofile.write(b'GRBS')
ofile.write(struct.pack('H',new_length))
ofile.write(struct.pack('H',new_cksum))
ofile.write(new_grbs)
ofile.write(b'\0')
new_length=len(new_rtbl)
new_cksum=cksum_region(new_rtbl)
ofile.write(b'RTBL')
ofile.write(struct.pack('H',new_length))
ofile.write(struct.pack('H',new_cksum))
ofile.write(new_rtbl)
ofile.write(b'\0')
ofile.close()