#352 · Games and puzzles · Advanced · 120 XP · about 25 min
In a minesweeper game, a field of squares hides some mines. field is the hidden truth: a list of equal-length strings where '*' is a mine and '.' is safe. The player clicks squares one after another, and clicks is the list of (row, col) positions clicked, with rows and columns numbered from 0.
The player's view starts with every square closed, shown as '#'. A click works like this:
'0' to '8'. If that digit is '0', all its closed neighbours open too, and every newly opened square that shows '0' opens its own closed neighbours, and so on.'*', and every later click is ignored.Write open_squares(field, clicks) that returns the player's view after all the clicks, as a list of strings.
field = [ '....*', '.....', '**...', ] open_squares(field, [(0, 0)]) # returns ['0001#', '2211#', '#####'] open_squares(field, [(2, 4), (2, 0), (0, 0)]) # returns ['#####', '##111', '*#100']
In the first example the top left square has no mines around it, so the opening spreads until it reaches squares that touch a mine. In the second, the first click opens a small area in the bottom right corner, the click on the mine at (2, 0) ends the game, and the last click is ignored.
field has 1 to 150 rows and 1 to 150 columns; every click is inside it.First solves only, ranked by stars and then time.