From 16af879d9c0bf43e66d891f4c3a8dce492603eb1 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Tue, 8 Apr 2014 12:40:56 +0200 Subject: Added the code --- cell.py | 50 +++++++++++++++++++++++ field.py | 110 +++++++++++++++++++++++++++++++++++++++++++++++++ game.py | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ minesweeper.py | 15 +++++++ 4 files changed, 301 insertions(+) create mode 100644 cell.py create mode 100644 field.py create mode 100644 game.py create mode 100644 minesweeper.py diff --git a/cell.py b/cell.py new file mode 100644 index 0000000..18968a2 --- /dev/null +++ b/cell.py @@ -0,0 +1,50 @@ +#!/usr/bin/python + +from random import randint + +class Cell: + def __init__(self,chance): + if randint(0,99) < chance: + self.isMine = True + else: + self.isMine = False + self.covered = True + self.cover = '#' + + def getIsMine(self): + return self.isMine + + def getValue(self): + return self.value + + def setValue(self,value): + if value == '0': + self.value = ' ' + else: + self.value = value + return + + def isCovered(self): + return self.covered + + def printCell(self): + if self.covered: + return self.cover + else: + return self.value + + def isSafe(self): + if self.cover == 'F': + return True + else: + return False + + def uncover(self): + self.covered = False + return + + def toggleFlag(self): + if self.cover == '#': + self.cover = 'F' + elif self.cover == 'F': + 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 @@ +#!/usr/bin/python + +from __future__ import print_function +from cell import Cell + +class Field: + def __init__(self,width,height,mines): + self.width = width + self.height = height + self.mines = mines + self.createField() + self.createHints() + return + + def createField(self): + minesAdded = 0 + chance = (self.mines * 100) / (self.width * self.height) + + while minesAdded != self.mines: + self.field = [] + minesAdded = 0 + for y in range(self.height): + row = [] + for x in range(self.width): + row.append(Cell(chance)) + if row[x].getIsMine(): + minesAdded += 1 + self.field.append(row) + return + + def createHints(self): + for y in range(len(self.field)): + for x in range(len(self.field[y])): + cell = self.field[y][x] + if cell.getIsMine(): + cell.setValue('x') + else: + m = str(self.getMinesAround(x,y)) + cell.setValue(m) + return + + def getMinesAround(self,x,y): + mines = 0 + for i in range(-1,2): + for j in range(-1,2): + xi = x+i + yj = y+j + if xi >= 0 and yj >= 0 and xi < self.width and yj < self.height: + if self.field[yj][xi].getIsMine(): + mines += 1 + return mines + + def uncoverEmptyAround(self,x,y): + for i in range(-1,2): + for j in range(-1,2): + xi = x+i + yj = y+j + if xi >= 0 and yj >= 0 and xi < self.width and yj < self.height: + cell = self.field[yj][xi] + if cell.isCovered(): + self.guess(xi,yj) + return + + def printField(self): + i = 1 + print("\n\t", end="") + for char in range(0,self.width): + print(chr(char+65) + " ", end="") + print("\n") + for list in self.field: + print(str(i) + "\t", end = '') + for item in list: + print(item.printCell() + " ", end='') + print() + i += 1 + return + + def cleared(self): + safe = 0 + for y in range(len(self.field)): + for x in range(len(self.field[y])): + cell = self.field[y][x] + if cell.getIsMine() and cell.isSafe(): + safe += 1 + + if safe == self.mines: + cleared = True + else: + cleared = False + + return cleared + + + + def guess(self,x,y): + cell = self.field[y][x] + if cell.getValue() == " ": + cell.uncover() + self.uncoverEmptyAround(x,y) + return False + elif cell.getValue() == 'x': + cell.uncover() + return True + else: + cell.uncover() + return False + + def flag(self,x,y): + cell = self.field[y][x] + cell.toggleFlag() \ No newline at end of file diff --git a/game.py b/game.py new file mode 100644 index 0000000..c8927b5 --- /dev/null +++ b/game.py @@ -0,0 +1,126 @@ +#!/usr/bin/python + +try: + input = raw_input +except NameError: + pass + +from field import Field + +class Help: + def setState(self,l): + self.loop = l + + def printHelp(self): + print("") + print("Listed commands:") + print(" try\t\t\tTests for mines") + print(" flag\t\t\tPlaces flag") + print(" ?\t\t\tPlaces questionmark") + print(" restart\t\tStarts new game") + print(" quit or exit\t\tQuits game") + print(" help\t\t\tPrints list of commands") + self.loop.command() + +class Setup: + def setState(self,l): + self.loop = l + + def setup(self): + print("") + print("Select diffeculty:") + print(" 1. Beginner\t\t(10 mines, 9x9)") + print(" 2. Intermediate\t(40 mines, 16x16)") + print(" 3. Expert\t\t(99 mines, 30x16)") + print(" 4. Custom") + print("") + + n = input("Choice: ") + n = n.split() + choice = int(n[0]) + + if choice == 1: + w = 9 + h = 9 + m = 10 + elif choice == 2: + w = 16 + h = 16 + m = 40 + elif choice == 3: + w = 30 + h = 16 + m = 99 + elif choice == 4: + w = int(input("Width: ")) + h = int(input("Heigt: ")) + m = int(input("Mines: ")) + else: + print(str(choice) + " is not a option.") + self.setup() + + minefield = Field(w,h,m) + self.loop.setMinefield(minefield) + self.loop.command() + +class Loop: + def setStates(self,s,h,e): + self.setup = s + self.end = e + self.help = h + + def setMinefield(self,m): + self.minefield = m + + def command(self): + self.minefield.printField() + if(self.minefield.cleared()): + self.end.endGame(False) + + command = input("Command >> ") + c = command.split(' ') + + if c[0] == "try": + if self.minefield.guess(ord(c[1])-65,int(c[2])-1): + self.minefield.printField() + self.end.endGame(True) + self.command() + elif c[0] == "flag": + self.minefield.flag(ord(c[1])-65,int(c[2])-1) + self.command() + elif c[0] == "?": + self.minefield.question(ord(c[1])-65,int(c[2])-1) + self.command() + elif c[0] == "restart": + self.setup.setup() + elif c[0] == "quit" or c[0] == "exit": + exit() + elif c[0] == "help": + self.help.printHelp() + else: + print(c[0] + " is not a recognized command") + self.help.printHelp() + +class End: + def setState(self,s): + self.setup = s + + def restart(self): + choice = input("Do you want to start again? [y/n] ") + choice = choice.split() + choice = choice[0] + if choice == "y": + self.setup.setup() + elif choice == "n": + exit() + else: + print(str(choice) + " is not a valid choice") + self.restart() + + def endGame(self,m): + print("") + if m: + print("You hit a mine :o") + else: + print("You won :D") + self.restart() \ No newline at end of file diff --git a/minesweeper.py b/minesweeper.py new file mode 100644 index 0000000..303bb05 --- /dev/null +++ b/minesweeper.py @@ -0,0 +1,15 @@ +#!/usr/bin/python + +import game + +help = game.Help() +setup = game.Setup() +loop = game.Loop() +end = game.End() + +help.setState(loop) +setup.setState(loop) +end.setState(setup) +loop.setStates(setup,help,end) + +setup.setup() \ No newline at end of file -- cgit v1.2.3