Timing, visual improvements

master
panki27 7 years ago
parent 54c296d53a
commit 1c1aaf03e9

@ -1,5 +1,5 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
import readline, random, io, sys import readline, random, io, sys, time, os
import curses import curses
stdscr = curses.initscr() stdscr = curses.initscr()
@ -12,6 +12,8 @@ FLAG = -2
UNKNOWN = -3 UNKNOWN = -3
FLAG_MINE = -4 FLAG_MINE = -4
STARTTIME = 0
firstmove = False firstmove = False
FIELD_GENERATED = False FIELD_GENERATED = False
@ -20,6 +22,7 @@ FIELDS_CLEARED = 0
width, height = 9, 9 width, height = 9, 9
MINECOUNT = 10 MINECOUNT = 10
FLAGCOUNT = 0
#put logic for program param here #put logic for program param here
playfield = [[UNKNOWN for x in range(width)] for y in range(height)] playfield = [[UNKNOWN for x in range(width)] for y in range(height)]
@ -50,9 +53,9 @@ def calculate_hint(col, row):
return hint return hint
def setup_playfield(w, h, x, y): def setup_playfield(w, h, x, y):
#do this only once [AFTER THE FIRST GUESS] #do this only once [AFTER THE FIRST GUESS] -> Done
#randomly distribute mines across the field #randomly distribute mines across the field
global playfield, FIELD_GENERATED global playfield, FIELD_GENERATED, STARTTIME
minesleft = MINECOUNT minesleft = MINECOUNT
while minesleft > 0: while minesleft > 0:
for rowindex, row in enumerate(playfield): for rowindex, row in enumerate(playfield):
@ -66,6 +69,7 @@ def setup_playfield(w, h, x, y):
else: else:
break break
FIELD_GENERATED = True FIELD_GENERATED = True
STARTTIME = time.time()
def gameover(win): def gameover(win):
stdscr.clear() stdscr.clear()
@ -95,11 +99,19 @@ def gameover(win):
stdscr.addstr(22, 0, ' (/ / // /|//||||\\ \ \ \ _)') stdscr.addstr(22, 0, ' (/ / // /|//||||\\ \ \ \ _)')
stdscr.addstr(23, 0, ' You lose! Press q to quit.') stdscr.addstr(23, 0, ' You lose! Press q to quit.')
else: else:
stdscr.addstr(0, 0, 'You win! Press q to quit.') now = time.time()
elapsed = now - STARTTIME
mins = elapsed / 60
secs = elapsed % 60
winstr = 'You win! It took you {}:{} to bash the field!'.format(int(mins), str(round(secs, 2)).zfill(5))
stdscr.addstr(0, 0, winstr)
stdscr.addstr(1,0, 'Press q to quit!')
while True: while True:
key = stdscr.getch() key = stdscr.getch()
if key == ord('q'): if key == ord('q'):
sys.exit(0) sys.exit(0)
#if key == ord('r'):
# os.execl(sys.executable, sys.executable, *sys.argv)
def print_playfield(playfield): def print_playfield(playfield):
currentline = 0 currentline = 0
@ -117,8 +129,10 @@ def print_playfield(playfield):
else: else:
rowstring += ' ' rowstring += ' '
# did we find a hint? # did we find a hint?
if cell >= 0: if cell > 0:
rowstring += str(cell) rowstring += str(cell)
elif cell == 0:
rowstring += ' '
elif cell == UNKNOWN or cell == MINE: elif cell == UNKNOWN or cell == MINE:
rowstring+= '#' rowstring+= '#'
elif cell == FLAG_MINE or cell == FLAG: elif cell == FLAG_MINE or cell == FLAG:
@ -167,16 +181,20 @@ def check_score():
gameover(True) gameover(True)
def place_flag(x, y): def place_flag(x, y):
global playfield global playfield, FLAGCOUNT
#playfield[y][x] = playfield[y][x] #playfield[y][x] = playfield[y][x]
if playfield[y][x] == MINE: if playfield[y][x] == MINE:
playfield[y][x] = FLAG_MINE playfield[y][x] = FLAG_MINE
FLAGCOUNT += 1
elif playfield[y][x] == UNKNOWN: elif playfield[y][x] == UNKNOWN:
playfield[y][x] = FLAG playfield[y][x] = FLAG
FLAGCOUNT += 1
elif playfield[y][x] == FLAG_MINE: elif playfield[y][x] == FLAG_MINE:
playfield[y][x] = MINE playfield[y][x] = MINE
FLAGCOUNT -= 1
elif playfield[y][x] == FLAG: elif playfield[y][x] == FLAG:
playfield[y][x] = UNKNOWN playfield[y][x] = UNKNOWN
FLAGCOUNT -=1
def handle_input(k): def handle_input(k):
global CURSOR_POSITION, firstmove global CURSOR_POSITION, firstmove
@ -200,6 +218,10 @@ def handle_input(k):
else: else:
hit(CURSOR_POSITION[0], CURSOR_POSITION[1]) hit(CURSOR_POSITION[0], CURSOR_POSITION[1])
def print_score():
scorestr = 'Mines: {} Flags: {}'.format(MINECOUNT, FLAGCOUNT)
stdscr.addstr(19 ,0,scorestr)
def main(stdscr): def main(stdscr):
stdscr.clear() stdscr.clear()
@ -214,7 +236,9 @@ def main(stdscr):
if (firstmove) and not (FIELD_GENERATED): if (firstmove) and not (FIELD_GENERATED):
setup_playfield(width, height, CURSOR_POSITION[0], CURSOR_POSITION[1]) setup_playfield(width, height, CURSOR_POSITION[0], CURSOR_POSITION[1])
handle_input(key) handle_input(key)
STARTTIME = time.time()
check_score() check_score()
print_score()
stdscr.refresh() stdscr.refresh()
#stdscr.getkey() #stdscr.getkey()
if __name__ == "__main__": if __name__ == "__main__":

Loading…
Cancel
Save