Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to create an automatic player



  • Hi all,

    I'm dealing with a QML ping pong game:

    0_1550503280315_Capture.PNG

    It now works very well. At the beginning, the ball starts moving automatically from the center of the table towards the right (five times, then the turn changes to the left side this time) and when the right/left paddle hits it, it reflects towards the opposite direction approaching the other paddle.

    If a paddle hits the ball while moving downwards, the ball follows that direction and moves downwards, and the same for upwards as well.

    Now it's partly boring, because the player must play with both paddles, that is, he is his opponent too!

    To make the game more realistic, we need to have one of the paddles play the game automatically, just like any other simple computer games we've ourselves played on a single player mode.

    Now what's your idea for that goal please?
    What would you consider to work well for this task, please?


  • Moderators

    @tomy hi
    great it's working so far now!

    I never made an "Ai"-like PC-Player, so take my advice with the amount of salt needed ;-)

    I would make a function, that is called each time the ball moves.
    In it you generate 2 random values.
    If the first value is inside specific limit, you'll have to try and error them, you proceed in the function call.

    The 2nd value decides how many pixels the racket is moving (y-axis) to be center with the ball.

    That way you can change the odds of movement and the amount moved depending on the difficult level, that you may or may not implement.



  • @J.Hilk
    Hi, danke! :)

    Yeah, the game is very good and working well. I added three sounds to make it more interesting. As well as a number of proper anchors to make it appear nice on the Android devices. Four different levels of speed are also added to give the players nearly full authority to apply their desire speed to the ball, so now it's almost one step away from being a game that people will be interested to play and that is creating an automatic player.

    I would make a function, that is called each time the ball moves.
    In it you generate 2 random values.
    If the first value is inside specific limit, you'll have to try and error them, you proceed in the function call.
    The 2nd value decides how many pixels the racket is moving (y-axis) to be center with the ball.

    Consider we want to make the left paddle the automatic one "autoRacket", and have a function called feedAutoRct (x, y) that will be called by feedAutoRct(ball.xincrement, ball.yincrement), each time the ball is approaching it reflecting off the right paddle or the lower or upper wall after hitting them.
    There, in the function, we create two random numbers using Math.random. They are normally [0,1).

    Assume the first value is 0.4, and the second is 0.7

    Also, we can, using a while loop for instance, anticipate the y point where the ball will reach the x axis of the paddle (because the x axis if const) and set it for the auto paddle.

    I read the method your offered, but couldn't understand it completely. :(
    Would you illustrate it a little more?

    That way you can change the odds of movement and the amount moved depending on the difficult level, that you may or may not implement.

    Incidentally, after implementing this step I want to add a menu to the game to pope up when the app runs and ask the user which level they like to start the game with: easy, normal, or hard! :)


  • Moderators

    @tomy said in How to create an automatic player:

    I read the method your offered, but couldn't understand it completely. :(
    Would you illustrate it a little more?

    I'' try to, maybe a function will say more than an other 100 word :)

    //to be called on ball x changed
    function adjustRacket( ballYPos ){
         if(Math.random() > 0.5){
             //first threshold, we want to actually adjust the autoRacket
            var maxYMovement = 10; //move not more than 10 pixels
            var deltaY = Math.floor(Math.random() * Math.floor(maxYMovement))
            //deltaY the amount of pixels the racket is to move
            autoRacket.y = autoRacket.y + (deltaY * (autoRacket.y - ballYPos < 0 ?  1 : -1))
         }
    }
    


  • @J.Hilk
    Oh, Hilk. That's working as though there's some real person behind the autoRacket! :) Isn't that person you?! ;)
    Thanks so much, it's superb and very astonishing how such a simple code is able to act that greatly! .
    Of course, I made little change to it.

    The app is fairly working well and some more features were added to it, like a menu, and more.

    Two more questions:

    • Since I have an iPhone and like to test it on my phone too, is there any place I can give the code and that gives me the version viable for installing on iOS, please?

    • I've produced the app's APK file using the Armv7 kit and it can be installed on many old or new Android devices, but on some other ones, it says: "App not installed", what could be the problem please?
      I think Armv7 is very common and (except for x86 arch which is, however, rare) is capable of being installed on almost all Android devices.


  • Moderators

    @tomy said in How to create an automatic player:

    @J.Hilk
    Oh, Hilk. That's working as though there's some real person behind the autoRacket! :) Isn't that person you?! ;)

    of course I managed to sneak a route connection to a server into the 5 Lines of codes above ;-) And it's actually me that is controlling the racket

    Thanks so much, it's superb and very astonishing how such a simple code is able to act that greatly! .

    The best code is usually simple

    Of course, I made little change to it.

    Of course the code sniped was basically pseudo code changed had to be made ;-)

    The app is fairly working well and some more features were added to it, like a menu, and more.

    Two more questions:

    • Since I have an iPhone and like to test it on my phone too, is there any place I can give the code and that gives me the version viable for installing on iOS, please?

    As far as I know, there are ways to give your code to companies (as long as it is open source) and they compile it for you. But I don't know much about it, you'll have to google more about it.

    • I've produced the app's APK file using the Armv7 kit and it can be installed on many old or new Android devices, but on some other ones, it says: "App not installed", what could be the problem please?
      I think Armv7 is very common and (except for x86 arch which is, however, rare) is capable of being installed on almost all Android devices.

    It's most likely due to the minimum android version that is required for Qt I think everything below 4.4 will not run Qt applications, at this moment.



  • @J.Hilk
    Hi. Thanks.

    I think AI is but a logic carried out by some short or long code written by a developer. And, the whole thing the code used to distinguish between itself and a non-AI code, is making use of the random() function. That is, that function is the centre of AI code as a tool. There might be other tools as well.
    Why I think so?
    Because it's part of the nature of humans to make mistakes and at times be very superb. That is, a spectrum of success and failure. That is consequently performed by how we use the ramification of that method. If we push up the likelihood of winning, we simply raise the opportunity by the numbers we use to feed the function. On the other hand, it's feasible, as you know, to broaden the rage of hit once again by those numbers to reduce the occurrence of winning.

    Agree up to here?
    And if so, is there any other tool, apart from random(), to be perfectly used in AI?


Log in to reply