diff options
| author | 2017-01-01 15:15:47 +0100 | |
|---|---|---|
| committer | 2017-01-01 15:15:47 +0100 | |
| commit | a850cd967ca10938d21753d6bdbaac2fe2a913e9 (patch) | |
| tree | c0fe128406aa9db31a17efefd0ff4e2603fa38d6 | |
| parent | f1b8efb736d5a5f704a6901129fb8cbf6a68cb3f (diff) | |
| download | minesweeper-py-master.tar.gz minesweeper-py-master.tar.bz2 minesweeper-py-master.zip | |
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | README.rst | 4 | ||||
| -rw-r--r-- | cell.py | 50 | ||||
| -rw-r--r-- | field.py | 116 | ||||
| -rw-r--r-- | game.py | 126 | ||||
| -rwxr-xr-x | minesweeper | 15 | ||||
| -rw-r--r-- | minesweeper/__init__.py | 0 | ||||
| -rw-r--r-- | minesweeper/__main__.py | 19 | ||||
| -rw-r--r-- | minesweeper/cell.py | 50 | ||||
| -rw-r--r-- | minesweeper/field.py | 126 | ||||
| -rw-r--r-- | minesweeper/game.py | 124 | ||||
| -rw-r--r-- | setup.py | 20 |
12 files changed, 343 insertions, 311 deletions
diff --git a/README.md b/README.md deleted file mode 100644 index 7d77f0b..0000000 --- a/README.md +++ /dev/null | |||
| @@ -1,4 +0,0 @@ | |||
| 1 | minesweeper-py | ||
| 2 | ============== | ||
| 3 | |||
| 4 | A commandline minesweeper game I made, because I was bored. | ||
diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..f0e7a78 --- /dev/null +++ b/README.rst | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | minesweeper-py | ||
| 2 | ============== | ||
| 3 | |||
| 4 | A commandline minesweeper game I made, because I was bored. | ||
diff --git a/cell.py b/cell.py deleted file mode 100644 index 18968a2..0000000 --- a/cell.py +++ /dev/null | |||
| @@ -1,50 +0,0 @@ | |||
| 1 | #!/usr/bin/python | ||
| 2 | |||
| 3 | from random import randint | ||
| 4 | |||
| 5 | class Cell: | ||
| 6 | def __init__(self,chance): | ||
| 7 | if randint(0,99) < chance: | ||
| 8 | self.isMine = True | ||
| 9 | else: | ||
| 10 | self.isMine = False | ||
| 11 | self.covered = True | ||
| 12 | self.cover = '#' | ||
| 13 | |||
| 14 | def getIsMine(self): | ||
| 15 | return self.isMine | ||
| 16 | |||
| 17 | def getValue(self): | ||
| 18 | return self.value | ||
| 19 | |||
| 20 | def setValue(self,value): | ||
| 21 | if value == '0': | ||
| 22 | self.value = ' ' | ||
| 23 | else: | ||
| 24 | self.value = value | ||
| 25 | return | ||
| 26 | |||
| 27 | def isCovered(self): | ||
| 28 | return self.covered | ||
| 29 | |||
| 30 | def printCell(self): | ||
| 31 | if self.covered: | ||
| 32 | return self.cover | ||
| 33 | else: | ||
| 34 | return self.value | ||
| 35 | |||
| 36 | def isSafe(self): | ||
| 37 | if self.cover == 'F': | ||
| 38 | return True | ||
| 39 | else: | ||
| 40 | return False | ||
| 41 | |||
| 42 | def uncover(self): | ||
| 43 | self.covered = False | ||
| 44 | return | ||
| 45 | |||
| 46 | def toggleFlag(self): | ||
| 47 | if self.cover == '#': | ||
| 48 | self.cover = 'F' | ||
| 49 | elif self.cover == 'F': | ||
| 50 | self.cover = '#' \ No newline at end of file | ||
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 | ||
diff --git a/game.py b/game.py deleted file mode 100644 index c8927b5..0000000 --- a/game.py +++ /dev/null | |||
| @@ -1,126 +0,0 @@ | |||
| 1 | #!/usr/bin/python | ||
| 2 | |||
| 3 | try: | ||
| 4 | input = raw_input | ||
| 5 | except NameError: | ||
| 6 | pass | ||
| 7 | |||
| 8 | from field import Field | ||
| 9 | |||
| 10 | class Help: | ||
| 11 | def setState(self,l): | ||
| 12 | self.loop = l | ||
| 13 | |||
| 14 | def printHelp(self): | ||
| 15 | print("") | ||
| 16 | print("Listed commands:") | ||
| 17 | print(" try\t<x>\t<y>\tTests for mines") | ||
| 18 | print(" flag\t<x>\t<y>\tPlaces flag") | ||
| 19 | print(" ?\t<x>\t<y>\tPlaces questionmark") | ||
| 20 | print(" restart\t\tStarts new game") | ||
| 21 | print(" quit or exit\t\tQuits game") | ||
| 22 | print(" help\t\t\tPrints list of commands") | ||
| 23 | self.loop.command() | ||
| 24 | |||
| 25 | class Setup: | ||
| 26 | def setState(self,l): | ||
| 27 | self.loop = l | ||
| 28 | |||
| 29 | def setup(self): | ||
| 30 | print("") | ||
| 31 | print("Select diffeculty:") | ||
| 32 | print(" 1. Beginner\t\t(10 mines, 9x9)") | ||
| 33 | print(" 2. Intermediate\t(40 mines, 16x16)") | ||
| 34 | print(" 3. Expert\t\t(99 mines, 30x16)") | ||
| 35 | print(" 4. Custom") | ||
| 36 | print("") | ||
| 37 | |||
| 38 | n = input("Choice: ") | ||
| 39 | n = n.split() | ||
| 40 | choice = int(n[0]) | ||
| 41 | |||
| 42 | if choice == 1: | ||
| 43 | w = 9 | ||
| 44 | h = 9 | ||
| 45 | m = 10 | ||
| 46 | elif choice == 2: | ||
| 47 | w = 16 | ||
| 48 | h = 16 | ||
| 49 | m = 40 | ||
| 50 | elif choice == 3: | ||
| 51 | w = 30 | ||
| 52 | h = 16 | ||
| 53 | m = 99 | ||
| 54 | elif choice == 4: | ||
| 55 | w = int(input("Width: ")) | ||
| 56 | h = int(input("Heigt: ")) | ||
| 57 | m = int(input("Mines: ")) | ||
| 58 | |||
