From d3d50b47a9ee0ce0c16e0a7172dee03bf1deccc6 Mon Sep 17 00:00:00 2001 From: panki27 Date: Tue, 17 Apr 2018 21:03:35 +0200 Subject: [PATCH] Mine placement and hint number generation --- minebash.py | 64 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/minebash.py b/minebash.py index 4a5f745..51aa7f1 100644 --- a/minebash.py +++ b/minebash.py @@ -1,39 +1,85 @@ #!/usr/bin/python import readline, random -UNKNOWN = 0 +NOTHING = 0 +MINE = -1 +FLAG = -2 +UNKNOWN = -3 width, height = 9, 9 +minecount = 20 #put logic for program param here -playfield = [[0 for x in range(width)] for y in range(height)] +playfield = [[-3 for x in range(width)] for y in range(height)] headline = '.' midline = '|' tailline = '\'' -def setup_playfield(w, h): -#do this only once - for i in range(w): +def setup_strings(colcount): + #setup lines to print + for i in range(colcount): global headline, midline, tailline headline += '---.' midline += '---|' - tailline += '---\'' + tailline += '---\'' + + +def calculate_hint(col, row): + hint = 0 + if playfield[col][row] != MINE: + for x in range(col-1, col+2): + if x >= 0 and x < len(playfield)-1: + for y in range(row-1, row+2): + if y >= 0 and y < len(playfield[0])-1: + if(playfield[x][y] == MINE): + hint+=1 + else: + hint = MINE + return hint + +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: + for rowindex, row in enumerate(playfield): + for colindex, cell in enumerate(row): + if (minecount > 0) and cell != MINE: + if random.random() < 0.1: + minecount -= 1 + playfield[colindex][rowindex] = MINE + else: + break + #now that's done, we'll have to calculate the hint numbers + for rowindex, row in enumerate(playfield): + for colindex, cell in enumerate(row): + playfield[colindex][rowindex] = calculate_hint(colindex, rowindex) def print_playfield(playfield): print headline for index, row in enumerate(playfield): rowstring = '|' for cell in row: - rowstring+='[ ]|' + # did we find a hint? + if cell >= 0: + rowstring += '[' + str(cell) + ']|' + elif cell == UNKNOWN: + rowstring+='[#]|' + elif cell == MINE: + rowstring+='[X]|' print rowstring if(index < len(row)-1): print midline - print(tailline) + print tailline def main(): + setup_strings(width) + #print_playfield(playfield) + #TODO: user input + #generate mines: setup_playfield(width, height) print_playfield(playfield) - if __name__ == "__main__": main() \ No newline at end of file