-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminesweeper_cli.py
More file actions
66 lines (61 loc) · 2.4 KB
/
Copy pathminesweeper_cli.py
File metadata and controls
66 lines (61 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import codes
from minesweeper import Minesweeper
from sweeper import Sweeper
class MinesweeperCLI:
def __init__(self, width, height, number_bombs) -> None:
self.minesweeper = Minesweeper(width, height, number_bombs)
self.sweeper = Sweeper(self.minesweeper.gamefield)
def play(self, auto_play=False):
while self.minesweeper.game_status == codes.ONGOING:
print(self.minesweeper.gamefield)
if auto_play:
self.use_guess()
else:
tile, command = self.prompt()
# input looks like: x y
if command == codes.FLIPPED:
self.minesweeper.flip(tile)
# input looks like: x y F
elif command == codes.FLAGGED:
self.minesweeper.flag_or_unflag(tile)
# input looks like: G (or empty)
elif command == codes.GUESS:
self.use_guess()
# when game is complete
print(self.minesweeper.gamefield)
if self.minesweeper.game_status == codes.WON:
print("Game won!")
return True
else:
print("Game lost.")
return False
def use_guess(self):
tile, guess, prob = self.sweeper.guess()
print(f"Confidence: {prob:.2f}")
if guess == codes.FLIPPED:
self.minesweeper.flip(tile)
elif guess == codes.FLAGGED:
self.minesweeper.flag_or_unflag(tile)
def prompt(self) -> tuple:
try:
line = input("Guess: ")
if line in ["G", ""]:
return ((), codes.GUESS)
line_split = line.split()
tile = (int(line_split[0]), int(line_split[1]))
if len(line_split) not in [2, 3]:
raise Exception()
if tile[0] < 0 or tile[0] >= self.minesweeper.gamefield.width:
raise Exception()
if tile[1] < 0 or tile[1] >= self.minesweeper.gamefield.height:
raise Exception()
if len(line_split) == 3:
if line_split[2] == "F":
return (tile, codes.FLAGGED)
else:
raise Exception()
return (tile, codes.FLIPPED)
except:
print(
"Invalid input, try again. Usage: x y [F]. Example: '5 2' to flip tile at column 5, row 2")
return self.prompt()