game logic, improved clearing of multiple tiles, first playable release

master
panki27 7 years ago
parent 6b34d11c09
commit 6d130398eb

@ -12,9 +12,10 @@ FLAG = -2
UNKNOWN = -3 UNKNOWN = -3
CURSOR_POSITION=[0,0] CURSOR_POSITION=[0,0]
FIELDS_CLEARED = 0
width, height = 9, 9 width, height = 9, 9
minecount = 20 MINECOUNT = 10
#put logic for program param here #put logic for program param here
playfield = [[-3 for x in range(width)] for y in range(height)] playfield = [[-3 for x in range(width)] for y in range(height)]
@ -35,9 +36,9 @@ def calculate_hint(col, row):
hint = 0 hint = 0
if playfield[row][col] != MINE: if playfield[row][col] != MINE:
for x in range(col-1, col+2): for x in range(col-1, col+2):
if x >= 0 and x < len(playfield)-1: if x >= 0 and x < len(playfield):
for y in range(row-1, row+2): for y in range(row-1, row+2):
if y >= 0 and y < len(playfield[0])-1: if y >= 0 and y < len(playfield[0]):
if(playfield[y][x] == MINE): if(playfield[y][x] == MINE):
hint+=1 hint+=1
else: else:
@ -48,13 +49,13 @@ def setup_playfield(w, h):
#do this only once [AFTER THE FIRST GUESS] #do this only once [AFTER THE FIRST GUESS]
#randomly distribute mines across the field #randomly distribute mines across the field
global playfield global playfield
global minecount minesleft = MINECOUNT
while minecount > 0: while minesleft > 0:
for rowindex, row in enumerate(playfield): for rowindex, row in enumerate(playfield):
for colindex, cell in enumerate(row): for colindex, cell in enumerate(row):
if (minecount > 0) and cell != MINE: if minesleft > 0 and playfield[colindex][rowindex] != MINE:
if random.random() < 0.1: if random.random() < 0.1:
minecount -= 1 minesleft -= 1
playfield[colindex][rowindex] = MINE playfield[colindex][rowindex] = MINE
else: else:
break break
@ -63,6 +64,40 @@ def setup_playfield(w, h):
# for colindex, cell in enumerate(row): # for colindex, cell in enumerate(row):
# playfield[colindex][rowindex] = calculate_hint(colindex, rowindex) # playfield[colindex][rowindex] = calculate_hint(colindex, rowindex)
def gameover(win):
stdscr.clear()
if not win:
stdscr.addstr(0, 0, ' ________________')
stdscr.addstr(1, 0, ' ____/ ( ( ) ) \___')
stdscr.addstr(2, 0, ' /( ( ( ) _ )) ) )\ ')
stdscr.addstr(3, 0, ' (( ( )( ) ) ( ) )')
stdscr.addstr(4, 0, ' ((/ ( _( ) ( _) ) ( () ) )')
stdscr.addstr(5, 0, ' ( ( ( (_) (( ( ) .((_ ) . )_')
stdscr.addstr(6, 0, ' ( ( ) ( ( ) ) ) . ) ( )')
stdscr.addstr(7, 0, ' ( ( ( ( ) ( _ ( _) ). ) . ) ) ( )')
stdscr.addstr(8, 0, ' ( ( ( ) ( ) ( )) ) _)( ) ) )')
stdscr.addstr(9, 0, ' ( ( ( \ ) ( (_ ( ) ( ) ) ) ) )) ( )')
stdscr.addstr(10, 0, ' ( ( ( ( (_ ( ) ( _ ) ) ( ) ) )')
stdscr.addstr(11, 0, ' ( ( ( ( ( ) (_ ) ) ) _) ) _( ( )')
stdscr.addstr(12, 0, ' (( ( )( ( _ ) _) _(_ ( (_ )')
stdscr.addstr(13, 0, ' (_((__(_(__(( ( ( | ) ) ) )_))__))_)___)')
stdscr.addstr(14, 0, ' ((__) \\||lll|l||/// \_))')
stdscr.addstr(15, 0, ' ( /(/ ( ) ) )\ )')
stdscr.addstr(16, 0, ' ( ( ( ( | | ) ) )\ )')
stdscr.addstr(17, 0, ' ( /(| / ( )) ) ) )) )')
stdscr.addstr(18, 0, ' ( ( ((((_(|)_))))) )')
stdscr.addstr(19, 0, ' ( ||\(|(|)|/|| )')
stdscr.addstr(20, 0, ' ( |(||(||)|||| )')
stdscr.addstr(21, 0, ' ( //|/l|||)|\\ \ )')
stdscr.addstr(22, 0, ' (/ / // /|//||||\\ \ \ \ _)')
stdscr.addstr(23, 0, ' You lose! Press q to quit.')
else:
stdscr.addstr(0, 0, 'You win! Press q to quit.')
while True:
key = stdscr.getch()
if key == ord('q'):
sys.exit(0)
def print_playfield(playfield): def print_playfield(playfield):
currentline = 0 currentline = 0
stdscr.addstr(currentline, 0, headline) stdscr.addstr(currentline, 0, headline)
@ -81,10 +116,10 @@ def print_playfield(playfield):
# did we find a hint? # did we find a hint?
if cell >= 0: if cell >= 0:
rowstring += str(cell) rowstring += str(cell)
elif cell == UNKNOWN: elif cell == UNKNOWN or cell == MINE:
rowstring+= '#' rowstring+= '#'
elif cell == MINE: #elif cell == MINE:
rowstring += 'X' # rowstring += 'X'
if selected: if selected:
rowstring += ']|' rowstring += ']|'
else: else:
@ -96,15 +131,35 @@ def print_playfield(playfield):
stdscr.addstr(currentline, 0, midline) stdscr.addstr(currentline, 0, midline)
currentline +=1 currentline +=1
#print midline #print midline
stdscr.addstr(currentline, 0, tailline) #print currentline
#print tailline
stdscr.addstr(currentline, 0, tailline + str(FIELDS_CLEARED))
currentline +=1 currentline +=1
#print tailline #print tailline
def hit(x, y): def hit(x, y, recursive_call=False):
global playfield global playfield, FIELDS_CLEARED
if playfield[y][x] == UNKNOWN: if playfield[y][x] == UNKNOWN:
hint = calculate_hint(x, y) hint = calculate_hint(x, y)
playfield[y][x] = hint playfield[y][x] = hint
FIELDS_CLEARED += 1
if not recursive_call and hint == NOTHING:
for i in range(x-1, x+2):
for j in range(y-1, y+2):
if i >= 0 and i < width:
if j >= 0 and j< height:
if playfield[j][i] == UNKNOWN:
hint = calculate_hint(i, j)
if hint > 0 and hint<3:
hit(i,j, True)
if hint == 0:
hit(i,j)
elif playfield[y][x] == MINE:
gameover(False)
def check_score():
if FIELDS_CLEARED == (width*height)-MINECOUNT:
gameover(True)
def handle_input(k): def handle_input(k):
global CURSOR_POSITION global CURSOR_POSITION
@ -121,9 +176,7 @@ def handle_input(k):
if CURSOR_POSITION[1] < height-1: if CURSOR_POSITION[1] < height-1:
CURSOR_POSITION[1] +=1 CURSOR_POSITION[1] +=1
elif k == ord(' '): elif k == ord(' '):
hit(CURSOR_POSITION[0], CURSOR_POSITION[1]) hit(CURSOR_POSITION[0], CURSOR_POSITION[1])
def main(stdscr): def main(stdscr):
stdscr.clear() stdscr.clear()
@ -136,6 +189,7 @@ def main(stdscr):
print_playfield(playfield) print_playfield(playfield)
key = stdscr.getch() key = stdscr.getch()
handle_input(key) handle_input(key)
check_score()
stdscr.refresh() stdscr.refresh()
#stdscr.getkey() #stdscr.getkey()
if __name__ == "__main__": if __name__ == "__main__":

Loading…
Cancel
Save