diff options
Diffstat (limited to 'minesweeper/game.py')
| -rw-r--r-- | minesweeper/game.py | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/minesweeper/game.py b/minesweeper/game.py new file mode 100644 index 0000000..9ef7b16 --- /dev/null +++ b/minesweeper/game.py | |||
| @@ -0,0 +1,124 @@ | |||
| 1 | from minesweeper.field import Field | ||
| 2 | |||
| 3 | |||
| 4 | class Help: | ||
| 5 | def set_state(self, loop): | ||
| 6 | self.loop = loop | ||
| 7 | |||
| 8 | def print(self): | ||
| 9 | print("""Listed commands: | ||
| 10 | try <x> <y> Tests for mines | ||
| 11 | flag <x> <y> Places flag | ||
| 12 | restart Starts new game | ||
| 13 | quit or exit Quits game | ||
| 14 | help Prints list of commands | ||
| 15 | """) | ||
| 16 | self.loop.command() | ||
| 17 | |||
| 18 | |||
| 19 | class Setup: | ||
| 20 | def set_state(self, loop): | ||
| 21 | self.loop = loop | ||
| 22 | |||
| 23 | def setup(self): | ||
| 24 | print("""Select diffeculty: | ||
| 25 | 1. Beginner (10 mines, 9x9) | ||
| 26 | 2. Intermediate (40 mines, 16x16) | ||
| 27 | 3. Expert (99 mines, 30x16) | ||
| 28 | 4. Custom" | ||
| 29 | """) | ||
| 30 | |||
| 31 | choice = int( | ||
| 32 | input("Choice: ").split()[0]) | ||
| 33 | |||
| 34 | if choice == 1: | ||
| 35 | width = 9 | ||
| 36 | height = 9 | ||
| 37 | mines = 10 | ||
| 38 | elif choice == 2: | ||
| 39 | width = 16 | ||
| 40 | height = 16 | ||
| 41 | mines = 40 | ||
| 42 | elif choice == 3: | ||
| 43 | width = 30 | ||
| 44 | height = 16 | ||
| 45 | mines = 99 | ||
| 46 | elif choice == 4: | ||
| 47 | width = int(input("Width: ")) | ||
| 48 | height = int(input("Height: ")) | ||
| 49 | mines = int(input("Mines: ")) | ||
| 50 | else: | ||
| 51 | print(str(choice) + " is not a option.") | ||
| 52 | self.setup() | ||
| 53 | |||
| 54 | minefield = Field(width, height, mines) | ||
| 55 | self.loop.set_minefield(minefield) | ||
| 56 | self.loop.command() | ||
| 57 | |||
| 58 | |||
| 59 | class Loop: | ||
| 60 | def set_states(self, setup, help, end): | ||
| 61 | self.setup = setup | ||
| 62 | self.end = end | ||
| 63 | self.help = help | ||
| 64 | |||
| 65 | def set_minefield(self, minefield): | ||
| 66 | self.minefield = minefield | ||
| 67 | |||
| 68 | def command(self): | ||
| 69 | self.minefield.print() | ||
| 70 | if self.minefield.cleared: | ||
| 71 | self.end.end_game(False) | ||
| 72 | |||
| 73 | command = input("Command >> ").split(' ') | ||
| 74 | |||
| 75 | if len(command) == 3: | ||
| 76 | coordinate = (ord(command[1]) - 65, int(command[2]) - 1) | ||
| 77 | |||
| 78 | if command[0] == "try": | ||
| 79 | if self.minefield.guess(*coordinate): | ||
| 80 | self.minefield.print() | ||
| 81 | self.end.end_game(dead=True) | ||
| 82 | return | ||
| 83 | self.command() | ||
| 84 | elif command[0] == "flag": | ||
| 85 | self.minefield.flag(*coordinate) | ||
| 86 | self.command() | ||
| 87 | # elif command[0] == "?": | ||
| 88 | # self.minefield.question(coordinate) | ||
| 89 | # self.command() | ||
| 90 | elif command[0] == "restart": | ||
| 91 | self.setup.setup() | ||
| 92 | elif command[0] == "quit" or command[0] == "exit": | ||
| 93 | exit() | ||
| 94 | elif command[0] == "help": | ||
| 95 | self.help.print() | ||
| 96 | else: | ||
| 97 | print(command[0] + " is not a recognized command") | ||
| 98 | self.help.print() | ||
| 99 | |||
| 100 | |||
| 101 | class End: | ||
| 102 | def set_state(self, setup): | ||
| 103 | self.setup = setup | ||
| 104 | |||
| 105 | def restart(self): | ||
| 106 | choice = input("Do you want to start again? [y/n] ").split()[0] | ||
| 107 | |||
| 108 | if choice == "y": | ||
| 109 | self.setup.setup() | ||
| 110 | elif choice == "n": | ||
| 111 | exit() | ||
| 112 | else: | ||
| 113 | print(str(choice) + " is not a valid choice") | ||
| 114 | self.restart() | ||
| 115 | |||
| 116 | def end_game(self, dead): | ||
| 117 | print("") | ||
| 118 | |||
| 119 | if dead: | ||
| 120 | print("You hit a mine :o") | ||
| 121 | else: | ||
| 122 | print("You won :D") | ||
| 123 | |||
| 124 | self.restart() | ||
