-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameboard.py
More file actions
90 lines (80 loc) · 2.32 KB
/
gameboard.py
File metadata and controls
90 lines (80 loc) · 2.32 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#
# GameBoard module
# -
# Represent a board to play on using a 2d array from NumPy
# NumPy : http://sourceforge.net/projects/numpy/files/NumPy/1.7.1/
#
# Author: Tom Renn
#
import numpy as np
import nodeModule
BOARD_WIDTH = 7
BOARD_HEIGHT = 6
EMPTY_SPOT = 0
class GameBoard:
# Constructor
# If no board is given, create one containing all 0s
def __init__(self, baseBoard=None):
if baseBoard is None:
self.board = np.zeros( (BOARD_HEIGHT, BOARD_WIDTH), dtype=np.int16)
else:
self.board = baseBoard
# place chip in a given column index
# return (row, col) of newly placed chip, or (-1, -1) if unable to be placed
def moveAt(self, index, player):
if index not in range(BOARD_WIDTH):
return (-1, -1)
# go from bottom of the board up
for i in reversed(range(BOARD_HEIGHT)):
row = self.board[i]
if row[index] == EMPTY_SPOT: # found empty row
row[index] = player
return (i, index)
else: # row occupied
continue
# if we got this far, the move is not able to be made
return (-1, -1)
# return the chip at the given row and column
# returns EMPTY_SPOT if board bounds are exceeded
def getChipAt(self, row, col):
if row >= 0 and row < BOARD_HEIGHT:
if col >= 0 and col < BOARD_WIDTH:
return self.board[row][col]
return EMPTY_SPOT
# return indexes of available moves
def getAvailableMoves(self):
movesAvailable = []
for i in range(BOARD_WIDTH):
if self.board[0][i] == EMPTY_SPOT:
movesAvailable.append(i)
return movesAvailable
# prints the gameboard in a nice format
def fancyPrint(self):
for row in range(BOARD_HEIGHT):
rowString = '|'
for col in range(BOARD_WIDTH):
pos = self.board[row][col]
if pos == EMPTY_SPOT:
# if bottom row and empty show _
if row == (BOARD_HEIGHT - 1):
pos = '_'
else:
pos = ' '
elif pos == nodeModule.PLAYER_MIN:
pos = 'P'
else:
pos = 'C'
rowString = rowString + pos + '|'
print rowString
# print index on bottom
rowString = '|'
for row in range(BOARD_WIDTH):
rowString = rowString + str(row) + '|'
print rowString
# number of available moves
def availableMoves(self):
count = 0
for i in range(BOARD_WIDTH):
if self.board[0][i] == EMPTY_SPOT:
count = count + 1
return count