Example #1
0
def minimax(board, player, depth, n):
	moves = get_moves(board, n)

	if not moves:
		return None

	best_move = moves[0]
	best_score = 0

	for move in moves:
		clone_board=gen_board(board,opp[player],move)
		#print clone_board
		if win(clone_board,opp[player],n):
			return move

	for move in moves:
		clone_board=gen_board(board,player,move)
                if win(clone_board,player,n):
                        return move

		score = min_play(clone_board,opp[player],depth,n)

		if best_score < score:
			best_score = score
			best_move = move

	return best_move
Example #2
0
def run_ai(game_param):
    stop = False
    if not main.win(game_param):
        while not stop:
            main.show_map(game_param)
            variables = get_variables(game_param)
            matrix = get_matrix_A_B(game_param, variables)
            numbers = roundNumbers(pinv(matrix['A'], matrix['B']))
            cells_actions = get_cells_actions(game_param, variables, numbers)
            show_matrix(variables, matrix, numbers)
            for cell in cells_actions:
                main.apply_user_action(game_param, cell[0], cell[1])
                if main.win(game_param) or main.lose(game_param, cell[1]):
                    stop = True
                    break
    print("End game !")
Example #3
0
def max_play(board, player, depth, n):
        moves = get_moves(board,n)

        if not moves or depth==0:       evaluate(board,player,n)

        best_score = float('-inf')

        for move in moves:
		clone_board = gen_board(board,player,move)

		if win(clone_board,player,n):
                        return evaluate(clone_board,player,n)

                score = max_play(clone_board,opp[player],depth-1, n)

                if score > best_score:
                        best_score = score

        return best_score
Example #4
0
        holeCards = ''.join(otherCards[1:])
        holeCards = re.findall('..', holeCards)

        if position == 1:
            tmp = ourCards
            ourCards = otherDudesCards
            otherDudesCards = tmp

        #print 'ourCards', ourCards
        #print 'otherDudesCards', otherDudesCards

        finished = False
        if (len(ourCards) == len(otherDudesCards)) or (len(bets[-1]) > 0 and bets[-1][-1] == 'f'):
            if len(ourCards) == len(otherDudesCards):
                winner = main.win([ai2num(c) for c in ourCards],
                                  [ai2num(c) for c in otherDudesCards],
                                  [ai2num(c) for c in holeCards])

                if winner == True:
                    outcome = 0.0
                elif winner == False:
                    outcome = 1.0
                else:
                    outcome = 0.5

            elif lastBet(bets) == position and bets[-1][-1] == 'f':
                outcome = 1.0
            elif lastBet(bets) == 1 - position and bets[-1][-1] == 'f':
                outcome = 0.0

            for pbets in getPreviousLastBetStates(bets, position):
Example #5
0
def make_play(player, action):
    bets = flask.session['bets'].split('/')[-1]

    raises = 0

    if len(bets) > 0:
        for b in bets:
            if b == 'r':
                raises += 1

        raises -= 1
    
    goesfirst = (player == 1 and flask.session['round'] == 0) or (player == 0 and flask.session['round'] > 0)

    #print raises, bets

    if (len(bets) == 0 and goesfirst):
        choices = ['c', 'r', 'f']
    elif (not goesfirst and len(bets) == 1 and bets[0] == 'c'):
        choices = ['c', 'r']
    elif raises >= 3:
        choices = ['c', 'f']
    else:
        choices = ['c', 'r', 'f']

    if flask.session['winner'] == -1 and action in choices:
        #print "{0} does {1}".format(player, action)
        flask.session['bets'] += action

        playerString = 'player' if (flask.session['player'] == player) else 'computer'

        if action == 'r':
            if flask.session['round'] == 0 and len(bets) == 0:
                #print 'f'
                flask.session['wallet'][playerString] -= 6
                flask.session['pool'][playerString] += 6
            elif len(bets) == 0 or bets == 'c':
                #print 'a'
                flask.session['wallet'][playerString] -= 4
                flask.session['pool'][playerString] += 4
            else:
                #print 'b'
                flask.session['wallet'][playerString] -= 8
                flask.session['pool'][playerString] += 8
        elif action == 'c':
            if flask.session['round'] == 0 and len(bets) == 0:
                #print 'i'
                flask.session['wallet'][playerString] -= 2
                flask.session['pool'][playerString] += 2
            elif len(bets) == 0:
                #print 'g'
                pass
            else:
                #print 'd'
                flask.session['wallet'][playerString] -= 4
                flask.session['pool'][playerString] += 4
        elif action == 'f':
            #print 'e'
            flask.session['winner'] = 1 - player

            winningPlayerString = 'computer' if playerString == 'player' else 'player'

            flask.session['wallet'][winningPlayerString] += sum(flask.session['pool'].values())
            flask.session['pool'] = { 'player' : 0,
                                      'computer' : 0 }

            return 'next'

    #print 'here we are', bets, flask.session['bets'].split('/')[-1], action, flask.session['round'], choices, flask.session['winner']

    if len(flask.session['bets'].split('/')[-1]) > 1 and action == 'c':
        if flask.session['round'] == 0 and flask.session['winner'] == -1:
            flask.session['cards'].append(flask.session['deck'].pop())
            flask.session['cards'].append(flask.session['deck'].pop())
            flask.session['cards'].append(flask.session['deck'].pop())
        elif flask.session['round'] == 1 and flask.session['winner'] == -1:
            flask.session['cards'].append(flask.session['deck'].pop())
        elif flask.session['round'] == 2 and flask.session['winner'] == -1:
            flask.session['cards'].append(flask.session['deck'].pop())
        elif flask.session['round'] >= 3:
            retVal = main.win([ai2num(card) for card in flask.session['hcards']],
                              [ai2num(card) for card in flask.session['ccards']],
                              [ai2num(card) for card in flask.session['cards']])

            if retVal == 0:
                retVal = 1 - flask.session['player']
            elif retVal == 1:
                retVal = flask.session['player']

            if flask.session['winner'] != 234134:
                if retVal != 2:
                    #print retVal
                    if flask.session['player'] == retVal:
                        winnerString = 'player'
                    else:
                        winnerString = 'computer'
                
                    #print flask.session['player'], 'won', winnerString

                    flask.session['wallet'][winnerString] += sum(flask.session['pool'].values())
                    flask.session['pool'] = { 'player' : 0,
                                              'computer' : 0 }
                else:
                    for player, value in flask.session['pool'].items():
                        flask.session['wallet'][player] += value
                        flask.session['pool'] = { 'player' : 0, 'computer' : 0 }

            flask.session['winner'] = flask.session['player'] if (flask.session['player'] == retVal) else 1 - flask.session['player']

        if flask.session['winner'] == -1:
            flask.session['round'] += 1
            flask.session['bets'] += '/'

        flask.session['percent'] = main.estimate([ai2num(c) for c in flask.session['hcards']], [ai2num(c) for c in flask.session['deck'] + flask.session['ccards']], [ai2num(c) for c in flask.session['cards']])

        return 'next'

    return None