41
41
rotate = 1
42
42
43
43
# the matrix
44
- < << << << HEAD
45
44
z = []
46
45
47
46
# the defender
52
51
53
52
# printout matrix
54
53
m = []
55
- == == == =
56
- z = []
57
-
58
- # the defender
59
- y = []
60
-
61
- # the shots underway
62
- s = []
63
-
64
- # printout matrix
65
- m = []
66
- > >> >> >> 46e79 a7f5e0d7f5b5bcbf3bc59ec25d113d594f4
67
54
68
55
char = 'q'
69
56
shipcount = 0
78
65
# new terminal setting unbuffered
79
66
new_term [3 ] = (new_term [3 ] & ~ termios .ICANON & ~ termios .ECHO )
80
67
81
- < << << << HEAD
82
68
83
- == == == =
84
- >> >> >> > 46e79 a7f5e0d7f5b5bcbf3bc59ec25d113d594f4
85
69
# switch to normal terminal on exit
86
70
def set_normal_term ():
87
71
termios .tcsetattr (fd , termios .TCSAFLUSH , old_term )
88
72
89
- < << << << HEAD
90
73
91
- == == == =
92
- >> >> >> > 46e79 a7f5e0d7f5b5bcbf3bc59ec25d113d594f4
93
74
# switch to unbuffered terminal
94
75
def set_curses_term ():
95
76
termios .tcsetattr (fd , termios .TCSAFLUSH , new_term )
96
77
97
- < << << << HEAD
98
78
99
- == == == =
100
- >> >> >> > 46e79 a7f5e0d7f5b5bcbf3bc59ec25d113d594f4
101
79
# poll the keyboard without blocking
102
80
def kbhit ():
103
81
dr , dw , de = select ([sys .stdin ], [], [], 0 )
@@ -110,30 +88,20 @@ def kbhit():
110
88
termios .tcflush (sys .stdin , termios .TCIOFLUSH )
111
89
return
112
90
113
- < << << << HEAD
114
91
115
- == == == =
116
- >> >> >> > 46e79 a7f5e0d7f5b5bcbf3bc59ec25d113d594f4
117
92
# output in cols x rows
118
93
def printout ():
119
94
check_hits ()
120
95
os .system ('clear' )
121
- < << << << HEAD
122
96
for a in range (0 , rows ):
123
97
out = ''
124
98
for b in range (0 , cols ):
125
- == == == =
126
- for a in range (0 ,rows ):
127
- out = ''
128
- for b in range (0 ,cols ):
129
- > >> >> >> 46e79 a7f5e0d7f5b5bcbf3bc59ec25d113d594f4
130
99
out += m [((cols )* a + b )]
131
100
print out
132
101
print_defender ()
133
102
print "Shots:" , shotcount , "Ships left:" , shipcount , "of" , shipcountorig
134
103
print "Left: \" " + lkey + "\" " , " Right: \" " + rkey + "\" " , " Fire: \" " + skey + "\" "
135
104
136
- < << << << HEAD
137
105
138
106
# print defender base line
139
107
def print_defender ():
@@ -150,33 +118,13 @@ def create_defender():
150
118
y .insert (dcol , defender )
151
119
152
120
153
- == == == =
154
- # print defender base line
155
- def print_defender ():
156
- out = ''
157
- for b in range (0 ,cols ):
158
- out += y [b ]
159
- print out
160
-
161
- # create the defender base line
162
- def create_defender ():
163
- for a in range (0 ,cols - 1 ):
164
- y .append (nodefender )
165
- y .insert (dcol , defender )
166
-
167
- > >> >> >> 46e79 a7f5e0d7f5b5bcbf3bc59ec25d113d594f4
168
121
# create the matrix, ships and shots
169
122
def create_matrix ():
170
123
global shipcount
171
124
global m
172
125
global s
173
- < << << << HEAD
174
126
for a in range (0 , rows ):
175
127
for b in range (0 , cols ):
176
- == == == =
177
- for a in range (0 ,rows ):
178
- for b in range (0 ,cols ):
179
- > >> >> >> 46e79 a7f5e0d7f5b5bcbf3bc59ec25d113d594f4
180
128
if (a < fillrows ) and (b / fillup == int (b / fillup )):
181
129
z .append (ship )
182
130
shipcount += 1
@@ -187,16 +135,10 @@ def create_matrix():
187
135
global shipcountorig
188
136
shipcountorig = shipcount
189
137
190
- < << << << HEAD
191
138
192
139
# check if left or right has been reached, add a line and change direction
193
140
def check_out ():
194
141
for a in range (0 , rows ):
195
- == == == =
196
- # check if left or right has been reached, add a line and change direction
197
- def check_out ():
198
- for a in range (0 ,rows ):
199
- > >> >> >> 46e79 a7f5e0d7f5b5bcbf3bc59ec25d113d594f4
200
142
pos1 = a * cols + cols - 1
201
143
pos2 = a * cols
202
144
if ((z [pos1 ] == ship ) and (rotate == 1 )) or ((z [pos2 ] == ship ) and (rotate == - 1 )):
@@ -209,7 +151,6 @@ def check_out():
209
151
else :
210
152
move_left ()
211
153
212
- < << << << HEAD
213
154
214
155
# step to the right
215
156
def move_right ():
@@ -218,21 +159,12 @@ def move_right():
218
159
z .pop (rows * cols - 1 )
219
160
220
161
221
- == == == =
222
- # step to the right
223
- def move_right ():
224
- global z
225
- z .insert (0 ,noship )
226
- z .pop (rows * cols - 1 )
227
-
228
- > >> >> >> 46e79 a7f5e0d7f5b5bcbf3bc59ec25d113d594f4
229
162
# step to the left
230
163
def move_left ():
231
164
global z
232
165
z .pop (0 )
233
166
z .append (noship )
234
167
235
- < << << << HEAD
236
168
237
169
# move down by one line
238
170
def move_down ():
@@ -242,15 +174,6 @@ def move_down():
242
174
z .pop (cols * rows )
243
175
244
176
245
- == == == =
246
- # move down by one line
247
- def move_down ():
248
- for a in range (0 ,cols ):
249
- global z
250
- z .insert (0 ,noship )
251
- z .pop (cols * rows )
252
-
253
- > >> >> >> 46e79 a7f5e0d7f5b5bcbf3bc59ec25d113d594f4
254
177
def defender_left ():
255
178
if y [0 ] == defender :
256
179
return
@@ -260,10 +183,7 @@ def defender_left():
260
183
global dcol
261
184
dcol -= 1
262
185
263
- < << << << HEAD
264
186
265
- == == == =
266
- >> >> >> > 46e79 a7f5e0d7f5b5bcbf3bc59ec25d113d594f4
267
187
def defender_right ():
268
188
if y [cols - 1 ] == defender :
269
189
return
@@ -273,15 +193,11 @@ def defender_right():
273
193
global dcol
274
194
dcol += 1
275
195
276
- < << << << HEAD
277
196
278
- == == == =
279
- >> >> >> > 46e79 a7f5e0d7f5b5bcbf3bc59ec25d113d594f4
280
197
def trigger_shot ():
281
198
s [(rows - 1 )* cols + dcol ] = shot
282
199
global shotcount
283
200
shotcount += 1
284
- < << << << HEAD
285
201
286
202
287
203
def shots_up ():
@@ -294,18 +210,6 @@ def shots_up():
294
210
def check_hits ():
295
211
global shipcount
296
212
for i in range (0 , cols * rows ):
297
- == == == =
298
-
299
- def shots_up ():
300
- for i in range (0 ,cols ):
301
- s .pop (0 )
302
- s .append (' ' )
303
-
304
- # check hits and merge for printout
305
- def check_hits ():
306
- global shipcount
307
- for i in range (0 ,cols * rows ):
308
- > >> >> >> 46e79 a7f5e0d7f5b5bcbf3bc59ec25d113d594f4
309
213
if s [i ] == shot and z [i ] == ship :
310
214
s [i ] = ' '
311
215
z [i ] = noship
@@ -319,11 +223,7 @@ def check_hits():
319
223
320
224
# check end
321
225
def check_end ():
322
- < << << << HEAD
323
226
for i in range (0 , cols ):
324
- == == == =
325
- for i in range (0 ,cols ):
326
- > >> >> >> 46e79 a7f5e0d7f5b5bcbf3bc59ec25d113d594f4
327
227
if z [(rows - 1 )* cols + i ] == ship :
328
228
print
329
229
print "Game over! You missed" , shipcount , "ship(s)!"
@@ -351,11 +251,7 @@ def check_end():
351
251
check_out ()
352
252
shots_up ()
353
253
printout ()
354
- < << << << HEAD
355
254
for w in range (0 , 12 ):
356
- == == == =
357
- for w in range (0 ,15 ):
358
- > >> >> >> 46e79 a7f5e0d7f5b5bcbf3bc59ec25d113d594f4
359
255
kbhit ()
360
256
if char :
361
257
if char == lkey :
@@ -366,11 +262,6 @@ def check_end():
366
262
printout ()
367
263
if char == skey :
368
264
trigger_shot ()
369
- < << << << HEAD
370
265
371
266
time .sleep (0.005 )
372
- == == == =
373
-
374
- time .sleep (0.02 )
375
- >> >> >> > 46e79 a7f5e0d7f5b5bcbf3bc59ec25d113d594f4
376
267
check_end ()
0 commit comments