-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
94 lines (73 loc) · 2.03 KB
/
Main.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
__author__ = 'cthulhu'
import Exceptions
from Board import Board
from Cell import Cell
# Player Classes
from RandomBot import RandomBot
from HumanPlayer import HumanPlayer
from MinesweeperAi import MinesweeperAi
# Game Config
size = 10
nMines = 16
######################
# Instanciate Player #
######################
#
# Your Ai Bot
#
# player = MinesweeperAi()
#
# Random Ai
#
# player = RandomBot()
#
# Default Humain Player
#
player = HumanPlayer()
# Instanciate Minesweeper Board
minesweeper = Board(size, nMines)
##############
# Start Game #
##############
first_turn = True
print("Starting Game")
minesweeper.reveal()
print("========================")
minesweeper.show()
print("========================")
# Game Loop
while True:
try:
# Get Move from player
# target_cell : Cell or tuple(row, column)
# is_flag : Boolean
target_cell, is_flag = player.get_move(minesweeper)
# Print chosen action
if isinstance(target_cell, Cell):
i, j = minesweeper.get_cell_coord(target_cell)
else:
i, j = target_cell
print("Action Chosen : ({}, {}) , Flag : {}".format(i, j, is_flag))
if first_turn:
print("First Move")
# Apply Action
# Result is Boolean : True = blew up a mine , False = you're fine
try:
minesweeper.player_action(target_cell, is_flag, first_turn)
except Exceptions.YouBlewUp as BOOM:
print(BOOM)
minesweeper.reveal()
break
if minesweeper.is_solved():
print("You Win !")
minesweeper.reveal()
break
else:
minesweeper.show()
print("========================================\n")
# Catch exceptions to help player's bot spot errors easily
except (Exceptions.IndicesOutOfBoundsException, Exceptions.InvalidFlagTarget) as e:
print(e)
break
# Flip the 'first_turn' flag when player actually reveals a cell
first_turn = first_turn and is_flag