Ejemplo n.º 1
0
    def minvalue(self, depth, state, alpha, beta):
        self.depthLvl += 1
        isDone = CheckerBoard.is_terminal(state)

        if isDone[0]:
            v = self.strategy.utility(state)  #if terminal state

        elif self.depthLvl >= depth:  #if cutoff depth hit
            v = float('inf')
            actions = state.get_actions(self.maxP)
            for a in actions:  #choose move at depth
                newboard = state.move(a)
                w = self.strategy.utility(newboard)
                v = min(v, w)

        else:  #if neither, then keep searching
            v = float("inf")
            actions = state.get_actions(self.minP)
            for a in actions:
                newboard = state.move(a)
                v = min(v, self.maxvalue(depth, newboard, alpha, beta))
                if v <= alpha:
                    break
                else:
                    beta = max(alpha, v)
        return v
Ejemplo n.º 2
0
def Game(red=ai.Strategy, black=tonto.Strategy, 
         maxplies=5, init=None, verbose=True, firstmove=0):
    """Game(red, black, maxplies, init, verbose, turn)
    Start a game of checkers
    red,black - Strategy classes (not instances)
    maxplies - # of turns to explore (default 10)
    init - Start with given board (default None uses a brand new game)
    verbose - Show messages (default True)
    firstmove - Player N starts 0 (red) or 1 (black).  Default 0. 
    """

    # Don't forget to create instances of your strategy,
    # e.g. black('b', checkerboard.CheckerBoard, maxplies)

    ### Still unsure what firstmove is supposed to do ###

    # Initialize board
    if init is None:
        board = CheckerBoard()  # New game
    else:
        board = init    # Initialize board to given config

    # Display Board
    if verbose:
        print('Initial Board:')
    print(board, '\n')

    # Initialize Players
    red = red('r', board, maxplies)
    black = black('b', board, maxplies)

    # Start playing
    while not board.is_terminal()[0]:   # Nobody has won yet

        state = red.play(board) # Get tuple of new board following an action and the action itself
        board = state[0]
        action = state[1]
        if verbose and action is not None:
            print('Red Move: ', board.get_action_str(action))
        print(board)

        # Player has forfeited -> exit loop
        if action is None:
            print('Red Player has forfeited the game')
            break

        if not board.is_terminal()[0]:  # Still nobody has won yet
            state = black.play(board)   # Get tuple of new board following an action and the action itself
            board = state[0]
            action = state[1]
            if verbose and action is not None:
                print('Black Move: ', board.get_action_str(action))
        print(board)

        # Player has forfeited -> exit loop
        if action is None:
            print('Black Player has forfeited the game')
            break

    if (board.is_terminal()[1] == 'r'):
        print('Red Player wins')
    elif (board.is_terminal()[1] == 'b'):
        print('Black Player wins')
    else:
        print('Game has ended in a stalemate')