-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolvepuzzles.py
88 lines (70 loc) · 2 KB
/
solvepuzzles.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
import json
import argparse
UNKNOWN_SQUARE = '?'
MINE_SQUARE = '+'
FLAG_SQUARE = '*'
IS_MINE = -1
VALID_START_LOC = 'y'
INVALID_START_LOC = 'n'
def printSol(puzzle):
output = ''
for i in range(puzzle['width']):
for j in range(puzzle['height']):
if [i, j] in puzzle['mines']:
output += MINE_SQUARE
else:
output += str(getNearMines(puzzle, i, j))
output += '\n'
print(output)
def printStage(puzzle):
output = ''
for i in range(puzzle['width']):
for j in range(puzzle['height']):
output += str(puzzle['knownMat'][i][j])
output += '\n'
print(output)
def getNearMines(puzzle, x, y):
if [x, y] in puzzle['mines']:
return IS_MINE #Hit a mine :(
near = 0
for k in [-1, 0, 1]:
for l in [-1, 0, 1]:
if [x+k, y+l] in puzzle['mines']:
near += 1
return near
def getNewBlankKnownMat():
return [[UNKNOWN_SQUARE for y in range(height)] for x in range(width)]
def initPuzzle(mineList, width, height):
puzzle = {'mines': mineList}
puzzle['knownMat'] = getNewBlankKnownMat()
puzzle['startingLocs'] = [[VALID_START_LOC for y in range(height)] for x in range(width)]
puzzle['width'] = width
puzzle['height'] = height
return puzzle
def _solvePuzzle(puzzle, x, y):
if [x, y] in puzzle['mines']:
print('There was a big error...')
sys.exit(0)
getStartingPoints(puzzle)
print(puzzle['startingLocs'])
def solvePuzzle(puzzle):
for i in range(puzzle['width']):
for j in range(puzzle['height']):
if [i, j] in puzzle['mines']:
puzzle['startingLocs'][i][j] = INVALID_START_LOC
else:
_solvePuzzle(puzzle)
parser = argparse.ArgumentParser(description='Generate games of minesweeper.')
parser.add_argument('--filename', metavar='-f', default='puzzle.txt', help='.', dest='filename')
args = parser.parse_args()
f = open(args.filename, mode='r')
filedata = json.load(f)
width = filedata['width']
height = filedata['height']
puzzles = filedata['puzzles']
for mineList in puzzles:
puzzle = initPuzzle(mineList, width, height)
printSol(puzzle)
print('\n\n')
solvePuzzle(puzzle)
f.close()