|
| 1 | +from random import randint, choice |
| 2 | +import os |
| 3 | +import re |
| 4 | + |
| 5 | + |
| 6 | +class MineField: |
| 7 | + def __init__(self, size, n_bombs): |
| 8 | + self.size = size |
| 9 | + self.digged = 0 |
| 10 | + self.n_bombs = n_bombs |
| 11 | + self.grid = [[0 for j in range(size)] for i in range(size)] |
| 12 | + self.grid_mask = [['·' for j in range(size)] for i in range(size)] |
| 13 | + self.plant_bombs() |
| 14 | + # self.assign_numbers() |
| 15 | + self.first_dig() |
| 16 | + |
| 17 | + def __str__(self): |
| 18 | + |
| 19 | + _str = ' '; |
| 20 | + |
| 21 | + for i in range(1, self.size+1): |
| 22 | + _str += str(i) + ' ' |
| 23 | + if i < 10: |
| 24 | + _str += ' ' |
| 25 | + _str += '\n' |
| 26 | + |
| 27 | + _str += ' ' + '―' * (self.size * 3 + 3) + '\n' |
| 28 | + |
| 29 | + for i, row in enumerate(self.grid_mask, 1): |
| 30 | + if i < 10: |
| 31 | + _str += ' ' |
| 32 | + _str += str(i) + '| ' |
| 33 | + for char in row: |
| 34 | + _str += str(char) + ' ' |
| 35 | + _str += '\n' |
| 36 | + return _str |
| 37 | + |
| 38 | + def plant_bombs(self): |
| 39 | + bombs_planted = 0 |
| 40 | + while bombs_planted < self.n_bombs: |
| 41 | + row = randint(0, self.size - 1) |
| 42 | + column = randint(0, self.size - 1) |
| 43 | + |
| 44 | + if self.grid[row][column] == '*': |
| 45 | + continue |
| 46 | + |
| 47 | + self.grid[row][column] = '*' |
| 48 | + |
| 49 | + self.fill_neighbors(row, column) |
| 50 | + |
| 51 | + bombs_planted += 1 |
| 52 | + |
| 53 | + def fill_neighbors(self, row, column): |
| 54 | + if row != 0: |
| 55 | + if column != 0: |
| 56 | + if self.grid[row-1][column-1] != '*': |
| 57 | + self.grid[row-1][column-1] += 1 |
| 58 | + |
| 59 | + if self.grid[row-1][column] != '*': |
| 60 | + self.grid[row-1][column] += 1 |
| 61 | + |
| 62 | + if column != self.size-1: |
| 63 | + if self.grid[row-1][column+1] != '*': |
| 64 | + self.grid[row-1][column+1] += 1 |
| 65 | + |
| 66 | + if column != 0: |
| 67 | + if self.grid[row][column-1] != '*': |
| 68 | + self.grid[row][column-1] += 1 |
| 69 | + |
| 70 | + if column != self.size-1: |
| 71 | + if self.grid[row][column+1] != '*': |
| 72 | + self.grid[row][column+1] += 1 |
| 73 | + |
| 74 | + if row != self.size-1: |
| 75 | + if column != 0: |
| 76 | + if self.grid[row+1][column-1] != '*': |
| 77 | + self.grid[row+1][column-1] += 1 |
| 78 | + |
| 79 | + if self.grid[row+1][column] != '*': |
| 80 | + self.grid[row+1][column] += 1 |
| 81 | + |
| 82 | + if column != self.size-1: |
| 83 | + if self.grid[row+1][column+1] != '*': |
| 84 | + self.grid[row+1][column+1] += 1 |
| 85 | + |
| 86 | + def first_dig(self): |
| 87 | + zeros = [] |
| 88 | + for row in range(self.size): |
| 89 | + for column in range(self.size): |
| 90 | + if self.grid[row][column] == 0: |
| 91 | + zeros.append((row, column)) |
| 92 | + |
| 93 | + spot_to_dig = choice(zeros) |
| 94 | + self.dig(spot_to_dig[0], spot_to_dig[1]) |
| 95 | + |
| 96 | + def dig(self, row, col): |
| 97 | + spot = self.grid[row][col] |
| 98 | + if spot == '*': |
| 99 | + return False |
| 100 | + elif spot != self.grid_mask[row][col]: |
| 101 | + self.grid_mask[row][col] = spot |
| 102 | + self.digged += 1 |
| 103 | + if (spot == 0): |
| 104 | + self.clear_zeros(row, col) |
| 105 | + return True |
| 106 | + |
| 107 | + def clear_zeros(self, row, col): |
| 108 | + if row != 0: |
| 109 | + if col != 0: |
| 110 | + self.dig(row-1, col-1) |
| 111 | + |
| 112 | + self.dig(row-1, col) |
| 113 | + |
| 114 | + if col != self.size-1: |
| 115 | + self.dig(row-1, col+1) |
| 116 | + |
| 117 | + if col != 0: |
| 118 | + self.dig(row, col-1) |
| 119 | + |
| 120 | + if col != self.size-1: |
| 121 | + self.dig(row, col+1) |
| 122 | + |
| 123 | + if row != self.size-1: |
| 124 | + if col != 0: |
| 125 | + self.dig(row+1, col-1) |
| 126 | + |
| 127 | + self.dig(row+1, col) |
| 128 | + |
| 129 | + if col != self.size-1: |
| 130 | + self.dig(row+1, col+1) |
| 131 | + |
| 132 | + def mark_spot(self, row, col): |
| 133 | + spot_mask = self.grid_mask[row][col] |
| 134 | + if spot_mask == 'X': |
| 135 | + self.grid_mask[row][col] = '·' |
| 136 | + elif spot_mask == '·': |
| 137 | + self.grid_mask[row][col] = 'X' |
| 138 | + |
| 139 | + def show_bombs(self): |
| 140 | + for row in range(self.size): |
| 141 | + for column in range(self.size): |
| 142 | + if self.grid[row][column] == '*': |
| 143 | + self.grid_mask[row][column] = '*' |
| 144 | + |
| 145 | + |
| 146 | +def print_help(): |
| 147 | + print('――――― Console Minesweeper ―――――') |
| 148 | + print('To dig in a spot, input the row\nand the column you want to dig in.') |
| 149 | + print('For example, if I wanted to dig\nin the row 2, column 4, I would\ntype: 2,4') |
| 150 | + print('―――――――――――――――――――――――――――――――――') |
| 151 | + print('If you just want to mark a spot\nwhere you know is a bomb, type the\ncoordinates followed with an "m".\nExample: 1,4m') |
| 152 | + print('\n') |
| 153 | + |
| 154 | + |
| 155 | +def play(field): |
| 156 | + pattern = re.compile(r"^[0-9]+,[0-9]+m?$") |
| 157 | + os.system('cls' if os.name == 'nt' else 'clear') |
| 158 | + print(field) |
| 159 | + move = 'h' |
| 160 | + alive = True |
| 161 | + while alive: |
| 162 | + |
| 163 | + move = input("Your move ('h' for help, 'q' to quit): ") |
| 164 | + os.system('cls' if os.name == 'nt' else 'clear') |
| 165 | + |
| 166 | + if move == 'h': |
| 167 | + print_help() |
| 168 | + elif move == 'q': |
| 169 | + print('Bye!\n') |
| 170 | + alive = 'bye' |
| 171 | + break |
| 172 | + elif re.fullmatch(pattern, move): |
| 173 | + row, column= [int(i)-1 for i in move.strip('m').split(',')] |
| 174 | + if (row > field.size) |( column > field.size): |
| 175 | + print('Invalid coordinates\n') |
| 176 | + elif move[-1] == 'm': |
| 177 | + field.mark_spot(row, column) |
| 178 | + else: |
| 179 | + alive = field.dig(row, column) |
| 180 | + if field.digged == (field.size**2 - field.n_bombs): |
| 181 | + break |
| 182 | + else: |
| 183 | + print('Invalid input\n') |
| 184 | + |
| 185 | + print(field) |
| 186 | + |
| 187 | + if alive: |
| 188 | + if alive == True: |
| 189 | + print(field) |
| 190 | + print("You won, congratulations!!!") |
| 191 | + else: |
| 192 | + os.system('cls' if os.name == 'nt' else 'clear') |
| 193 | + field.show_bombs() |
| 194 | + print(field) |
| 195 | + print("You lost :(") |
| 196 | + |
| 197 | + |
| 198 | +field = MineField(10, 10) |
| 199 | +play(field) |
0 commit comments