fixed godawful random generation

master
panki27 7 years ago
parent 5dab62b061
commit 053974241e

@ -1,5 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
#TODO: Color, show controls, command line param #TODO: show controls, restartable
import readline, random, io, sys, time, os import readline, random, io, sys, time, os
@ -80,16 +80,11 @@ def setup_playfield(w, h, x, y):
global playfield, FIELD_GENERATED, STARTTIME global playfield, FIELD_GENERATED, STARTTIME
minesleft = MINECOUNT minesleft = MINECOUNT
while minesleft > 0: while minesleft > 0:
for rowindex, row in enumerate(playfield): randx = random.randint(0, width-1)
for colindex, cell in enumerate(row): randy = random.randint(1, height-1)
if colindex == x and rowindex == y: if playfield[randy][randx] != MINE and randx != x and randy != y:
continue playfield[randy][randx] = MINE
if minesleft > 0 and playfield[colindex][rowindex] != MINE:
if random.random() < 0.1:
minesleft -= 1 minesleft -= 1
playfield[colindex][rowindex] = MINE
else:
break
FIELD_GENERATED = True FIELD_GENERATED = True
STARTTIME = time.time() STARTTIME = time.time()
@ -145,7 +140,7 @@ def gameover(win):
def print_playfield(playfield, screen): def print_playfield(playfield, screen):
currentline = 0 currentline = 0
screen.addstr(currentline, 10, headline) screen.addstr(currentline, 10, headline, curses.color_pair(1))
currentline +=1 currentline +=1
#print headline #print headline
for rowindex, row in enumerate(playfield): for rowindex, row in enumerate(playfield):
@ -162,13 +157,16 @@ def print_playfield(playfield, screen):
pos += 1 pos += 1
# did we find a hint? # did we find a hint?
if cell > 0: if cell > 0:
screen.addstr(currentline, pos, str(cell)) if cell == 1: color = curses.color_pair(3) #cyan
elif cell == 2: color = curses.color_pair(4) #blue
else: color = curses.color_pair(5) #yellow
screen.addstr(currentline, pos, str(cell), color)
elif cell == 0: elif cell == 0:
screen.addstr(currentline, pos, ' ') screen.addstr(currentline, pos, ' ')
elif cell == UNKNOWN or cell == MINE: elif cell == UNKNOWN or cell == MINE:
screen.addstr(currentline, pos, '#') #rowstring+= '#' screen.addstr(currentline, pos, '#', curses.color_pair(7)) #rowstring+= '#'
elif cell == FLAG_MINE or cell == FLAG: elif cell == FLAG_MINE or cell == FLAG:
screen.addstr(currentline, pos, 'P') #rowstring += 'P' screen.addstr(currentline, pos, 'P', curses.color_pair(2)) #rowstring += 'P'
#elif cell == MINE: #elif cell == MINE:
# rowstring += 'X' # rowstring += 'X'
pos += 1 pos += 1
@ -251,13 +249,23 @@ def print_score(screen):
scorestr = 'Mines: {} Flags: {}'.format(MINECOUNT, FLAGCOUNT) scorestr = 'Mines: {} Flags: {}'.format(MINECOUNT, FLAGCOUNT)
screen.addstr(19 ,20, scorestr) screen.addstr(19 ,20, scorestr)
def setup_colors():
curses.start_color()
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_BLACK)
curses.init_pair(5, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(7, curses.COLOR_WHITE, curses.COLOR_WHITE)
def main(stdscr): def main(stdscr):
global SCREEN global SCREEN
SCREEN = stdscr SCREEN = stdscr
stdscr.clear() stdscr.clear()
setup_strings(width) setup_strings(width)
setup_colors()
#generate mines: #generate mines:
#print_playfield(playfield)
#TODO: user input #TODO: user input
while(True): while(True):
print_playfield(playfield, stdscr) print_playfield(playfield, stdscr)

Loading…
Cancel
Save