diff options
Diffstat (limited to 'field.py')
| -rw-r--r-- | field.py | 116 |
1 files changed, 0 insertions, 116 deletions
diff --git a/field.py b/field.py deleted file mode 100644 index f7a2544..0000000 --- a/field.py +++ /dev/null | |||
| @@ -1,116 +0,0 @@ | |||
| 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 | |||
| 67 | print("\n\t", end="") | ||
| 68 | for char in range(0,self.width): | ||
| 69 | print(chr(char+65) + " ", end="") | ||
| 70 | print("\n") | ||
| 71 | |||
| 72 | for list in self.field: | ||
| 73 | print(str(i) + "\t", end = '') | ||
| 74 | for item in list: | ||
| 75 | print(item.printCell() + " ", end='') | ||
| 76 | print("\t" + str(i)) | ||
| 77 | i += 1 | ||
| 78 | |||
| 79 | print("\n\t", end="") | ||
| 80 | for char in range(0,self.width): | ||
| 81 | print(chr(char+65) + " ", end="") | ||
| 82 | print("\n") | ||
| 83 | |||
| 84 | def cleared(self): | ||
| 85 | safe = 0 | ||
| 86 | for y in range(len(self.field)): | ||
| 87 | for x in range(len(self.field[y])): | ||
| 88 | cell = self.field[y][x] | ||
| 89 | if cell.getIsMine() and cell.isSafe(): | ||
| 90 | safe += 1 | ||
| 91 | |||
| 92 | if safe == self.mines: | ||
| 93 | cleared = True | ||
| 94 | else: | ||
| 95 | cleared = False | ||
| 96 | |||
| 97 | return cleared | ||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | def guess(self,x,y): | ||
| 102 | cell = self.field[y][x] | ||
| 103 | if cell.getValue() == " ": | ||
| 104 | cell.uncover() | ||
| 105 | self.uncoverEmptyAround(x,y) | ||
| 106 | return False | ||
| 107 | elif cell.getValue() == 'x': | ||
| 108 | cell.uncover() | ||
| 109 | return True | ||
| 110 | else: | ||
| 111 | cell.uncover() | ||
| 112 | return False | ||
| 113 | |||
| 114 | def flag(self,x,y): | ||
| 115 | cell = self.field[y][x] | ||
| 116 | cell.toggleFlag() \ No newline at end of file | ||
