-
Notifications
You must be signed in to change notification settings - Fork 0
/
boardUtils.py
66 lines (54 loc) · 1.4 KB
/
boardUtils.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
import io,math,random,time, json
import numpy as np
mario = "M"
toad = "T"
wall = "#"
ground = " "
spacer = " "
marioTrail = "."
toadTrail = " "
dataDir = './maps/'
def buildCircle(r):
y,x = np.ogrid[-r: r+1, -r: r+1]
mask = x**2+y**2 <= r**2-1
return mask
def buildBoard(n):
circle = buildCircle(n);
board = []
for row in circle:
board.append([ground if cell else wall for cell in row])
return board
def drawBoard(board):
for row in board:
st = ""
for cell in row:
st += spacer + cell
print st
def writeBoard(board):
serializedBoard = []
for row in board:
line = ""
for cell in row:
line += cell
line+="\n"
serializedBoard.append(line)
timestr = time.strftime("%Y%m%d-%H%M%S")
out = open(dataDir + timestr,'w')
out.write("".join(serializedBoard))
def readBoard(fileName):
f = open(dataDir + fileName,'r')
board = []
for line in list(f):
row = []
for cell in line:
row.append(cell)
board.append(row[:-1]) # remove newline
return board
def jsonBoard(board): # returns json strong of array of given board state.
walls = []
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] is wall:
d = {'x':i,'y':j,'type':'wall'}
walls.append(d)
return walls