diff options
| author | 2014-04-08 12:40:56 +0200 | |
|---|---|---|
| committer | 2014-04-08 12:40:56 +0200 | |
| commit | 16af879d9c0bf43e66d891f4c3a8dce492603eb1 (patch) | |
| tree | bb2e9d9b309e9ecfee3191a549f21ab299fdfeb7 | |
| parent | 32d7355a448b815e023aa7f4c2e15a027e4033a2 (diff) | |
| download | minesweeper-py-16af879d9c0bf43e66d891f4c3a8dce492603eb1.tar.gz minesweeper-py-16af879d9c0bf43e66d891f4c3a8dce492603eb1.tar.bz2 minesweeper-py-16af879d9c0bf43e66d891f4c3a8dce492603eb1.zip | |
Added the code
| -rw-r--r-- | cell.py | 50 | ||||
| -rw-r--r-- | field.py | 110 | ||||
| -rw-r--r-- | game.py | 126 | ||||
| -rw-r--r-- | minesweeper.py | 15 |
4 files changed, 301 insertions, 0 deletions
| @@ -0,0 +1,50 @@ | |||
| 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 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 | ||
| @@ -0,0 +1,126 @@ | |||
| 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 | else: | ||
| 59 | print(str(choice) + " is not a option.") | ||
| 60 | self.setup() | ||
| 61 | |||
| 62 | minefield = Field(w,h,m) | ||
| 63 | self.loop.setMinefield(minefield) | ||
| 64 | self.loop.command() | ||
| 65 | |||
| 66 | class Loop: | ||
| 67 | def setStates(self,s,h,e): | ||
| 68 | self.setup = s | ||
| 69 | self.end = e | ||
| 70 | self.help = h | ||
| 71 | |||
| 72 | def setMinefield(self,m): | ||
| 73 | self.minefield = m | ||
| 74 | |||
| 75 | def command(self): | ||
| 76 | self.minefield.printField() | ||
| 77 | if(self.minefield.cleared()): | ||
| 78 | self.end.endGame(False) | ||
| 79 | |||
| 80 | command = input("Command >> ") | ||
| 81 | c = command.split(' ') | ||
| 82 | |||
| 83 | if c[0] == "try": | ||
| 84 | if self.minefield.guess(ord(c[1])-65,int(c[2])-1): | ||
