diff options
Diffstat (limited to 'minesweeper/field.py')
| -rw-r--r-- | minesweeper/field.py | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/minesweeper/field.py b/minesweeper/field.py new file mode 100644 index 0000000..e6d1cd3 --- /dev/null +++ b/minesweeper/field.py | |||
| @@ -0,0 +1,126 @@ | |||
| 1 | from minesweeper.cell import Cell | ||
| 2 | |||
| 3 | |||
| 4 | class Field: | ||
| 5 | def __init__(self, width, height, nr_of_mines): | ||
| 6 | self.width = width | ||
| 7 | self.height = height | ||
| 8 | |||
| 9 | self.create_field(nr_of_mines) | ||
| 10 | self.create_hints() | ||
| 11 | |||
| 12 | def create_field(self, nr_of_mines): | ||
| 13 | mines_added = None | ||
| 14 | chance = (nr_of_mines * 100) / (self.width * self.height) | ||
| 15 | |||
| 16 | while mines_added != nr_of_mines: | ||
| 17 | self.field = [] | ||
| 18 | mines_added = 0 | ||
| 19 | |||
| 20 | for _ in range(self.height): | ||
| 21 | row = [] | ||
| 22 | for _ in range(self.width): | ||
| 23 | cell = Cell(chance) | ||
| 24 | row.append(cell) | ||
| 25 | |||
| 26 | if cell.is_mine: | ||
| 27 | mines_added += 1 | ||
| 28 | |||
| 29 | self.field.append(row) | ||
| 30 | |||
| 31 | def create_hints(self): | ||
| 32 | for y, row in enumerate(self.field): | ||
| 33 | for x, cell in enumerate(row): | ||
| 34 | if cell.is_mine: | ||
| 35 | cell.value = 'x' | ||
| 36 | else: | ||
| 37 | cell.value = len(list(self.mines_around(x, y))) | ||
| 38 | |||
| 39 | def cells_around(self, x, y): | ||
| 40 | for i in range(-1, 2): | ||
| 41 | for j in range(-1, 2): | ||
| 42 | dx = x + i | ||
| 43 | dy = y + j | ||
| 44 | |||
| 45 | if dx >= 0 and dy >= 0 \ | ||
| 46 | and dx < self.width and dy < self.height: | ||
| 47 | yield (dx, dy), self.field[dy][dx] | ||
| 48 | |||
| 49 | def mines_around(self, x, y): | ||
| 50 | for position, cell in self.cells_around(x, y): | ||
| 51 | if cell.is_mine: | ||
| 52 | yield cell | ||
| 53 | |||
| 54 | @property | ||
| 55 | def mines(self): | ||
| 56 | for row in self.field: | ||
| 57 | for cell in row: | ||
| 58 | if cell.is_mine: | ||
| 59 | yield cell | ||
| 60 | |||
| 61 | @property | ||
| 62 | def covered_cells(self): | ||
| 63 | for row in self.field: | ||
| 64 | for cell in row: | ||
| 65 | if cell.covered: | ||
| 66 | yield cell | ||
| 67 | |||
| 68 | def uncover_empty_around(self, x, y): | ||
| 69 | for position, cell in self.cells_around(x, y): | ||
| 70 | if cell.covered: | ||
| 71 | self.guess(*position) | ||
| 72 | |||
| 73 | def print_column_names(self): | ||
| 74 | print("\n\t", end="") | ||
| 75 | |||
| 76 | for char in range(0, self.width): | ||
| 77 | print(chr(char + 65) + " ", end="") | ||
| 78 | |||
| 79 | print("\n") | ||
| 80 | |||
| 81 | def print_row(self, count, row): | ||
| 82 | print(str(count) + "\t", end='') | ||
| 83 | |||
| 84 | for cell in row: | ||
| 85 | print(cell.visible_value + " ", end='') | ||
| 86 | |||
| 87 | print("\t" + str(count)) | ||
| 88 | |||
| 89 | def print(self): | ||
| 90 | self.print_column_names() | ||
| 91 | |||
| 92 | for count, row in enumerate(self.field): | ||
| 93 | self.print_row(count + 1, row) | ||
| 94 | |||
| 95 | self.print_column_names() | ||
| 96 | |||
| 97 | @property | ||
| 98 | def cleared(self): | ||
| 99 | all_safe = True | ||
| 100 | for mine in self.mines: | ||
| 101 | all_safe &= mine.safe | ||
| 102 | |||
| 103 | only_mines_covered = True | ||
| 104 | for cell in self.covered_cells: | ||
| 105 | only_mines_covered &= cell.is_mine | ||
| 106 | |||
| 107 | return all_safe or only_mines_covered | ||
| 108 | |||
| 109 | def guess(self, x, y): | ||
| 110 | cell = self.field[y][x] | ||
| 111 | if cell.value == " ": | ||
| 112 | cell.uncover() | ||
| 113 | self.uncover_empty_around(x, y) | ||
| 114 | return False | ||
| 115 | elif cell.value == 'x': | ||
| 116 | cell.uncover() | ||
| 117 | return True | ||
| 118 | else: | ||
| 119 | cell.uncover() | ||
| 120 | return False | ||
| 121 | |||
| 122 | def flag(self, x, y): | ||
| 123 | cell = self.field[y][x] | ||
| 124 | cell.toggle_flag() | ||
| 125 | |||
| 126 | # vim: set ts=8 sw=4 tw=0 et : | ||
