-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
88 lines (80 loc) · 4.97 KB
/
utils.py
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
import copy
def open_cell_zero(cell_position_x, cell_position_y, board, number_of_opened_cells):
temp_board = copy.deepcopy(board)
if temp_board[cell_position_x][cell_position_y] != 0:
temp_board[cell_position_x][cell_position_y] = -2 # opened
number_of_opened_cells = number_of_opened_cells - 1
return temp_board, number_of_opened_cells
if temp_board[cell_position_x][cell_position_y] == 0:
temp_board[cell_position_x][cell_position_y] = -2 # opened
number_of_opened_cells = number_of_opened_cells - 1 # opened
# Left
if (cell_position_y - 1 >= 0) & (temp_board[cell_position_x][cell_position_y-1] != -2): # not opened yet
temp_board, number_of_opened_cells = open_cell_zero(cell_position_x, cell_position_y - 1, temp_board,
number_of_opened_cells)
# Right
if cell_position_y + 1 < len(temp_board[0]):
if temp_board[cell_position_x][cell_position_y+1] != -2: # not opened yet
temp_board, number_of_opened_cells = open_cell_zero(cell_position_x, cell_position_y + 1, temp_board,
number_of_opened_cells)
# Down
if cell_position_x + 1 < len(temp_board):
if temp_board[cell_position_x+1][cell_position_y] != -2: # not opened yet
temp_board, number_of_opened_cells = open_cell_zero(cell_position_x + 1, cell_position_y, temp_board,
number_of_opened_cells)
# Up
if (cell_position_x - 1 >= 0) & (temp_board[cell_position_x-1][cell_position_y] != -2): # not opened yet
temp_board, number_of_opened_cells = open_cell_zero(cell_position_x - 1, cell_position_y, temp_board,
number_of_opened_cells)
# Up Left
if (cell_position_y - 1 >= 0) & (cell_position_x - 1 >= 0) & (temp_board[cell_position_x - 1][cell_position_y - 1] != -2): # not opened yet
temp_board, number_of_opened_cells = open_cell_zero(cell_position_x - 1, cell_position_y - 1, temp_board,
number_of_opened_cells)
# Up Right
if (cell_position_y + 1 < len(temp_board[0])) & (cell_position_x - 1 >= 0):
if temp_board[cell_position_x - 1][cell_position_y + 1] != -2: # not opened yet
temp_board, number_of_opened_cells = open_cell_zero(cell_position_x - 1, cell_position_y + 1,
temp_board,
number_of_opened_cells)
# Down Left
if (cell_position_y - 1 >= 0) & (cell_position_x + 1 < len(temp_board)):
if temp_board[cell_position_x + 1][cell_position_y - 1] != -2: # not opened yet
temp_board, number_of_opened_cells = open_cell_zero(cell_position_x + 1, cell_position_y - 1,
temp_board,
number_of_opened_cells)
# Down Right
if (cell_position_y + 1 < len(temp_board[0])) & (cell_position_x + 1 < len(temp_board)):
if temp_board[cell_position_x + 1][cell_position_y + 1] != -2: # not opened yet
temp_board, number_of_opened_cells = open_cell_zero(cell_position_x + 1, cell_position_y + 1,
temp_board,
number_of_opened_cells)
return temp_board, number_of_opened_cells
def is_solution(clicks, board, mines_number):
"""
Check if 'clicks' is a solution
:param clicks: [(7, 7), (0, 0), (0,7), (7, 0), (4,6), (7, 5)]
:param board: BEGINNER_BOARD
:param mines_number: BEGINNER_MINES_NUMBER
:return: True if this is a solution, otherwise False
"""
temp_board = copy.deepcopy(board)
number_of_opened_cells = len(board) * len(board[0]) - mines_number
for click in clicks:
if number_of_opened_cells == 0:
return True
if temp_board[click[0]][click[1]] == -1: # mine
return False
if temp_board[click[0]][click[1]] != 0 & temp_board[click[0]][click[1]] != -2 :
temp_board[click[0]][click[1]] = -2 # opened
number_of_opened_cells = number_of_opened_cells - 1
if temp_board[click[0]][click[1]] == 0:
# open multiple cells
temp_board, number_of_opened_cells = open_cell_zero(click[0], click[1], temp_board, number_of_opened_cells)
if number_of_opened_cells == 0:
return True
return False
def shape_of_population(population):
res = []
for i in population:
res.append(len(i))
return res