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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
#!/usr/bin/python
from __future__ import print_function
from cell import Cell
class Field:
def __init__(self,width,height,mines):
self.width = width
self.height = height
self.mines = mines
self.createField()
self.createHints()
return
def createField(self):
minesAdded = 0
chance = (self.mines * 100) / (self.width * self.height)
while minesAdded != self.mines:
self.field = []
minesAdded = 0
for y in range(self.height):
row = []
for x in range(self.width):
row.append(Cell(chance))
if row[x].getIsMine():
minesAdded += 1
self.field.append(row)
return
def createHints(self):
for y in range(len(self.field)):
for x in range(len(self.field[y])):
cell = self.field[y][x]
if cell.getIsMine():
cell.setValue('x')
else:
m = str(self.getMinesAround(x,y))
cell.setValue(m)
return
def getMinesAround(self,x,y):
mines = 0
for i in range(-1,2):
for j in range(-1,2):
xi = x+i
yj = y+j
if xi >= 0 and yj >= 0 and xi < self.width and yj < self.height:
if self.field[yj][xi].getIsMine():
mines += 1
return mines
def uncoverEmptyAround(self,x,y):
for i in range(-1,2):
for j in range(-1,2):
xi = x+i
yj = y+j
if xi >= 0 and yj >= 0 and xi < self.width and yj < self.height:
cell = self.field[yj][xi]
if cell.isCovered():
self.guess(xi,yj)
return
def printField(self):
i = 1
print("\n\t", end="")
for char in range(0,self.width):
print(chr(char+65) + " ", end="")
print("\n")
for list in self.field:
print(str(i) + "\t", end = '')
for item in list:
print(item.printCell() + " ", end='')
print("\t" + str(i))
i += 1
print("\n\t", end="")
for char in range(0,self.width):
print(chr(char+65) + " ", end="")
print("\n")
def cleared(self):
safe = 0
for y in range(len(self.field)):
for x in range(len(self.field[y])):
cell = self.field[y][x]
if cell.getIsMine() and cell.isSafe():
safe += 1
if safe == self.mines:
cleared = True
else:
cleared = False
return cleared
def guess(self,x,y):
cell = self.field[y][x]
if cell.getValue() == " ":
cell.uncover()
self.uncoverEmptyAround(x,y)
return False
elif cell.getValue() == 'x':
cell.uncover()
return True
else:
cell.uncover()
return False
def flag(self,x,y):
cell = self.field[y][x]
cell.toggleFlag()
|