<reponame>Liuian/1092_INTRODUCTION-TO-MACHINE-LEARNING-AND-ITS-APPLICATION-TO-GAMING
"""
The template of the script for the machine learning process in game pingpong
"""


class MLPlay:
    import random
    def __init__(self, side):
        """
        Constructor

        @param side A string "1P" or "2P" indicates that the `MLPlay` is used by
               which side.
        """
        self.ball_served = False    #self.ball_served記錄球被發出去了沒
        self.side = side
        self.ball_x = 0
        self.prev_ball_x = 0
        self.ball_y = 0
        self.prev_ball_y = 0
        self.des_x = 100             #75 is initial place

    def update(self, scene_info):
        """
        Generate the command according to the received scene information
        """
        #if game pass pr game over -> reset
        if scene_info["status"] != "GAME_ALIVE":
            print(scene_info["ball_speed"])
            return "RESET"

        #if self.ball_served is False, serve the ball
        if not self.ball_served:    
            if scene_info["frame"] == 149:
                self.ball_served = True
                rand = self.random.randint(0, 1)
                if rand == 0:
                    #print("left")
                    return "SERVE_TO_LEFT"
                    #???command = "SERVE_TO_RIGHT"
                else:
                    return "SERVE_TO_RIGHT"
                    #print("right")
                    #???command = "SERVE_TO_RIGHT"

        #if self.ball_serve is true, move the plate
        else:                       
            #update the parmeters
            self.prev_ball_x = self.ball_x
            self.prev_ball_y = self.ball_y
            self.ball_x = scene_info["ball"][0]
            self.ball_y = scene_info["ball"][1]
            velo_ball_x = self.ball_x - self.prev_ball_x
            velo_ball_y = self.ball_y - self.prev_ball_y

            if self.side == "1P":
                #judge the ball's x destination
                if velo_ball_y > 0:                                             #ball go down
                    if velo_ball_x < 0:                                         #ball move left
                        self.des_x = self.ball_x - (420 - self.ball_y)
                    else:                                                       #ball move right
                        self.des_x = self.ball_x + (420 - self.ball_y)
                else:                                                           #ball move up  
                    if velo_ball_x < 0:
                        self.des_x = self.ball_x - (680 - (420 - self.ball_y))
                    else:
                        self.des_x = self.ball_x + (680 - (420 - self.ball_y))
                #adjest the destination into the scene
                while self.des_x > 200 or self.des_x < 0:
                    if self.des_x > 200:
                        self.des_x = (200 - (self.des_x - 200))
                    else:
                        self.des_x = -self.des_x
                #judge the cammand
                #print(scene_info["platform_1P"][0], self.des_x)
                if scene_info["frame"] <= 150:                                  
                    return "NONE"
                elif (scene_info["platform_1P"][1] > 410 and scene_info["platform_1P"][0] + 20 + 10 > self.des_x and scene_info["platform_1P"][0] + 20 - 10 < self.des_x):
                    return "NONE"                                               #when plate in the des_x plate don't move 
                elif scene_info["platform_1P"][0] + 20 < self.des_x:            #plate at ball's right, plate move left
                    return "MOVE_RIGHT"
                elif scene_info["platform_1P"][0] + 20 > self.des_x:
                    return "MOVE_LEFT"
                else:
                    return "NONE"
                #return command
            
            else:                                                               #if self.side == 2P
                #judge the ball's x destination
                if velo_ball_y < 0:                                             #ball move up
                    if velo_ball_x < 0:                                         #ball move left
                        self.des_x = self.ball_x - (self.ball_y - 80)
                    else:                                                       #ball move right   
                        self.des_x = self.ball_x + (self.ball_y - 80)
                else:                                                           #ball move down  
                    if velo_ball_x < 0:                                         #ball move left
                        self.des_x = self.ball_x - (680 - (self.ball_y - 80))
                    else:                                                       #ball move right
                        self.des_x = self.ball_x + (680 - (self.ball_y - 80))
                #adjest the destination into the scene
                while self.des_x > 200 or self.des_x < 0:
                    if self.des_x > 200:
                        self.des_x = (200 - (self.des_x - 200))
                    else:
                        self.des_x = -self.des_x
                #judge the cammand
                if scene_info["frame"] <= 150:
                    return "NONE"
                elif (scene_info["platform_2P"][1] < 60 and scene_info["platform_2P"][0] + 20 + 10 > self.des_x and scene_info["platform_2P"][0] + 20 - 10 < self.des_x):
                #elif scene_info["platform_1P"][1] <
                    return "NONE"                                               #when plate in the des_x plate don't move
                elif scene_info["platform_2P"][0] + 20 < self.des_x:            #plate at ball's right, plate move left
                    return "MOVE_RIGHT"
                elif scene_info["platform_2P"][0] + 20 > self.des_x:
                    return "MOVE_LEFT"
                else:
                    return "NONE"
                #return command
            
    def reset(self):
        """
        Reset the status
        """
        self.ball_served = False

"""
The template of the script for the machine learning process in game pingpong
"""


class MLPlay:
    import random
    def __init__(self, side):
        """
        Constructor

        @param side A string "1P" or "2P" indicates that the `MLPlay` is used by
               which side.
        """
        self.ball_served = False    #self.ball_served記錄球被發出去了沒
        self.side = side
        self.ball_x = 0
        self.prev_ball_x = 0
        self.ball_y = 0
        self.prev_ball_y = 0
        self.des_x = 100             #75 is initial place

    def update(self, scene_info):
        """
        Generate the command according to the received scene information
        """
        #if game pass pr game over -> reset
        if scene_info["status"] != "GAME_ALIVE":
            print(scene_info["ball_speed"])
            return "RESET"

        #if self.ball_served is False, serve the ball
        if not self.ball_served:    
            if scene_info["frame"] == 149:
                self.ball_served = True
                rand = self.random.randint(0, 1)
                if rand == 0:
                    #print("left")
                    return "SERVE_TO_LEFT"
                    #???command = "SERVE_TO_RIGHT"
                else:
                    return "SERVE_TO_RIGHT"
                    #print("right")
                    #???command = "SERVE_TO_RIGHT"

        #if self.ball_serve is true, move the plate
        else:                       
            #update the parmeters
            self.prev_ball_x = self.ball_x
            self.prev_ball_y = self.ball_y
            self.ball_x = scene_info["ball"][0]
            self.ball_y = scene_info["ball"][1]
            velo_ball_x = self.ball_x - self.prev_ball_x
            velo_ball_y = self.ball_y - self.prev_ball_y

            if self.side == "1P":
                #judge the ball's x destination
                if velo_ball_y > 0:                                             #ball go down
                    if velo_ball_x < 0:                                         #ball move left
                        self.des_x = self.ball_x - (420 - self.ball_y)
                    else:                                                       #ball move right
                        self.des_x = self.ball_x + (420 - self.ball_y)
                else:                                                           #ball move up  
                    if velo_ball_x < 0:
                        self.des_x = self.ball_x - (680 - (420 - self.ball_y))
                    else:
                        self.des_x = self.ball_x + (680 - (420 - self.ball_y))
                #adjest the destination into the scene
                while self.des_x > 200 or self.des_x < 0:
                    if self.des_x > 200:
                        self.des_x = (200 - (self.des_x - 200))
                    else:
                        self.des_x = -self.des_x
                #judge the cammand
                #print(scene_info["platform_1P"][0], self.des_x)
                if scene_info["frame"] <= 150:                                  
                    return "NONE"
                elif (scene_info["platform_1P"][1] > 410 and scene_info["platform_1P"][0] + 20 + 10 > self.des_x and scene_info["platform_1P"][0] + 20 - 10 < self.des_x):
                    return "NONE"                                               #when plate in the des_x plate don't move 
                elif scene_info["platform_1P"][0] + 20 < self.des_x:            #plate at ball's right, plate move left
                    return "MOVE_RIGHT"
                elif scene_info["platform_1P"][0] + 20 > self.des_x:
                    return "MOVE_LEFT"
                else:
                    return "NONE"
                #return command
            
            else:                                                               #if self.side == 2P
                #judge the ball's x destination
                if velo_ball_y < 0:                                             #ball move up
                    if velo_ball_x < 0:                                         #ball move left
                        self.des_x = self.ball_x - (self.ball_y - 80)
                    else:                                                       #ball move right   
                        self.des_x = self.ball_x + (self.ball_y - 80)
                else:                                                           #ball move down  
                    if velo_ball_x < 0:                                         #ball move left
                        self.des_x = self.ball_x - (680 - (self.ball_y - 80))
                    else:                                                       #ball move right
                        self.des_x = self.ball_x + (680 - (self.ball_y - 80))
                #adjest the destination into the scene
                while self.des_x > 200 or self.des_x < 0:
                    if self.des_x > 200:
                        self.des_x = (200 - (self.des_x - 200))
                    else:
                        self.des_x = -self.des_x
                #judge the cammand
                if scene_info["frame"] <= 150:
                    return "NONE"
                elif (scene_info["platform_2P"][1] < 60 and scene_info["platform_2P"][0] + 20 + 10 > self.des_x and scene_info["platform_2P"][0] + 20 - 10 < self.des_x):
                #elif scene_info["platform_1P"][1] <
                    return "NONE"                                               #when plate in the des_x plate don't move
                elif scene_info["platform_2P"][0] + 20 < self.des_x:            #plate at ball's right, plate move left
                    return "MOVE_RIGHT"
                elif scene_info["platform_2P"][0] + 20 > self.des_x:
                    return "MOVE_LEFT"
                else:
                    return "NONE"
                #return command
            
    def reset(self):
        """
        Reset the status
        """
        self.ball_served = False
The template of the script for the machine learning process in game pingpong Constructor          @param side A string "1P" or "2P" indicates that the `MLPlay` is used by                which side. #self.ball_served記錄球被發出去了沒 #75 is initial place Generate the command according to the received scene information #if game pass pr game over -> reset #if self.ball_served is False, serve the ball #print("left") #???command = "SERVE_TO_RIGHT" #print("right") #???command = "SERVE_TO_RIGHT" #if self.ball_serve is true, move the plate #update the parmeters #judge the ball's x destination #ball go down #ball move left #ball move right #ball move up #adjest the destination into the scene #judge the cammand #print(scene_info["platform_1P"][0], self.des_x) #when plate in the des_x plate don't move #plate at ball's right, plate move left #return command #if self.side == 2P #judge the ball's x destination #ball move up #ball move left #ball move right #ball move down #ball move left #ball move right #adjest the destination into the scene #judge the cammand #elif scene_info["platform_1P"][1] < #when plate in the des_x plate don't move #plate at ball's right, plate move left #return command Reset the status