/
board.py
97 lines (84 loc) · 2.98 KB
/
board.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
95
96
97
import os
import copy
from note import Note
from governance import Governance
from drive import Drive
from preview import Preview
from relay import Relay
from shield import Shield
from square import Square
class Board:
"""
Class that represents the BoxShogi board
"""
# The BoxShogi board is 5x5
def __init__(self):
self._board = self._initEmptyBoard()
def getBoard(self):
return self._board
def _initEmptyBoard(self):
"""
:return: the initialized board
"""
board = [[Square(None,i,j) for j in range(5)] for i in range(5)]
board[0][0] = Square(Note(False), 0, 0)
board[0][1] = Square(Governance(False), 0, 1)
board[0][2] = Square(Relay(False), 0, 2)
board[0][3] = Square(Shield(False), 0, 3)
board[0][4] = Square(Drive(False), 0, 4)
board[1][4] = Square(Preview(False), 1, 4)
board[3][0] = Square(Preview(True), 3, 0)
board[4][0] = Square(Drive(True), 4, 0)
board[4][1] = Square(Shield(True), 4, 1)
board[4][2] = Square(Relay(True), 4, 2)
board[4][3] = Square(Governance(True), 4, 3)
board[4][4] = Square(Note(True), 4, 4)
return board
def __repr__(self):
return self._stringifyBoard(self.rotate90Clockwise(copy.deepcopy(self._board)))
def _stringifyBoard(self, board):
"""
Utility function for printing the board
"""
s = ''
for row in range(len(board) - 1, -1, -1):
s += '' + str(row + 1) + ' |'
for col in range(0, len(board[row])):
s += self._stringifySquare(board[col][row])
s += os.linesep
s += ' a b c d e' + os.linesep
return s
def rotate90Clockwise(self,A):
"""
I would like to represent the matrix in a slightly different way than
the expected output. For example, A[0][0] = a5. Therefore, I need to
rotate my matrix by 90 degree clockwise to match the expected output.
:param A: An input matrix
:return: the input matrix A rotated by 90 degrees
"""
N = len(A[0])
for i in range(N // 2):
for j in range(i, N - i - 1):
temp = A[i][j]
A[i][j] = A[N - 1 - j][i]
A[N - 1 - j][i] = A[N - 1 - i][N - 1 - j]
A[N - 1 - i][N - 1 - j] = A[j][N - 1 - i]
A[j][N - 1 - i] = temp
return A
def _stringifySquare(self, sq):
"""
Utility function for stringifying an individual square on the board
:param sq: Array of strings.
"""
if sq.getPiece():
sq = repr(sq.getPiece())
else:
sq = "__"
if type(sq) is not str or len(sq) > 2:
raise ValueError('Board must be an array of strings like "", "P", or "+P"')
if len(sq) == 0:
return '__|'
if len(sq) == 1:
return ' ' + sq + '|'
if len(sq) == 2:
return sq + '|'