def depth_score(self, board, depth, player): import BoardEval as Be if depth == 0: return Be.score_matrix(board) if player == 1: # if is computer turn _total_score = 0 zeros = [i for i in range(16) if ((board >> (i*4)) & 0xf) == 0] for zero in zeros: _board = board | (1 << (zero*4)) # only consider insert 2 first _total_score += self.depth_score(_board, depth-1, 0) return _total_score/len(zeros) else: # if is player turn import move_table best = 0 moves = [move_table.move_up, move_table.move_down, move_table.move_left, move_table.move_right] for direction in moves: _board = direction(board) # skip those invalid move if _board == board: continue _score = self.depth_score(_board, depth-1, 1) if _score > best: best = _score return best
def score_board(self): import BoardEval as Be return Be.score_matrix(self.matrix)