def __init__(self, player): self.board = [[0 for x in xrange(WIDTH)] for x in xrange(HEIGHT)] self.move_count = 0 # if player == 2: # self.heuristic = MatrixBoardHeuristic(player) # else: self.heuristic = ThreatHeuristic(player)
class MatrixBoard(object): def __init__(self, player): self.board = [[0 for x in xrange(WIDTH)] for x in xrange(HEIGHT)] self.move_count = 0 # if player == 2: # self.heuristic = MatrixBoardHeuristic(player) # else: self.heuristic = ThreatHeuristic(player) def array(self): return self.board def move(self, column): idx = 5 while self.board[idx][column] != 0: idx = idx - 1 self.board[idx][column] = self.get_current_player() self.move_count += 1 def unmove(self, column): self.move_count -= 1 idx = 5 while idx >= 0 and self.board[idx][column] != 0: idx = idx - 1 self.board[idx+1][column] = 0 def get_current_player(self): return self.move_count % 2 + 1 @Connect4Timer def check_winner(self, p, column): for i in range(6): for j in range(7): if self.board[i][j] == p: # See if it has 3 more in any direction starting here if i<=2 and self.board[i+1][j]==p and self.board[i+2][j]==p and self.board[i+3][j]==p: return True if i>=3 and self.board[i-1][j]==p and self.board[i-2][j]==p and self.board[i-3][j]==p: return True if j<=3 and self.board[i][j+1]==p and self.board[i][j+2]==p and self.board[i][j+3]==p: return True if j>=3 and self.board[i][j-1]==p and self.board[i][j-2]==p and self.board[i][j-3]==p: return True if i<=2 and j<=3 and self.board[i+1][j+1]==p and self.board[i+2][j+2]==p and self.board[i+3][j+3]==p: return True if i<=2 and j>=3 and self.board[i+1][j-1]==p and self.board[i+2][j-2]==p and self.board[i+3][j-3]==p: return True if i>=3 and j<=3 and self.board[i-1][j+1]==p and self.board[i-2][j+2]==p and self.board[i-3][j+3]==p: return True if i>=3 and j>=3 and self.board[i-1][j-1]==p and self.board[i-2][j-2]==p and self.board[i-3][j-3]==p: return True return False @Connect4Timer def heuristic_value(self, max_player): #if max_player == PLAYER_ONE: return self.heuristic.evaluate_board(self, max_player) def valid_moves(self): return [move for move in range(WIDTH) if self.board[0][move]==0] def __eq__(self, other): return self.move_count == other.move_count and (self.board == other.board or self.reversed_board() == other.board) def get(self, square): return self.board[square[0]][square[1]] def reversed_board(self): return [list(reversed(row)) for row in self.board] def __hash__(self): # represent each row as a ternary little-endian integer and # add the rows h = 0 for row in self.board: fctr = 1 for c in row: h += c * fctr fctr *= 3 return h def __str__(self): result = "" result += "0 1 2 3 4 5 6\n" result += "-------------------\n" """Prints out the game board""" for row in self.board: for el in row: result += str(el) + " " result += "\n" return result