From dd685dfdc3fd8476b965d2da185f6179f47f3bcc Mon Sep 17 00:00:00 2001 From: Date: Tue, 17 Apr 2018 20:02:35 +0200 Subject: [PATCH 01/18] Implemented Drawing of the playingfield --- README.md | 1 + minebash.py | 39 +++++++++++++++++++++++++++++++++++++++ pattern | 3 +++ 3 files changed, 43 insertions(+) create mode 100644 README.md create mode 100644 minebash.py create mode 100644 pattern diff --git a/README.md b/README.md new file mode 100644 index 0000000..dc5f354 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# minebash diff --git a/minebash.py b/minebash.py new file mode 100644 index 0000000..4a5f745 --- /dev/null +++ b/minebash.py @@ -0,0 +1,39 @@ +#!/usr/bin/python +import readline, random + +UNKNOWN = 0 + + +width, height = 9, 9 +#put logic for program param here +playfield = [[0 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): + global headline, midline, tailline + headline += '---.' + midline += '---|' + tailline += '---\'' + +def print_playfield(playfield): + print headline + for index, row in enumerate(playfield): + rowstring = '|' + for cell in row: + rowstring+='[ ]|' + print rowstring + if(index < len(row)-1): + print midline + print(tailline) + +def main(): + setup_playfield(width, height) + print_playfield(playfield) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/pattern b/pattern new file mode 100644 index 0000000..af2c4bd --- /dev/null +++ b/pattern @@ -0,0 +1,3 @@ +.---. +|[x]| +|---| From 9947811318fc9a7d013a7b9300683d20d8d53e76 Mon Sep 17 00:00:00 2001 From: panki27 Date: Tue, 17 Apr 2018 21:03:35 +0200 Subject: [PATCH 02/18] 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 From 7d433d50f30aaa97fda9ddf998b76958dc7149bc Mon Sep 17 00:00:00 2001 From: panki27 Date: Tue, 17 Apr 2018 23:07:14 +0200 Subject: [PATCH 03/18] Activation off fields and really a lot of other stuff i dont remember --- minebash.py | 102 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 81 insertions(+), 21 deletions(-) diff --git a/minebash.py b/minebash.py index 51aa7f1..08e9865 100644 --- a/minebash.py +++ b/minebash.py @@ -1,11 +1,17 @@ #!/usr/bin/python -import readline, random +import readline, random, io, sys + +import curses +stdscr = curses.initscr() +curses.noecho() +curses.cbreak() NOTHING = 0 MINE = -1 FLAG = -2 UNKNOWN = -3 +CURSOR_POSITION=[0,0] width, height = 9, 9 minecount = 20 @@ -53,33 +59,87 @@ def setup_playfield(w, h): 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) + #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): + currentline = 0 + stdscr.addstr(currentline, 0, headline) + currentline +=1 + #print headline + for rowindex, row in enumerate(playfield): rowstring = '|' - for cell in row: + for colindex, cell in enumerate(row): + # is the cell selected? + selected = False + if [colindex, rowindex] == CURSOR_POSITION: + rowstring += '[' + selected = True + else: + 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 + rowstring += str(cell) + elif cell == UNKNOWN or cell == MINE: + rowstring+='#' + if selected: + rowstring += ']|' + else: + rowstring+=' |' + #print rowstring + stdscr.addstr(currentline, 0, rowstring) + currentline +=1 + if(rowindex < len(row)-1): + stdscr.addstr(currentline, 0, midline) + currentline +=1 + #print midline + stdscr.addstr(currentline, 0, tailline) + currentline +=1 + #print tailline + +def hit(x, y): + global playfield + if playfield[y][x] == UNKNOWN: + hint = calculate_hint(x, y) + playfield[y][x] = hint + +def handle_input(k): + global CURSOR_POSITION + if k == curses.KEY_LEFT: + if CURSOR_POSITION[0] > 0: + CURSOR_POSITION[0] -=1 + elif k == curses.KEY_RIGHT: + if CURSOR_POSITION[0] < width-1: + CURSOR_POSITION[0] +=1 + elif k == curses.KEY_UP: + if CURSOR_POSITION[1] > 0: + CURSOR_POSITION[1] -=1 + elif k == curses.KEY_DOWN: + if CURSOR_POSITION[1] < height-1: + CURSOR_POSITION[1] +=1 + elif k == ord(' '): + hit(CURSOR_POSITION[0], CURSOR_POSITION[1]) + + -def main(): +def main(stdscr): + stdscr.clear() setup_strings(width) - #print_playfield(playfield) - #TODO: user input #generate mines: setup_playfield(width, height) - print_playfield(playfield) + #print_playfield(playfield) + #TODO: user input + while(True): + print_playfield(playfield) + key = stdscr.getch() + handle_input(key) + stdscr.refresh() + #stdscr.getkey() if __name__ == "__main__": - main() \ No newline at end of file + curses.wrapper(main) + #finally: + curses.nocbreak() + stdscr.keypad(False) + curses.echo() + curses.endwin() \ No newline at end of file From 6b34d11c09a6e3144945524ddf2384ca46d176ca Mon Sep 17 00:00:00 2001 From: root Date: Wed, 18 Apr 2018 09:56:09 +0200 Subject: [PATCH 04/18] fixed x-y mishap --- minebash.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) mode change 100644 => 100755 minebash.py diff --git a/minebash.py b/minebash.py old mode 100644 new mode 100755 index 08e9865..88c924c --- a/minebash.py +++ b/minebash.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python2 import readline, random, io, sys import curses @@ -33,12 +33,12 @@ def setup_strings(colcount): def calculate_hint(col, row): hint = 0 - if playfield[col][row] != MINE: + if playfield[row][col] != 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): + if(playfield[y][x] == MINE): hint+=1 else: hint = MINE @@ -81,8 +81,10 @@ def print_playfield(playfield): # did we find a hint? if cell >= 0: rowstring += str(cell) - elif cell == UNKNOWN or cell == MINE: - rowstring+='#' + elif cell == UNKNOWN: + rowstring+= '#' + elif cell == MINE: + rowstring += 'X' if selected: rowstring += ']|' else: @@ -142,4 +144,4 @@ if __name__ == "__main__": curses.nocbreak() stdscr.keypad(False) curses.echo() - curses.endwin() \ No newline at end of file + curses.endwin() From 6d130398eb245ba17fcbaf3322eb92dd29f81807 Mon Sep 17 00:00:00 2001 From: panki27 Date: Wed, 18 Apr 2018 12:42:54 +0200 Subject: [PATCH 05/18] game logic, improved clearing of multiple tiles, first playable release --- minebash.py | 86 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 70 insertions(+), 16 deletions(-) diff --git a/minebash.py b/minebash.py index 88c924c..4025192 100755 --- a/minebash.py +++ b/minebash.py @@ -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__": From 7b6005ef4ce7fe13acfa96b76b4982c7a2cce0fc Mon Sep 17 00:00:00 2001 From: panki27 Date: Wed, 18 Apr 2018 13:25:40 +0200 Subject: [PATCH 06/18] placing and removing of flags --- minebash.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/minebash.py b/minebash.py index 4025192..ac10864 100755 --- a/minebash.py +++ b/minebash.py @@ -10,6 +10,7 @@ NOTHING = 0 MINE = -1 FLAG = -2 UNKNOWN = -3 +FLAG_MINE = -4 CURSOR_POSITION=[0,0] FIELDS_CLEARED = 0 @@ -17,7 +18,7 @@ FIELDS_CLEARED = 0 width, height = 9, 9 MINECOUNT = 10 #put logic for program param here -playfield = [[-3 for x in range(width)] for y in range(height)] +playfield = [[UNKNOWN for x in range(width)] for y in range(height)] headline = '.' midline = '|' @@ -39,7 +40,7 @@ def calculate_hint(col, row): if x >= 0 and x < len(playfield): for y in range(row-1, row+2): if y >= 0 and y < len(playfield[0]): - if(playfield[y][x] == MINE): + if playfield[y][x] == MINE or playfield[y][x] == FLAG_MINE: hint+=1 else: hint = MINE @@ -118,6 +119,8 @@ def print_playfield(playfield): rowstring += str(cell) elif cell == UNKNOWN or cell == MINE: rowstring+= '#' + elif cell == FLAG_MINE or cell == FLAG: + rowstring += 'P' #elif cell == MINE: # rowstring += 'X' if selected: @@ -133,7 +136,7 @@ def print_playfield(playfield): #print midline #print currentline #print tailline - stdscr.addstr(currentline, 0, tailline + str(FIELDS_CLEARED)) + stdscr.addstr(currentline, 0, tailline) currentline +=1 #print tailline @@ -161,6 +164,18 @@ def check_score(): if FIELDS_CLEARED == (width*height)-MINECOUNT: gameover(True) +def place_flag(x, y): + global playfield + #playfield[y][x] = playfield[y][x] + if playfield[y][x] == MINE: + playfield[y][x] = FLAG_MINE + elif playfield[y][x] == UNKNOWN: + playfield[y][x] = FLAG + elif playfield[y][x] == FLAG_MINE: + playfield[y][x] = MINE + elif playfield[y][x] == FLAG: + playfield[y][x] = UNKNOWN + def handle_input(k): global CURSOR_POSITION if k == curses.KEY_LEFT: @@ -175,6 +190,8 @@ def handle_input(k): elif k == curses.KEY_DOWN: if CURSOR_POSITION[1] < height-1: CURSOR_POSITION[1] +=1 + elif k == ord('f'): + place_flag(CURSOR_POSITION[0], CURSOR_POSITION[1]) elif k == ord(' '): hit(CURSOR_POSITION[0], CURSOR_POSITION[1]) From 54c296d53acae19fc3e718386658d1ddac345012 Mon Sep 17 00:00:00 2001 From: panki27 Date: Wed, 18 Apr 2018 14:32:11 +0200 Subject: [PATCH 07/18] Playfield generation after first guess --- minebash.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/minebash.py b/minebash.py index ac10864..0dd1ba5 100755 --- a/minebash.py +++ b/minebash.py @@ -12,6 +12,9 @@ FLAG = -2 UNKNOWN = -3 FLAG_MINE = -4 +firstmove = False +FIELD_GENERATED = False + CURSOR_POSITION=[0,0] FIELDS_CLEARED = 0 @@ -46,24 +49,23 @@ def calculate_hint(col, row): hint = MINE return hint -def setup_playfield(w, h): +def setup_playfield(w, h, x, y): #do this only once [AFTER THE FIRST GUESS] #randomly distribute mines across the field - global playfield + global playfield, FIELD_GENERATED minesleft = MINECOUNT while minesleft > 0: for rowindex, row in enumerate(playfield): for colindex, cell in enumerate(row): + if colindex == x and rowindex == y: + continue if minesleft > 0 and playfield[colindex][rowindex] != MINE: if random.random() < 0.1: minesleft -= 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) + FIELD_GENERATED = True def gameover(win): stdscr.clear() @@ -177,7 +179,7 @@ def place_flag(x, y): playfield[y][x] = UNKNOWN def handle_input(k): - global CURSOR_POSITION + global CURSOR_POSITION, firstmove if k == curses.KEY_LEFT: if CURSOR_POSITION[0] > 0: CURSOR_POSITION[0] -=1 @@ -193,19 +195,25 @@ def handle_input(k): elif k == ord('f'): place_flag(CURSOR_POSITION[0], CURSOR_POSITION[1]) elif k == ord(' '): - hit(CURSOR_POSITION[0], CURSOR_POSITION[1]) + if not firstmove: + firstmove = True + else: + hit(CURSOR_POSITION[0], CURSOR_POSITION[1]) def main(stdscr): + stdscr.clear() setup_strings(width) #generate mines: - setup_playfield(width, height) #print_playfield(playfield) #TODO: user input while(True): print_playfield(playfield) key = stdscr.getch() handle_input(key) + if (firstmove) and not (FIELD_GENERATED): + setup_playfield(width, height, CURSOR_POSITION[0], CURSOR_POSITION[1]) + handle_input(key) check_score() stdscr.refresh() #stdscr.getkey() From 1c1aaf03e9c37cb747ebaa040b8acdbf47013863 Mon Sep 17 00:00:00 2001 From: panki27 Date: Wed, 18 Apr 2018 17:57:22 +0200 Subject: [PATCH 08/18] Timing, visual improvements --- minebash.py | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/minebash.py b/minebash.py index 0dd1ba5..3234ba1 100755 --- a/minebash.py +++ b/minebash.py @@ -1,5 +1,5 @@ #!/usr/bin/env python2 -import readline, random, io, sys +import readline, random, io, sys, time, os import curses stdscr = curses.initscr() @@ -12,6 +12,8 @@ FLAG = -2 UNKNOWN = -3 FLAG_MINE = -4 +STARTTIME = 0 + firstmove = False FIELD_GENERATED = False @@ -20,6 +22,7 @@ FIELDS_CLEARED = 0 width, height = 9, 9 MINECOUNT = 10 +FLAGCOUNT = 0 #put logic for program param here playfield = [[UNKNOWN for x in range(width)] for y in range(height)] @@ -50,9 +53,9 @@ def calculate_hint(col, row): return hint 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 - global playfield, FIELD_GENERATED + global playfield, FIELD_GENERATED, STARTTIME minesleft = MINECOUNT while minesleft > 0: for rowindex, row in enumerate(playfield): @@ -66,6 +69,7 @@ def setup_playfield(w, h, x, y): else: break FIELD_GENERATED = True + STARTTIME = time.time() def gameover(win): stdscr.clear() @@ -93,13 +97,21 @@ def gameover(win): stdscr.addstr(20, 0, ' ( |(||(||)|||| )') stdscr.addstr(21, 0, ' ( //|/l|||)|\\ \ )') 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: - 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: key = stdscr.getch() if key == ord('q'): sys.exit(0) + #if key == ord('r'): + # os.execl(sys.executable, sys.executable, *sys.argv) def print_playfield(playfield): currentline = 0 @@ -117,8 +129,10 @@ def print_playfield(playfield): else: rowstring += ' ' # did we find a hint? - if cell >= 0: + if cell > 0: rowstring += str(cell) + elif cell == 0: + rowstring += ' ' elif cell == UNKNOWN or cell == MINE: rowstring+= '#' elif cell == FLAG_MINE or cell == FLAG: @@ -167,16 +181,20 @@ def check_score(): gameover(True) def place_flag(x, y): - global playfield + global playfield, FLAGCOUNT #playfield[y][x] = playfield[y][x] if playfield[y][x] == MINE: playfield[y][x] = FLAG_MINE + FLAGCOUNT += 1 elif playfield[y][x] == UNKNOWN: playfield[y][x] = FLAG + FLAGCOUNT += 1 elif playfield[y][x] == FLAG_MINE: playfield[y][x] = MINE + FLAGCOUNT -= 1 elif playfield[y][x] == FLAG: playfield[y][x] = UNKNOWN + FLAGCOUNT -=1 def handle_input(k): global CURSOR_POSITION, firstmove @@ -200,6 +218,10 @@ def handle_input(k): else: hit(CURSOR_POSITION[0], CURSOR_POSITION[1]) +def print_score(): + scorestr = 'Mines: {} Flags: {}'.format(MINECOUNT, FLAGCOUNT) + stdscr.addstr(19 ,0,scorestr) + def main(stdscr): stdscr.clear() @@ -214,7 +236,9 @@ def main(stdscr): if (firstmove) and not (FIELD_GENERATED): setup_playfield(width, height, CURSOR_POSITION[0], CURSOR_POSITION[1]) handle_input(key) + STARTTIME = time.time() check_score() + print_score() stdscr.refresh() #stdscr.getkey() if __name__ == "__main__": From 2aff33fa79c34c691b9a772563295696b39405f0 Mon Sep 17 00:00:00 2001 From: panki27 Date: Wed, 18 Apr 2018 19:03:27 +0200 Subject: [PATCH 09/18] Win screen --- minebash.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/minebash.py b/minebash.py index 3234ba1..f2facd9 100755 --- a/minebash.py +++ b/minebash.py @@ -104,8 +104,16 @@ def gameover(win): 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!') + 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(10, 0, winstr) + stdscr.addstr(11,0, 'Press q to quit!') while True: key = stdscr.getch() if key == ord('q'): From 35b88812e81b929041ca987f14de8755fcf78ee9 Mon Sep 17 00:00:00 2001 From: panki27 Date: Wed, 18 Apr 2018 19:53:41 +0200 Subject: [PATCH 10/18] correct curses usage --- minebash.py | 137 +++++++++++++++++++++++++++++----------------------- 1 file changed, 76 insertions(+), 61 deletions(-) diff --git a/minebash.py b/minebash.py index f2facd9..854c889 100755 --- a/minebash.py +++ b/minebash.py @@ -1,10 +1,10 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 +#TODO: Color, show controls, command line param + import readline, random, io, sys, time, os import curses -stdscr = curses.initscr() -curses.noecho() -curses.cbreak() +from curses import wrapper NOTHING = 0 MINE = -1 @@ -14,6 +14,8 @@ FLAG_MINE = -4 STARTTIME = 0 +SCREEN = 0 + firstmove = False FIELD_GENERATED = False @@ -23,9 +25,20 @@ FIELDS_CLEARED = 0 width, height = 9, 9 MINECOUNT = 10 FLAGCOUNT = 0 -#put logic for program param here +if len(sys.argv) > 1: + if len(sys.argv) == 4: + width = int(sys.argv[1]) + height = int(sys.argv[2]) + MINECOUNT = int(sys.argv[3]) + else: + print('Specify parameters as width height minecount (for example ./minebash.py 5 5 7)') + sys.exit(0) playfield = [[UNKNOWN for x in range(width)] for y in range(height)] + +#stdscr = curses.initscr() +#curses.noecho() +#curses.cbreak() headline = '.' midline = '|' tailline = '\'' @@ -38,6 +51,14 @@ def setup_strings(colcount): midline += '---|' tailline += '---\'' +def endgame(msg=''): + # curses.nocbreak() + # stdscr.keypad(False) + # curses.echo() + #curses.endwin() + if msg != '': + print(msg) + sys.exit(0) def calculate_hint(col, row): hint = 0 @@ -72,58 +93,58 @@ def setup_playfield(w, h, x, y): STARTTIME = time.time() def gameover(win): - stdscr.clear() + SCREEN.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.') + SCREEN.addstr(0, 0, ' ________________') + SCREEN.addstr(1, 0, ' ____/ ( ( ) ) \___') + SCREEN.addstr(2, 0, ' /( ( ( ) _ )) ) )\ ') + SCREEN.addstr(3, 0, ' (( ( )( ) ) ( ) )') + SCREEN.addstr(4, 0, ' ((/ ( _( ) ( _) ) ( () ) )') + SCREEN.addstr(5, 0, ' ( ( ( (_) (( ( ) .((_ ) . )_') + SCREEN.addstr(6, 0, ' ( ( ) ( ( ) ) ) . ) ( )') + SCREEN.addstr(7, 0, ' ( ( ( ( ) ( _ ( _) ). ) . ) ) ( )') + SCREEN.addstr(8, 0, ' ( ( ( ) ( ) ( )) ) _)( ) ) )') + SCREEN.addstr(9, 0, ' ( ( ( \ ) ( (_ ( ) ( ) ) ) ) )) ( )') + SCREEN.addstr(10, 0, ' ( ( ( ( (_ ( ) ( _ ) ) ( ) ) )') + SCREEN.addstr(11, 0, ' ( ( ( ( ( ) (_ ) ) ) _) ) _( ( )') + SCREEN.addstr(12, 0, ' (( ( )( ( _ ) _) _(_ ( (_ )') + SCREEN.addstr(13, 0, ' (_((__(_(__(( ( ( | ) ) ) )_))__))_)___)') + SCREEN.addstr(14, 0, ' ((__) \\||lll|l||/// \_))') + SCREEN.addstr(15, 0, ' ( /(/ ( ) ) )\ )') + SCREEN.addstr(16, 0, ' ( ( ( ( | | ) ) )\ )') + SCREEN.addstr(17, 0, ' ( /(| / ( )) ) ) )) )') + SCREEN.addstr(18, 0, ' ( ( ((((_(|)_))))) )') + SCREEN.addstr(19, 0, ' ( ||\(|(|)|/|| )') + SCREEN.addstr(20, 0, ' ( |(||(||)|||| )') + SCREEN.addstr(21, 0, ' ( //|/l|||)|\\ \ )') + SCREEN.addstr(22, 0, ' (/ / // /|//||||\\ \ \ \ _)') + SCREEN.addstr(23, 0, ' You lose! Press q to quit.') else: 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, ' /$$ /$$ /$$ /$$ /$$$$$$$ /$$') - 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(10, 0, winstr) - stdscr.addstr(11,0, 'Press q to quit!') + SCREEN.addstr(0, 0, ' /$$ /$$ /$$ /$$ /$$$$$$$ /$$') + SCREEN.addstr(1, 0, '| $$ /$ | $$ | $$| $$ | $$__ $$ | $$') + SCREEN.addstr(2, 0, '| $$ /$$$| $$ /$$$$$$ | $$| $$ | $$ \ $$ /$$$$$$ /$$$$$$$ /$$$$$$ | $$') + SCREEN.addstr(3, 0, '| $$/$$ $$ $$ /$$__ $$| $$| $$ | $$ | $$ /$$__ $$| $$__ $$ /$$__ $$| $$') + SCREEN.addstr(4, 0, '| $$$$_ $$$$| $$$$$$$$| $$| $$ | $$ | $$| $$ \ $$| $$ \ $$| $$$$$$$$|__/') + SCREEN.addstr(5, 0, '| $$$/ \ $$$| $$_____/| $$| $$ | $$ | $$| $$ | $$| $$ | $$| $$_____/ ') + SCREEN.addstr(6, 0, '| $$/ \ $$| $$$$$$$| $$| $$ | $$$$$$$/| $$$$$$/| $$ | $$| $$$$$$$ /$$') + SCREEN.addstr(7, 0, '|__/ \__/ \_______/|__/|__/ |_______/ \______/ |__/ |__/ \_______/|__/') + SCREEN.addstr(10, 0, winstr) + SCREEN.addstr(11,0, 'Press q to quit!') while True: - key = stdscr.getch() + key = SCREEN.getch() if key == ord('q'): - sys.exit(0) + endgame() #if key == ord('r'): # os.execl(sys.executable, sys.executable, *sys.argv) -def print_playfield(playfield): +def print_playfield(playfield, screen): currentline = 0 - stdscr.addstr(currentline, 0, headline) + screen.addstr(currentline, 0, headline) currentline +=1 #print headline for rowindex, row in enumerate(playfield): @@ -152,15 +173,12 @@ def print_playfield(playfield): else: rowstring+=' |' #print rowstring - stdscr.addstr(currentline, 0, rowstring) + screen.addstr(currentline, 0, rowstring) currentline +=1 if(rowindex < len(row)-1): - stdscr.addstr(currentline, 0, midline) + screen.addstr(currentline, 0, midline) currentline +=1 - #print midline - #print currentline - #print tailline - stdscr.addstr(currentline, 0, tailline) + screen.addstr(currentline, 0, tailline) currentline +=1 #print tailline @@ -226,19 +244,20 @@ def handle_input(k): else: hit(CURSOR_POSITION[0], CURSOR_POSITION[1]) -def print_score(): +def print_score(screen): scorestr = 'Mines: {} Flags: {}'.format(MINECOUNT, FLAGCOUNT) - stdscr.addstr(19 ,0,scorestr) + screen.addstr(19 ,0,scorestr) def main(stdscr): - + global SCREEN + SCREEN = stdscr stdscr.clear() setup_strings(width) #generate mines: #print_playfield(playfield) #TODO: user input while(True): - print_playfield(playfield) + print_playfield(playfield, stdscr) key = stdscr.getch() handle_input(key) if (firstmove) and not (FIELD_GENERATED): @@ -246,13 +265,9 @@ def main(stdscr): handle_input(key) STARTTIME = time.time() check_score() - print_score() + print_score(stdscr) stdscr.refresh() #stdscr.getkey() if __name__ == "__main__": - curses.wrapper(main) - #finally: - curses.nocbreak() - stdscr.keypad(False) - curses.echo() - curses.endwin() + wrapper(main) + From a085c46c066dfaa1c033ed3bac9b090a35019596 Mon Sep 17 00:00:00 2001 From: panki27 Date: Wed, 18 Apr 2018 20:10:09 +0200 Subject: [PATCH 11/18] fixed cli arguments --- minebash.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/minebash.py b/minebash.py index 854c889..feae435 100755 --- a/minebash.py +++ b/minebash.py @@ -29,7 +29,8 @@ if len(sys.argv) > 1: if len(sys.argv) == 4: width = int(sys.argv[1]) height = int(sys.argv[2]) - MINECOUNT = int(sys.argv[3]) + if int(sys.argv[3]) < width*height: + MINECOUNT = int(sys.argv[3]) else: print('Specify parameters as width height minecount (for example ./minebash.py 5 5 7)') sys.exit(0) @@ -144,7 +145,7 @@ def gameover(win): def print_playfield(playfield, screen): currentline = 0 - screen.addstr(currentline, 0, headline) + screen.addstr(currentline, 10, headline) currentline +=1 #print headline for rowindex, row in enumerate(playfield): @@ -173,12 +174,12 @@ def print_playfield(playfield, screen): else: rowstring+=' |' #print rowstring - screen.addstr(currentline, 0, rowstring) + screen.addstr(currentline, 10, rowstring) currentline +=1 if(rowindex < len(row)-1): - screen.addstr(currentline, 0, midline) + screen.addstr(currentline, 10, midline) currentline +=1 - screen.addstr(currentline, 0, tailline) + screen.addstr(currentline, 10, tailline) currentline +=1 #print tailline @@ -246,7 +247,7 @@ def handle_input(k): def print_score(screen): scorestr = 'Mines: {} Flags: {}'.format(MINECOUNT, FLAGCOUNT) - screen.addstr(19 ,0,scorestr) + screen.addstr(19 ,20,scorestr) def main(stdscr): global SCREEN From cc5715abcca461b248e039a17c385270c705c805 Mon Sep 17 00:00:00 2001 From: panki27 Date: Wed, 18 Apr 2018 20:28:04 +0200 Subject: [PATCH 12/18] field is printed with ncurses instead of string concat --- minebash.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/minebash.py b/minebash.py index feae435..06b25d2 100755 --- a/minebash.py +++ b/minebash.py @@ -149,32 +149,34 @@ def print_playfield(playfield, screen): currentline +=1 #print headline for rowindex, row in enumerate(playfield): - rowstring = '|' + screen.addstr(currentline, 10, '|') + pos = 11 for colindex, cell in enumerate(row): # is the cell selected? selected = False if [colindex, rowindex] == CURSOR_POSITION: - rowstring += '[' + screen.addstr(currentline, pos, '[') selected = True else: - rowstring += ' ' + screen.addstr(currentline, pos, ' ') + pos += 1 # did we find a hint? if cell > 0: - rowstring += str(cell) + screen.addstr(currentline, pos, str(cell)) elif cell == 0: - rowstring += ' ' + screen.addstr(currentline, pos, ' ') elif cell == UNKNOWN or cell == MINE: - rowstring+= '#' + screen.addstr(currentline, pos, '#') #rowstring+= '#' elif cell == FLAG_MINE or cell == FLAG: - rowstring += 'P' + screen.addstr(currentline, pos, 'P') #rowstring += 'P' #elif cell == MINE: # rowstring += 'X' + pos += 1 if selected: - rowstring += ']|' + screen.addstr(currentline, pos, ']|') else: - rowstring+=' |' - #print rowstring - screen.addstr(currentline, 10, rowstring) + screen.addstr(currentline, pos, ' |') + pos += 2 currentline +=1 if(rowindex < len(row)-1): screen.addstr(currentline, 10, midline) From 61ccbe24daf6eab472adda11154efb96bbd6000b Mon Sep 17 00:00:00 2001 From: panki27 Date: Wed, 18 Apr 2018 21:51:04 +0200 Subject: [PATCH 13/18] fixed godawful random generation --- minebash.py | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/minebash.py b/minebash.py index 06b25d2..d26cdff 100755 --- a/minebash.py +++ b/minebash.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -#TODO: Color, show controls, command line param +#TODO: show controls, restartable import readline, random, io, sys, time, os @@ -80,16 +80,11 @@ def setup_playfield(w, h, x, y): global playfield, FIELD_GENERATED, STARTTIME minesleft = MINECOUNT while minesleft > 0: - for rowindex, row in enumerate(playfield): - for colindex, cell in enumerate(row): - if colindex == x and rowindex == y: - continue - if minesleft > 0 and playfield[colindex][rowindex] != MINE: - if random.random() < 0.1: - minesleft -= 1 - playfield[colindex][rowindex] = MINE - else: - break + randx = random.randint(0, width-1) + randy = random.randint(1, height-1) + if playfield[randy][randx] != MINE and randx != x and randy != y: + playfield[randy][randx] = MINE + minesleft -= 1 FIELD_GENERATED = True STARTTIME = time.time() @@ -145,7 +140,7 @@ def gameover(win): def print_playfield(playfield, screen): currentline = 0 - screen.addstr(currentline, 10, headline) + screen.addstr(currentline, 10, headline, curses.color_pair(1)) currentline +=1 #print headline for rowindex, row in enumerate(playfield): @@ -162,13 +157,16 @@ def print_playfield(playfield, screen): pos += 1 # did we find a hint? 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: screen.addstr(currentline, pos, ' ') 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: - screen.addstr(currentline, pos, 'P') #rowstring += 'P' + screen.addstr(currentline, pos, 'P', curses.color_pair(2)) #rowstring += 'P' #elif cell == MINE: # rowstring += 'X' pos += 1 @@ -249,15 +247,25 @@ def handle_input(k): def print_score(screen): 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): global SCREEN SCREEN = stdscr stdscr.clear() setup_strings(width) + setup_colors() #generate mines: - #print_playfield(playfield) #TODO: user input while(True): print_playfield(playfield, stdscr) From 3af04a0051aeb580c79d1d66def40dc6997a0408 Mon Sep 17 00:00:00 2001 From: panki27 Date: Thu, 19 Apr 2018 09:33:46 +0200 Subject: [PATCH 14/18] centered score, flags can only be placed after first move --- minebash.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/minebash.py b/minebash.py index d26cdff..359f135 100755 --- a/minebash.py +++ b/minebash.py @@ -12,6 +12,9 @@ FLAG = -2 UNKNOWN = -3 FLAG_MINE = -4 +score_x, score_y = 0, 0 +OFFSET = 11 + STARTTIME = 0 SCREEN = 0 @@ -78,6 +81,7 @@ def setup_playfield(w, h, x, y): #do this only once [AFTER THE FIRST GUESS] -> Done #randomly distribute mines across the field global playfield, FIELD_GENERATED, STARTTIME + minesleft = MINECOUNT while minesleft > 0: randx = random.randint(0, width-1) @@ -139,13 +143,14 @@ def gameover(win): # os.execl(sys.executable, sys.executable, *sys.argv) def print_playfield(playfield, screen): + global score_x, score_y currentline = 0 screen.addstr(currentline, 10, headline, curses.color_pair(1)) currentline +=1 #print headline for rowindex, row in enumerate(playfield): screen.addstr(currentline, 10, '|') - pos = 11 + pos = OFFSET for colindex, cell in enumerate(row): # is the cell selected? selected = False @@ -181,6 +186,8 @@ def print_playfield(playfield, screen): currentline +=1 screen.addstr(currentline, 10, tailline) currentline +=1 + score_y = currentline + score_x = int(pos/2) #print tailline def hit(x, y, recursive_call=False): @@ -238,7 +245,8 @@ def handle_input(k): if CURSOR_POSITION[1] < height-1: CURSOR_POSITION[1] +=1 elif k == ord('f'): - place_flag(CURSOR_POSITION[0], CURSOR_POSITION[1]) + if FIELD_GENERATED: + place_flag(CURSOR_POSITION[0], CURSOR_POSITION[1]) elif k == ord(' '): if not firstmove: firstmove = True @@ -247,7 +255,8 @@ def handle_input(k): def print_score(screen): scorestr = 'Mines: {} Flags: {}'.format(MINECOUNT, FLAGCOUNT) - screen.addstr(19 ,20, scorestr) + xpos = int((score_x) - (len(scorestr)/2)) + int(OFFSET/2) + screen.addstr(score_y, xpos, scorestr) def setup_colors(): curses.start_color() From 5abe6cd61fe72aa8f1e5d354acf61aaa7e886d65 Mon Sep 17 00:00:00 2001 From: panki27 Date: Thu, 19 Apr 2018 10:07:43 +0200 Subject: [PATCH 15/18] added difficulty settings easy, medium and hard, aswell as help text --- minebash.py | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/minebash.py b/minebash.py index 359f135..769c7fe 100755 --- a/minebash.py +++ b/minebash.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -#TODO: show controls, restartable +#TODO: show controls, restartable, command line flags like "easy, hard" import readline, random, io, sys, time, os @@ -28,12 +28,54 @@ FIELDS_CLEARED = 0 width, height = 9, 9 MINECOUNT = 10 FLAGCOUNT = 0 + +difficulty = 'medium' + +param_error = 'minebash: Invalid parameters. See \'minebash.py ?\' for help ' +helpstr = '''Usage: minebash.py [easy|medium|hard] [width height minecount] + +Difficulty presets: + easy: 5x5 4 mines + medium: 9x9 15 mines + hard: 12x12 35 mines +Specify your own: + 4 4 4 4x4 4 mines + 8 8 10 8x8 10 mines + +Controls: + Arrow Keys: Move Cursor + Spacebar: Try field + F: Place flag +''' + if len(sys.argv) > 1: - if len(sys.argv) == 4: + if len(sys.argv) == 2: #param is string like easy, hard + if sys.argv[1] == '?': + print(helpstr) + sys.exit(0) + if sys.argv[1] == 'easy': + width = 5 + height = 5 + MINECOUNT = 4 + elif sys.argv[1] == 'medium': + width = 9 + height = 9 + MINECOUNT = 15 + elif sys.argv[1] == 'hard': + width = 12 + height = 12 + MINECOUNT = 35 + + difficulty = sys.argv[1] + elif len(sys.argv) == 4: #this means the user has specified width, height and minecount width = int(sys.argv[1]) height = int(sys.argv[2]) if int(sys.argv[3]) < width*height: MINECOUNT = int(sys.argv[3]) + difficulty = 'custom' + else: + print('Minecount muss be less than width x height.') + system.exit(0) else: print('Specify parameters as width height minecount (for example ./minebash.py 5 5 7)') sys.exit(0) @@ -254,7 +296,7 @@ def handle_input(k): hit(CURSOR_POSITION[0], CURSOR_POSITION[1]) def print_score(screen): - scorestr = 'Mines: {} Flags: {}'.format(MINECOUNT, FLAGCOUNT) + scorestr = 'Mines: {} Flags: {} Difficulty: {}'.format(MINECOUNT, FLAGCOUNT, difficulty) xpos = int((score_x) - (len(scorestr)/2)) + int(OFFSET/2) screen.addstr(score_y, xpos, scorestr) From 5e48893f6166a983537bc338907345b9cf23b697 Mon Sep 17 00:00:00 2001 From: panki27 Date: Thu, 19 Apr 2018 10:38:29 +0200 Subject: [PATCH 16/18] input validation --- minebash.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/minebash.py b/minebash.py index 769c7fe..00c2a2f 100755 --- a/minebash.py +++ b/minebash.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 #TODO: show controls, restartable, command line flags like "easy, hard" -import readline, random, io, sys, time, os +import random, io, sys, time, os import curses from curses import wrapper @@ -65,7 +65,9 @@ if len(sys.argv) > 1: width = 12 height = 12 MINECOUNT = 35 - + else: + print(param_error) + sys.exit(0) difficulty = sys.argv[1] elif len(sys.argv) == 4: #this means the user has specified width, height and minecount width = int(sys.argv[1]) @@ -77,7 +79,7 @@ if len(sys.argv) > 1: print('Minecount muss be less than width x height.') system.exit(0) else: - print('Specify parameters as width height minecount (for example ./minebash.py 5 5 7)') + print(param_error) sys.exit(0) playfield = [[UNKNOWN for x in range(width)] for y in range(height)] From 57eb699531ce7be921e33786e0cd2693a80765a2 Mon Sep 17 00:00:00 2001 From: panki27 Date: Thu, 19 Apr 2018 10:51:21 +0200 Subject: [PATCH 17/18] omitted file extension, fleshed out basic README --- README.md | 28 ++++++++++++++++++++++++++++ minebash.py => minebash | 0 2 files changed, 28 insertions(+) rename minebash.py => minebash (100%) diff --git a/README.md b/README.md index dc5f354..67f470b 100644 --- a/README.md +++ b/README.md @@ -1 +1,29 @@ # minebash + +minebash is a minesweeper implementation in Python for the Linux command line. + +##Features + +1. Color +2. User specified field size & minecounts +3. Difficulty presets + +##Dependencies + +1. None, aside from curses which is standard in Python on Linux + +##Setup +``` +git clone https://github.com/panki27/minebash.git +cd minebash +chmod +x minebash +./minebash +``` + +##Controls +- Arrow Keys: Move Cursor +- Spacebar: Check field +- F: Place flag + +##Command line options +See 'minebash ?' \ No newline at end of file diff --git a/minebash.py b/minebash similarity index 100% rename from minebash.py rename to minebash From c9a9a3ae24c79b6d653941b42575c264be246306 Mon Sep 17 00:00:00 2001 From: panki27 Date: Thu, 19 Apr 2018 10:53:23 +0200 Subject: [PATCH 18/18] fixed markdown --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 67f470b..9c6ed6c 100644 --- a/README.md +++ b/README.md @@ -2,17 +2,18 @@ minebash is a minesweeper implementation in Python for the Linux command line. -##Features +## Features 1. Color 2. User specified field size & minecounts 3. Difficulty presets -##Dependencies +## Dependencies 1. None, aside from curses which is standard in Python on Linux -##Setup +## Setup + ``` git clone https://github.com/panki27/minebash.git cd minebash @@ -20,10 +21,10 @@ chmod +x minebash ./minebash ``` -##Controls +## Controls - Arrow Keys: Move Cursor - Spacebar: Check field - F: Place flag -##Command line options +## Command line options See 'minebash ?' \ No newline at end of file