-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinesweeper.py
71 lines (55 loc) · 1.85 KB
/
Minesweeper.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
#!/usr/bin/python2
# -*- coding: utf-8 -*-
import numpy as np
def check(row, column):
global game_size
global game_filed
points = []
for x in (-1, 0, 1):
if row + x not in range(game_size): continue
for y in (-1, 0, 1):
if column + y not in range(game_size): continue
points.append(minesweeper_field[row + x, column + y])
game_filed[row, column] = points.count(1)
if game_filed[row, column] == str(0):
for x in (-1, 0, 1):
if row + x not in range(game_size): continue
for y in (-1, 0, 1):
if column + y not in range(game_size): continue
if game_filed[row + x, column + y] != str(0):
check(row + x, column + y)
print """
HOW TO PLAY?
When game ask you for field just type two numbers (split by comma).
For example:
Chose filed> 3,4
Counting fields starts from 1.
"""
while True:
try:
game_size = int(raw_input("Size of game field: "))
break
except ValueError:
print "You have to give an intiger!"
minesweeper_field = np.random.randint(6, size=(game_size, game_size))
for x in np.nditer(minesweeper_field, op_flags=['readwrite']):
if x > 1: x[...] = 0
game_filed = np.chararray((game_size, game_size))
game_filed[:] = "?"
while True:
print game_filed
chosen_field = (raw_input("Chose filed> ")).split(",")
chosen_field[0] = int(chosen_field[0]) - 1
chosen_field[1] = int(chosen_field[1]) - 1
if minesweeper_field[chosen_field[0], chosen_field[1]] == 1:
print "GAME OVER!"
print minesweeper_field
break
try:
check(chosen_field[0], chosen_field[1])
except IndexError:
print "Wrong input!"
if str(minesweeper_field).count("1") == str(game_filed).count("?"):
print "YOU WIN!"
print minesweeper_field
break