-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminesweeper.py
More file actions
91 lines (68 loc) · 2.62 KB
/
minesweeper.py
File metadata and controls
91 lines (68 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
Solving a minesweeper board
Author: Oğuzhan Çölkesen
"""
def pretty_print(board):
""" Prints a Minesweeper board in a human-friendly format.
If the input board is None, then no output is produced.
Parameters:
board - a list of strings representing the board to be printed.
Returns:
None.
"""
if (board != None):
for row in board:
print(row)
def solve_board(board):
""" Solves the given Minesweeper board.
Solving a board involves replacing all occurrences of . in the original
board description with a single digit between 0-8 indicating how many of
that cell's neighbors contain mines.
Parameters:
board - a list of strings describing a Minesweeper board. Each string
is made up of .s and *s, with the former indicating empty
cells and the latter indicating mines.
Returns:
A list of strings, where each . in one of the original strings has
been replaced with a digit between 0-8, denoting the number of
adjacent cells that contain a mine.
"""
index_manipulations_list = [[1, 1], [1, 0], [1, -1], [0, 1], [0, -1],
[-1, 1], [-1, 0], [-1, -1]]
solved_board = [""] * len(board)
for row in range(len(board)):
for col in range(len(board[0])):
count = 0
if board[row][col] == ".":
for index_manip in index_manipulations_list:
if (col + index_manip[1] >= 0) and (row + index_manip[0] >= 0):
#the if statement prevents to have negative indeces that python
#interprets as starting from the opposite side of the list/string
try:
if board[row + index_manip[0]][col + index_manip[1]] == "*":
count += 1
except IndexError:
count = count
solved_board[row] += str(count)
else:
solved_board[row] += "*"
return solved_board
def main():
""" Tester function. """
print()
print("Testing solve_board")
print()
solved = solve_board(["..",
".*",
".."])
pretty_print(solved)
print()
solved = solve_board(["*.*.*",
"..*..",
"*****",
".....",
"..**."])
pretty_print(solved)
print()
if __name__ == "__main__":
main()