diff options
Diffstat (limited to 'field.py')
| -rw-r--r-- | field.py | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/field.py b/field.py new file mode 100644 index 0000000..5372c09 --- /dev/null +++ b/field.py | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | #!/usr/bin/python | ||
| 2 | |||
| 3 | from __future__ import print_function | ||
| 4 | from cell import Cell | ||
| 5 | |||
| 6 | class Field: | ||
| 7 | def __init__(self,width,height,mines): | ||
| 8 | self.width = width | ||
| 9 | self.height = height | ||
| 10 | self.mines = mines | ||
| 11 | self.createField() | ||
| 12 | self.createHints() | ||
| 13 | return | ||
| 14 | |||
| 15 | def createField(self): | ||
| 16 | minesAdded = 0 | ||
| 17 | chance = (self.mines * 100) / (self.width * self.height) | ||
| 18 | |||
| 19 | while minesAdded != self.mines: | ||
| 20 | self.field = [] | ||
| 21 | minesAdded = 0 | ||
| 22 | for y in range(self.height): | ||
| 23 | row = [] | ||
| 24 | for x in range(self.width): | ||
| 25 | row.append(Cell(chance)) | ||
| 26 | if row[x].getIsMine(): | ||
| 27 | minesAdded += 1 | ||
| 28 | self.field.append(row) | ||
| 29 | return | ||
| 30 | |||
| 31 | def createHints(self): | ||
| 32 | for y in range(len(self.field)): | ||
| 33 | for x in range(len(self.field[y])): | ||
| 34 | cell = self.field[y][x] | ||
| 35 | if cell.getIsMine(): | ||
| 36 | cell.setValue('x') | ||
| 37 | else: | ||
| 38 | m = str(self.getMinesAround(x,y)) | ||
| 39 | cell.setValue(m) | ||
| 40 | return | ||
| 41 | |||
| 42 | def getMinesAround(self,x,y): | ||
| 43 | mines = 0 | ||
| 44 | for i in range(-1,2): | ||
| 45 | for j in range(-1,2): | ||
| 46 | xi = x+i | ||
| 47 | yj = y+j | ||
| 48 | if xi >= 0 and yj >= 0 and xi < self.width and yj < self.height: | ||
| 49 | if self.field[yj][xi].getIsMine(): | ||
| 50 | mines += 1 | ||
| 51 | return mines | ||
| 52 | |||
| 53 | def uncoverEmptyAround(self,x,y): | ||
| 54 | for i in range(-1,2): | ||
| 55 | for j in range(-1,2): | ||
| 56 | xi = x+i | ||
| 57 | yj = y+j | ||
| 58 | if xi >= 0 and yj >= 0 and xi < self.width and yj < self.height: | ||
| 59 | cell = self.field[yj][xi] | ||
| 60 | if cell.isCovered(): | ||
| 61 | self.guess(xi,yj) | ||
| 62 | return | ||
| 63 | |||
| 64 | def printField(self): | ||
| 65 | i = 1 | ||
| 66 | print("\n\t", end="") | ||
| 67 | for char in range(0,self.width): | ||
| 68 | print(chr(char+65) + " ", end="") | ||
| 69 | print("\n") | ||
| 70 | for list in self.field: | ||
| 71 | print(str(i) + "\t", end = '') | ||
| 72 | for item in list: | ||
| 73 | print(item.printCell() + " ", end='') | ||
| 74 | print() | ||
| 75 | i += 1 | ||
| 76 | return | ||
| 77 | |||
| 78 | def cleared(self): | ||
| 79 | safe = 0 | ||
| 80 | for y in range(len(self.field)): | ||
| 81 | for x in range(len(self.field[y])): | ||
| 82 | cell = self.field[y][x] | ||
| 83 | if cell.getIsMine() and cell.isSafe(): | ||
| 84 | safe += 1 | ||
| 85 | |||
| 86 | if safe == self.mines: | ||
| 87 | cleared = True | ||
| 88 | else: | ||
| 89 | cleared = False | ||
| 90 | |||
| 91 | return cleared | ||
| 92 | |||
| 93 | |||
| 94 | |||
| 95 | def guess(self,x,y): | ||
| 96 | cell = self.field[y][x] | ||
| 97 | if cell.getValue() == " ": | ||
| 98 | cell.uncover() | ||
| 99 | self.uncoverEmptyAround(x,y) | ||
| 100 | return False | ||
| 101 | elif cell.getValue() == 'x': | ||
| 102 | cell.uncover() | ||
| 103 | return True | ||
| 104 | else: | ||
| 105 | cell.uncover() | ||
| 106 | return False | ||
| 107 | |||
| 108 | def flag(self,x,y): | ||
| 109 | cell = self.field[y][x] | ||
| 110 | cell.toggleFlag() \ No newline at end of file | ||
