Exemple #1
0
def eval(player, cols, board):
    #print player, cols
    #r4.printBoard(cols, board)
    count = 0
    opp = opponent(player)
    for x in range(cols):
    	y = r4.findMinRow(x, cols, board)
        if y*cols >= len(board):
            continue
    	board[y*cols + x] = player
    	length = r4.findLength(x, y, cols, board)
        if length >= 4:
            count += 1
        elif (y+1)*cols < len(board):
            board[(y+1)*cols + x] = opp
            length = r4.findLength(x, y+1, cols, board)
            if length >= 4:
            	count -= 1
            board[(y+1)*cols + x] = 0
        board[y*cols + x] = opp
        length = r4.findLength(x, y, cols, board)
        if length >= 4:
            count -= 1
        elif (y+1)*cols < len(board):
            board[(y+1)*cols + x] = player
            length = r4.findLength(x, y, cols, board)
            if length >= 4:
            	count += 1
            board[(y+1)*cols + x] = 0
        board[y*cols + x] = 0
    return count
Exemple #2
0
def abminimax(player, cols, board, recur, alpha, beta, dict):
    #print board
    opp = opponent(player)
    winner = r4.findWinner(cols, board)
    xlen = cols
    ylen = len(board)/cols
    if winner == player:
    	return eval_max+recur, -1
    elif winner == opp:
    	return eval_min-recur, -1
    if recur == 0:
    	return eval(player, cols, board), -1
    b_hash = hashBoard(board)
    # I should be using a better check than this... ie a full comparision
    # after the hash comparison
    if b_hash in dict:
    	#score, b_hash_str = dict[b_hash]
        #if b_hash_str == str(board):
    	    #print "skip! to %i" % dict[b_hash]
    	#    return score, -1
        #else:
        #    print "false alarm :("
        return dict[b_hash], -1
    best_move = -1
    valid_moves = []
    moves = range(xlen)
    random.shuffle(moves)
    for x in moves: #range(xlen):
    	y = r4.findMinRow(x, cols, board)
        if r4.isValid(x, y, cols, board):
            valid_moves.append(x)
        else:
            continue
        board[y*cols + x] = player
        score, bluh = abminimax(opp, cols, board, recur - 1,
                -beta, -alpha, dict)
        board[y*cols + x] = 0
        if score < beta:
            beta = score
            best_move = x
        if beta <= alpha:
            #print "ab pruning, returning %s" % str((beta, recur, best_move))
            dict[b_hash] = -beta #, str(board)
            return -beta, best_move
    # if there are no valid moves
    if len(valid_moves) == 0:
    	beta = min(beta, 0)
    # if you are going to lose...
    elif best_move == -1:
        # this is a random move, because moves was shuffled
        best_move = valid_moves[0]
    dict[b_hash] = -beta #, str(board)
    return -beta, best_move