# Mine Sweeper

Sat Dec 03 2022 11:05:26 GMT+0000 (Coordinated Universal Time)

Saved by @Parker_kim #python

```from random import*

class Board:
def __init__(self, dim, num_bombs):
self.dim = dim
self.num_bombs = num_bombs
self.board = ["@" for i in range(dim * dim)]
self.around = [dim, -dim, 1, -1, dim+1, -dim+1, dim-1, -dim-1]

def set_board(self, first_choice):
x = set()

while len(x) < self.num_bombs:
idx = randint(0, pow(self.dim, 2) - 1)
while idx == first_choice:
idx = randint(0, pow(self.dim, 2) - 1)

self.bombs = sorted(list(x))
print(self.bombs)

def print_board(self):
for idx in range(pow(self.dim, 2)):
if idx % self.dim == 0 and idx != 0:
print()
print(self.board[idx], end = " ")
print()

def update_board(self, idx):
if idx in self.bombs:
for i in self.bombs:
self.board[i] = "*"
self.print_board()
print()
print("Game Over!")
return False
elif 0 <= idx < pow(self.dim, 2):
self.board[idx] = str(len(set(self.bombs).intersection(set([idx+j for j in self.around if 0 < idx+j < pow(self.dim, 2)]))))
if "@" not in [el for i, el in enumerate(self.board) if i not in self.bombs]:
print("Win!")
self.print_board()
return False
return True

def get_input(self):
while True:
try:
i = int(input("Dig the mine(0~" + str(pow(self.dim, 2) - 1) + "): "))
if 0 < i >= pow(self.dim, 2):
raise Exception
break
except:
print("Invalid_input!")
return i

def play(dim, num_bombs):

board = Board(dim, num_bombs)
board.print_board()
i = board.get_input()
board.set_board(i)

while board.update_board(i):
board.print_board()
i = board.get_input()

dim = int(input("Enter demension: "))
num_bombs = int(input("Enter number of bombs: "))
play(dim, num_bombs)
```
content_copyCOPY

Mine Sweeper game