-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
94 lines (80 loc) · 2.2 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
94
import os
from gameboard import GameBoard
Defuse = 0
Explore = 1
Exit = 2
command_dict = {'e': Explore, 'd': Defuse, 'q': Exit}
def initialize():
world_size = 0
mine_count = 0
while(True):
try:
world_size = int(input('size: '))
break
except:
print('wrong input type')
while(True):
try:
mine_count = int(input('mines: '))
if mine_count <= world_size * world_size:
break
print(f'mines should be less than number of cells ({world_size*world_size})')
except:
print('wrong input type')
return GameBoard(world_size, mine_count)
def take_cell(max_row, max_col):
row=0
col=0
print('Enter your move:')
while(True):
try:
row = int(input('row: '))
if row < max_row:
break
print(f'row should be less than {max_row}. counting from zero.')
except:
print('wrong input type')
while(True):
try:
col = int(input('col: '))
if col < max_col:
break
print(f'column should be less than {max_col}. counting from zero.')
except:
print('wrong input type')
return row, col
def take_command():
command=0
while(True):
try:
command = input('Defuse(d)/Explore(e): ')
if command in ('e', 'd', 'q'):
command = command_dict[command]
break
print('please enter `e` or `d`')
except:
print('wrong input type')
return command
def clear_console():
if os.name == 'posix':
os.system('clear')
else:
os.system('cls')
def run():
gameboard = initialize()
size = gameboard.world_size
while not gameboard.is_game_over():
clear_console()
gameboard.show()
command = take_command()
if command == Exit:
break
row, col = take_cell(size, size)
if command == Explore:
gameboard.explore(row, col)
elif command == Defuse:
gameboard.defuse(row, col)
clear_console()
gameboard.show()
if __name__ == '__main__':
run()