-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
87 lines (68 loc) · 2.77 KB
/
Copy pathparser.py
File metadata and controls
87 lines (68 loc) · 2.77 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
from random import sample
class Cell:
def __init__(self, row: int, col: int):
self.row = row # 0; строка ячейки
self.col = col # 0; столбец ячейки
self.mine = False # True или False в зависимости от того, содержит ячейка мину или нет
self.neighbours = 0 # число от 0 до 8, количество мин в соседних ячейках
class Game:
def __init__(self, rows: int, cols: int, mines: int):
self.rows = rows
self.cols = cols
self.mines = mines
self._field = self.set_field()
print(self._field.keys())
self.set_mines()
self.board_m = [[(0, 1)[self._field[i * self.cols + j].mine] for j in range(self.cols)] for i in
range(self.rows)]
self.board_n = [[self._field[i * self.cols + j].neighbours for j in range(self.cols)] for i in range(self.rows)]
@property
def board(self):
return [[self._field[i * self.cols + j] for j in range(self.cols)] for i in range(self.rows)]
def set_field(self):
return {i * self.cols + j: Cell(i, j) for i in range(self.rows) for j in range(self.cols)}
def set_mines(self):
mines_ids = sample(range(self.cols * self.rows), self.mines)
for mine_id in mines_ids:
self._field[mine_id].mine = True
self.set_neighbours(mine_id)
def set_neighbours(self, n):
if (col_idx := n % self.cols) == 0:
d_col = [0, 1] if self.cols > 1 else [0] # first row # first column
print("first_col")
elif col_idx == self.cols - 1:
print("last column")
d_col = [0, -1] # last column
else:
d_col = [-1, 1, 0]
if (row_idx := n // self.cols) == 0:
d_row = [0, self.cols] if self.rows > 1 else [0] # first row
print("first row")
elif row_idx == self.rows - 1:
d_row = [-self.cols, 0] # last row
print("last row")
else:
d_row = [-self.cols, 0, self.cols]
for dr in d_row:
for dc in d_col:
print(n, dr, dc, n + dr + dc)
self._field[n + dr + dc].neighbours += 1
self._field[n].neighbours -= 1
# INPUT DATA:
print("______________________________________")
n, m = 5, 5
game = Game(n, m, 2)
total_mines = 0
#
# for r in range(n):
# for c in range(m):
# if not game.board[r][c].mine:
# print(r, c, ';', game.board[r][c].neighbours)
# total_mines += game.board[r][c].mine
# print(total_mines)
for row in game.board_m:
print(row)
print("______________________________________")
for row in game.board_n:
print(row)
print("______________________________________")