Exemplo n.º 1
0
def move(player, board):
    xlen = len(board[0])
    ylen = len(board)
    moves = range(xlen)
    random.shuffle(moves)
    best_move = -1
    for m in moves:
        y = r4.findMinRow(m, board)
        if not r4.isValid(m, y, board):
            continue
        r4.setLoc(m, y, player, board)
        winner = r4.findWinner(board)
        r4.setLoc(m, y, 0, board)
        if winner == 0:
            best_move = m
        elif winner == player:
            best_move = m
            break
    return best_move
Exemplo n.º 2
0
def abminimax(player, board, recur, alpha, beta):
    opp = opponent(player)
    winner = r4.findWinner(board)
    xlen = len(board[0])
    ylen = len(board)
    if winner == player:
    	return eval_max, -1
    elif winner == opp:
    	return eval_min, -1
    if recur == 0:
    	return eval2(player, board), -1
    best_move = -1
    valid_moves = []
    moves = range(xlen)
    random.shuffle(moves)
    for x in moves:
    	y = r4.findMinRow(x, board)
        if r4.isValid(x, y, board):
            valid_moves.append(x)
        else:
            continue
        r4.setLoc(x, y, player, board)
        score, bluh = abminimax(opp, board, recur - 1,
                -beta, -alpha)
        r4.setLoc(x, y, 0, board)
        if score < beta:
            beta = score
            best_move = x
        if beta <= alpha:
            #print "ab pruning, returning %s" % str((beta, recur, best_move))
            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 beta == eval_max:
        # this is a random move, because moves was shuffled
        best_move = valid_moves[0]
    return -beta, best_move
Exemplo n.º 3
0
def minimax(player, board, recur):
    opp = opponent(player)
    winner = r4.findWinner(board)
    xlen = len(board[0])
    ylen = len(board)
    if winner == player:
    	return eval_max, -1, 0
    elif winner == opp:
    	return eval_min, -1, 0
    if recur == 0:
    	return eval(player, board), -1, 0
    min_score = eval_max
    min_time = recur
    best_move = -1
    valid_moves = []
    moves = range(xlen)
    random.shuffle(moves)
    for x in moves:
    	y = r4.findMinRow(x, board)
        if r4.isValid(x, y, board):
            valid_moves.append(x)
        else:
            continue
        r4.setLoc(x, y, player, board)
        score, m, time = minimax(opp, board, recur - 1)
        r4.setLoc(x, y, 0, board)
        if (score < min_score or score == min_score and time < min_time):
            min_score = score
            best_move = x
            min_time = time
    # if there are no valid moves
    if len(valid_moves) == 0:
    	min_score = 0
    # if you are going to lose...
    elif min_score == eval_max:
        # this is a random move, because moves was shuffled
        best_move = valid_moves[0]
    return -min_score, best_move, min_time + 1
Exemplo n.º 4
0
def eval2(player, board):
    count = 0
    opp = opponent(player)
    for x in range(len(board[0])):
    	y = r4.findMinRow(x, board)
        if not r4.isValid(x, y, board):
            continue
    	r4.setLoc(x, y, player, board)
    	length = r4.findLength(x, y, board)
        if length >= 4:
            count += 1
        elif r4.isValid(x, y+1, board):
            r4.setLoc(x, y+1, opp, board)
            length = r4.findLength(x, y+1, board)
            if length >= 4:
            	count -= 1
            r4.setLoc(x, y+1, 0, board)
        r4.setLoc(x, y, 0, board)
    return count