-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboard.py
executable file
·75 lines (63 loc) · 2.04 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
#! /usr/bin/python3
from numpy import *
import math
from err import *
class board:
def __init__(self, M, N, K, symbols):
"""
Initilize a board of size m (rows) x n (columns), with k players playing in turn.
Players use the symbols specified.
"""
self.row = N
self.col = M
self.max_address = (M * N)
self.num_players = K
self.players = list(range(0, (K+1)))
self.sym = [' ']+symbols
self.reset()
def reset(self):
row = [0] * self.row
self.board = zeros((self.col, self.row), 'int')
self.free_positions = self.col * self.row
def get_xy_from_pos(self, pos):
x = int(math.floor(pos / self.row))
y = pos % self.col
return x, y
def set_pos(self, pos, player):
if(pos < self.max_address):
# x = int (math.floor( pos / self.row ))
# y = pos % self.col
x, y = self.get_xy_from_pos(pos)
if(self.board[x][y] == 0):
self.board[x][y] = player
returnVal = err.OK
self.free_positions -= 1
else:
returnVal = err.INVALID_MOVE
# dbg(1,"X:" + str(x) + " Y:" +str(y)+" R:"+str(returnVal))
else:
returnVal = err.INVALID_MOVE
return returnVal
def get_board_str(self):
board_string = '\n'
for j, rows in enumerate(self.board):
for i, e in enumerate(rows):
board_string += ' ' + self.sym[e] + ' '
if i < len(rows)-1:
board_string += '|'
if(j < len(self.board)-1):
board_string += '\n---+---+---\n'
board_string += '\n'
return board_string
def count_empty_squares(self):
self.free_positions = sum(sum(self.board == 0))
return None
def dbg(level, message):
if (level == 1):
print(message)
def test():
b = board(3, 3, 2, ['X', 'O'])
b.set_pos(0, 1)
b.set_pos(0, 1)
if __name__ == '__main__':
test()