Esempio n. 1
0
def play_a_game_random(commentary=False):
    board = BG.init_board()  # initialize the board
    player = np.random.randint(2) * 2 - 1  # which player begins?
    randomPlayer = -1
    while not BG.game_over(board) and not BG.check_for_error(board):
        if commentary: print("lets go player ", player)

        # roll dice
        dice = BG.roll_dice()
        if commentary: print("rolled dices:", dice)

        # make a move (2 moves if the same number appears on the dice)
        for i in range(1 + int(dice[0] == dice[1])):
            board_copy = np.copy(board)

            if player == randomPlayer:
                move = flipped_agent.action(board_copy, dice, player, i)
            else:
                move = action(board_copy, dice, player, i)

            # update the board
            if len(move) != 0:
                for m in move:
                    board = BG.update_board(board, m, player)

            # give status after every move:
            if commentary:
                print("move from player", player, ":")
                BG.pretty_print(board)

        # players take turns
        player = -player

    # return the winner
    return -1 * player
    def do(self, board_real, dice, actor_theta, player):
        commentary = False
        print_results = False
        for i in range(0, 25):
            board = np.copy(board_real)
            old_state = np.copy(board_real)
            self.z = np.zeros(198)
            if(len(board) == 0):
                break
            count = 0
            while not Backgammon.game_over(board) and not Backgammon.check_for_error(board):
                if commentary:
                    print("Simulationgame: lets go player ", player)

                dice = Backgammon.roll_dice()
                if commentary:
                    print("Simulationgame: rolled dices:", dice)

                # make a move (2 moves if the same number appears on the dice)
                for i in range(1 + int(dice[0] == dice[1])):
                    board_copy = np.copy(board)
                    if player == 1:
                        
                        move, new_state = self.nextMove(board_copy, dice, player, actor_theta)
                        
                        
                    elif player == -1:
                        move = agentX.action(board_copy, dice, player, i)
                    if len(move) != 0:

                        for m in move:
                            board = Backgammon.update_board(board, m, player)
                        if(player == 1 and count > 1):
                            new_state = np.copy(board)  
                            if(not Backgammon.game_over(new_state) and not Backgammon.check_for_error(new_state)):


                                delta = 0 + self.getValue(new_state, actor_theta, player) - self.getValue(old_state, actor_theta, player)
                                self.theta = self.theta + (self.alpha * delta * self.z)
                                self.z = self.lamb * self.z + getFeatures(old_state, player)
                                old_state = new_state

                    if commentary:
                        print("Simulationgame: move from player", player, ":")
                        Backgammon.pretty_print(board)
                player = -player
                count = count + 1   
            if(print_results):
                print("simulation game nr", i)
                Backgammon.pretty_print(board)
            delta = player * -1 + 0 - self.getValue(old_state, actor_theta, player)
            
            self.theta = np.add(self.theta , (self.alpha * delta * self.z))
            self.z = self.lamb * self.z + getFeatures(old_state, player)
Esempio n. 3
0
 def render(self):
     B.pretty_print(self.board)