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

master
panki27 7 years ago
parent 82b97c3247
commit 94bf386a24

@ -12,9 +12,10 @@ FLAG = -2
UNKNOWN = -3
CURSOR_POSITION=[0,0]
FIELDS_CLEARED = 0
width, height = 9, 9
minecount = 20
MINECOUNT = 10
#put logic for program param here
playfield = [[-3 for x in range(width)] for y in range(height)]
@ -35,9 +36,9 @@ def calculate_hint(col, row):
hint = 0
if playfield[row][col] != MINE:
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):
if y >= 0 and y < len(playfield[0])-1:
if y >= 0 and y < len(playfield[0]):
if(playfield[y][x] == MINE):
hint+=1
else:
@ -48,13 +49,13 @@ def setup_playfield(w, h):
#do this only once [AFTER THE FIRST GUESS]
#randomly distribute mines across the field
global playfield
global minecount
while minecount > 0:
minesleft = MINECOUNT
while minesleft > 0:
for rowindex, row in enumerate(playfield):
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:
minecount -= 1
minesleft -= 1
playfield[colindex][rowindex] = MINE
else:
break
@ -63,6 +64,40 @@ def setup_playfield(w, h):
# for colindex, cell in enumerate(row):
# 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):
currentline = 0
stdscr.addstr(currentline, 0, headline)
@ -81,10 +116,10 @@ def print_playfield(playfield):
# did we find a hint?
if cell >= 0:
rowstring += str(cell)
elif cell == UNKNOWN:
elif cell == UNKNOWN or cell == MINE:
rowstring+= '#'
elif cell == MINE:
rowstring += 'X'
#elif cell == MINE:
# rowstring += 'X'
if selected:
rowstring += ']|'
else:
@ -96,15 +131,35 @@ def print_playfield(playfield):
stdscr.addstr(currentline, 0, midline)
currentline +=1
#print midline
stdscr.addstr(currentline, 0, tailline)
#print currentline
#print tailline
stdscr.addstr(currentline, 0, tailline + str(FIELDS_CLEARED))
currentline +=1
#print tailline
def hit(x, y):
global playfield
def hit(x, y, recursive_call=False):
global playfield, FIELDS_CLEARED
if playfield[y][x] == UNKNOWN:
hint = calculate_hint(x, y)
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):
global CURSOR_POSITION
@ -121,9 +176,7 @@ def handle_input(k):
if CURSOR_POSITION[1] < height-1:
CURSOR_POSITION[1] +=1
elif k == ord(' '):
hit(CURSOR_POSITION[0], CURSOR_POSITION[1])
hit(CURSOR_POSITION[0], CURSOR_POSITION[1])
def main(stdscr):
stdscr.clear()
@ -136,6 +189,7 @@ def main(stdscr):
print_playfield(playfield)
key = stdscr.getch()
handle_input(key)
check_score()
stdscr.refresh()
#stdscr.getkey()
if __name__ == "__main__":

Loading…
Cancel
Save