- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBoard.py
More file actions
Latest commit
72 lines (56 loc) · 1.78 KB
/
Copy pathBoard.py
File metadata and controls
72 lines (56 loc) · 1.78 KB
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
'''
Base class Board for creating all the board applications
'''
classPoint(object):
'''
Position for the square on the board with row and column
'''
def__init__(self, row, col):
self.row=row
self.col=col
def__str__(self):
return'row: %d, col: %d'% (self.row, self.col)
classBoard(object):
'''
Create Board with initial character
'''
def__init__(self, num_rows=3, num_cols=3, init_char='x'):
self.init_char=init_char
self.num_rows=num_rows
self.num_cols=num_cols
self._rows= [[init_charforiinrange(num_cols)] forjinrange(num_rows)]
def__getitem__(self, point):
returnself._rows[point.row][point.col]
defget_by_pos(self,row,col):
returnself._rows[row][col]
defset_by_pos(self,row,col,letter):
self._rows[row][col] =letter
def__setitem__(self, point, letter):
self._rows[point.row][point.col] =letter
defget_num_rows(self):
returnself.num_rows
defget_num_cols(self):
returnself.num_cols
def__str__(self):
'''
Display the board with borders
'''
return'Board:\n'+'\n'.join(['['+'|'.join(row) +']'forrowinself._rows])
defempty(self, point):
'''
Determine whether the space is isEmpty
Args:
point: position for the space on the board with row and column
Return:
True if the space in this point is empty
'''
returnself._rows[point.row][point.col] ==self.init_char
defvalid(self, point):
'''Determine whether a point is valid.
Args:
point: position for the space on the board with row and column
Returns:
True if the position exist in the board
'''
return (point.row>=0andpoint.row<self.num_rowsand
point.col>=0andpoint.col<self.num_cols)