def playGameNegamaxMultipleRounds(rounds=100):
    '''Extra credit problem-- 100 games, average win ratio for 'X'. I used
    negamaxIDS with alpha-beta prunning because it's faster (but less effective
    sometimes). Can specify any number of rounds..'''
    
    print("Playing {} games and finding average # of 'X' wins.".format(rounds))
    
    xWins = 0
    value = 0
    
    for round in range(rounds):
	game = TTT(False)
	print(game)
	while not game.isOver():
	    if game.player == 'X':
		value,move = negamaxIDS(game, 9, ab = True)
	    else:
		move = opponentRandom(game.board)
	    if move == None:
		print("move is None. Stopping")
		break
	    game.makeMove(move)
	    print("Player",game.player,"to",move,"for value",value)
	    if not debug: print()
	    print(game)
	    game.changePlayer()
	if game.winner == 'X':
	    xWins = xWins + 1
        
    msg = "'X' won {0} out of {1} games for {2}% wins.".format(xWins, rounds, (xWins/rounds) * 100)    
    print(msg)
Example #2
0
def playGameNegamaxMultipleRounds(rounds=100):
    '''Extra credit problem-- 100 games, average win ratio for 'X'. I used
    negamaxIDS with alpha-beta prunning because it's faster (but less effective
    sometimes). Can specify any number of rounds..'''

    print("Playing {} games and finding average # of 'X' wins.".format(rounds))

    xWins = 0
    value = 0

    for round in range(rounds):
        game = TTT(False)
        print(game)
        while not game.isOver():
            if game.player == 'X':
                value, move = negamaxIDS(game, 9, ab=True)
            else:
                move = opponentRandom(game.board)
            if move == None:
                print("move is None. Stopping")
                break
            game.makeMove(move)
            print("Player", game.player, "to", move, "for value", value)
            if not debug: print()
            print(game)
            game.changePlayer()
        if game.winner == 'X':
            xWins = xWins + 1

    msg = "'X' won {0} out of {1} games for {2}% wins.".format(
        xWins, rounds, (xWins / rounds) * 100)
    print(msg)
def playGameNegamax(game, IDS=False, ab=False):
    '''The original game function given for the assignment with added code for
    displaying the move count once the game is over.'''
    
    print(game)
    value = 0
    turns = 0
    
    while not game.isOver():
	if game.player == 'X':
		turns = turns + 1
	if not IDS and game.player == 'X':
	    value,move = negamax(game,9)
	else:
	    if not ab and game.player == 'X':
		value,move = negamaxIDS(game, 9, ab = False)
	    else:
		if game.player == 'X':
		    value,move = negamaxIDS(game, 9, ab = True)
		else:
		    # This is the 'stupid' opponent
		    move = opponent(game.board)
		
        if move == None :
            print("move is None. Stopping")
            break
        game.makeMove(move)
        print("Player",game.player,"to",move,"for value",value)
        if not debug: print()
        print(game)
        game.changePlayer()

    movesCount = '{} moves explored '.format(game.iMoveCount)
    ebfValue = 'for an ebf of {:.3f}'.format(ebf(game.iMoveCount, turns))
    mebf = 'alpha-beta prunning, ' + movesCount + ebfValue + '.'
    
    print('\nWith ' + mebf if ab else '\nWithout ' + mebf)
    print()
Example #4
0
def playGameNegamax(game, IDS=False, ab=False):
    '''The original game function given for the assignment with added code for
    displaying the move count once the game is over.'''

    print(game)
    value = 0
    turns = 0

    while not game.isOver():
        if game.player == 'X':
            turns = turns + 1
        if not IDS and game.player == 'X':
            value, move = negamax(game, 9)
        else:
            if not ab and game.player == 'X':
                value, move = negamaxIDS(game, 9, ab=False)
            else:
                if game.player == 'X':
                    value, move = negamaxIDS(game, 9, ab=True)
                else:
                    # This is the 'stupid' opponent
                    move = opponent(game.board)

        if move == None:
            print("move is None. Stopping")
            break
        game.makeMove(move)
        print("Player", game.player, "to", move, "for value", value)
        if not debug: print()
        print(game)
        game.changePlayer()

    movesCount = '{} moves explored '.format(game.iMoveCount)
    ebfValue = 'for an ebf of {:.3f}'.format(ebf(game.iMoveCount, turns))
    mebf = 'alpha-beta prunning, ' + movesCount + ebfValue + '.'

    print('\nWith ' + mebf if ab else '\nWithout ' + mebf)
    print()