@@ -146,17 +146,18 @@ def check_for_win():
146146 """Check for a complete, winning game. That's one with all squares uncovered
147147 and all bombs correctly flagged, with no non-bomb squares flaged.
148148 """
149+ # first make sure everything has been explored and decided
149150 for x in range (20 ):
150151 for y in range (15 ):
151- visible = tilegrid [x , y ]
152- under = get_data (x , y )
153- if visible == BLANK :
154- print ('Found a unexplored square at (%d, %d)' % (x , y ))
155- return False #still covewred squares, not done
156- elif visible == BOMBFLAGGED and under != BOMB :
157- print ('Found misflagged bomb at (%d, %d)' % (x , y ))
152+ if tilegrid [x , y ] == BLANK or tilegrid [x , y ] == BOMBQUESTION :
153+ return None #still ignored or question squares
154+ # then check for mistagged bombs
155+ for x in range (20 ):
156+ for y in range (15 ):
157+ if tilegrid [x , y ] == BOMBFLAGGED and get_data (x , y ) != BOMB :
158158 return False #misflagged bombs, not done
159- return True
159+ return True #nothing unexplored, and no misflagged bombs
160+
160161# comment or remove if not using screenshots ######################
161162#pylint:disable=global-statement
162163# from adafruit_bitmapsaver import save_pixels #
@@ -210,23 +211,25 @@ def play_a_game():
210211 touch_y = max (min ([touch_at [1 ] // 16 , 14 ]), 0 )
211212 print ('Touched (%d, %d)' % (touch_x , touch_y ))
212213 if tilegrid [touch_x , touch_y ] == BLANK :
214+ tilegrid [touch_x , touch_y ] = BOMBQUESTION
215+ elif tilegrid [touch_x , touch_y ] == BOMBQUESTION :
213216 tilegrid [touch_x , touch_y ] = BOMBFLAGGED
214217 elif tilegrid [touch_x , touch_y ] == BOMBFLAGGED :
215218 under_the_tile = get_data (touch_x , touch_y )
216219 if under_the_tile == 14 :
217- reveal ()
218- tilegrid [touch_x , touch_y ] = BOMBDEATH
220+ set_data (touch_x , touch_y , BOMBDEATH )
219221 return False #lost
220222 elif under_the_tile > OPEN0 and under_the_tile <= OPEN8 :
221223 tilegrid [touch_x , touch_y ] = under_the_tile
222224 elif under_the_tile == OPEN0 :
223225 tilegrid [touch_x , touch_y ] = BLANK
224226 number_uncovered += expand_uncovered (touch_x , touch_y )
225- else :
226- print ('Unexpected value on board' )
227- return None #something bad happened
228- if check_for_win ():
229- return True #won
227+ else : #something bad happened
228+ raise ValueError ('Unexpected value on board' )
229+ status = check_for_win ()
230+ if status is None :
231+ continue
232+ return status
230233
231234def reset_board ():
232235 for x in range (20 ):
0 commit comments