-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.py
executable file
·267 lines (213 loc) · 4.93 KB
/
array.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# rsimai@suse.com
# Hackweek 13, learning Python
import time
import os
import sys
import atexit
import termios
from select import select
# dimensions
rows = 25
cols = 50
# defender starts at cols/2
dcol = cols / 2
# characters to be used
ship = 'X'
noship = ' '
defender = 'U'
nodefender = '_'
shot = '|'
bang = 'O'
# keys to be used
lkey = 'a'
rkey = 's'
skey = ' '
# automatic fill up
fillup = 1.7
fillrows = 5
blocks = 2
# rotate 1 is right, -1 is left
rotate = 1
# the matrix
z = []
# the defender
y = []
# the shots underway
s = []
# printout matrix
m = []
char = 'q'
shipcount = 0
shotcount = 0
slist = []
# get/save the terminal settings
fd = sys.stdin.fileno()
new_term = termios.tcgetattr(fd)
old_term = termios.tcgetattr(fd)
# new terminal setting unbuffered
new_term[3] = (new_term[3] & ~termios.ICANON & ~termios.ECHO)
# switch to normal terminal on exit
def set_normal_term():
termios.tcsetattr(fd, termios.TCSAFLUSH, old_term)
# switch to unbuffered terminal
def set_curses_term():
termios.tcsetattr(fd, termios.TCSAFLUSH, new_term)
# poll the keyboard without blocking
def kbhit():
dr, dw, de = select([sys.stdin], [], [], 0)
if dr:
global char
char = sys.stdin.read(1)
else:
global char
char = ''
termios.tcflush(sys.stdin, termios.TCIOFLUSH)
return
# output in cols x rows
def printout():
check_hits()
os.system('clear')
for a in range(0, rows):
out = ''
for b in range(0, cols):
out += m[((cols)*a+b)]
print out
print_defender()
print "Shots:", shotcount, "Ships left:", shipcount, "of", shipcountorig
print "Left: \"" + lkey + "\"", " Right: \"" + rkey + "\"", " Fire: \"" + skey + "\""
# print defender base line
def print_defender():
out = ''
for b in range(0, cols):
out += y[b]
print out
# create the defender base line
def create_defender():
for a in range(0, cols-1):
y.append(nodefender)
y.insert(dcol, defender)
# create the matrix, ships and shots
def create_matrix():
global shipcount
global m
global s
for a in range(0, rows):
for b in range(0, cols):
if (a < fillrows) and (b/fillup == int(b/fillup)):
z.append(ship)
shipcount += 1
else:
z.append(noship)
s.append(' ')
m = s
global shipcountorig
shipcountorig = shipcount
# check if left or right has been reached, add a line and change direction
def check_out():
for a in range(0, rows):
pos1 = a*cols+cols-1
pos2 = a*cols
if ((z[pos1] == ship) and (rotate == 1)) or ((z[pos2] == ship) and (rotate == -1)):
global rotate
rotate = rotate * -1
move_down()
return
if rotate == 1:
move_right()
else:
move_left()
# step to the right
def move_right():
global z
z.insert(0, noship)
z.pop(rows*cols-1)
# step to the left
def move_left():
global z
z.pop(0)
z.append(noship)
# move down by one line
def move_down():
for a in range(0, cols):
global z
z.insert(0, noship)
z.pop(cols*rows)
def defender_left():
if y[0] == defender:
return
else:
y.pop(0)
y.append(nodefender)
global dcol
dcol -= 1
def defender_right():
if y[cols-1] == defender:
return
else:
y.insert(0, nodefender)
y.pop(cols)
global dcol
dcol += 1
def trigger_shot():
s[(rows-1)*cols+dcol] = shot
global shotcount
shotcount += 1
def shots_up():
for i in range(0, cols):
s.pop(0)
s.append(' ')
# check hits and merge for printout
def check_hits():
global shipcount
for i in range(0, cols*rows):
if s[i] == shot and z[i] == ship:
s[i] = ' '
z[i] = noship
m[i] = bang
shipcount -= 1
elif s[i] == shot:
m[i] = shot
else:
m[i] = z[i]
# check end
def check_end():
for i in range(0, cols):
if z[(rows-1)*cols+i] == ship:
print
print "Game over! You missed", shipcount, "ship(s)!"
print
exit(0)
if shipcount == 0:
print
print "Well done!"
print
exit(0)
# main
# prepare the terminal for keyboard input
atexit.register(set_normal_term)
set_curses_term()
# create the board
create_defender()
create_matrix()
# play loop
while True:
# printing/checking
check_out()
shots_up()
printout()
for w in range(0, 12):
kbhit()
if char:
if char == lkey:
defender_left()
printout()
if char == rkey:
defender_right()
printout()
if char == skey:
trigger_shot()
time.sleep(0.005)
check_end()