In my pong game, I want to have three different difficulties easy, medium, and hard. There is one pong that will be controlled by code. My question is, is there more to AI then just a bunch of if statements, or is there some structure that is needed? Also, how could a make the hard mode AI, different from the Easy mode? I already know that I will set them all to the same speed.
AI is more than just a bunch of if statements, but you need to roll back a bit.
Ask yourself,
- “What does it means for a game to display (artificial) intelligence?”
- “What kinds of actions does the game take?”
- “What constitutes an intelligent action?”
So, “What does it means for a PONG GAME to display (artificial) intelligence?”
- Defensive AI - The game tries to block the ball.
- A (Simple algorithm): Try to keep paddle in same y position as ball
- B (Better algorithm): Calculate the path of the ball and move the paddle to the point of intersection.
- Offensive AI - The game tries to make you miss the ball. (This is harder to program).
- Assuming the paddle movement can affect the path of the ball and it isn’t just a ‘frictionless bounce’, move the paddle in order to affect the path of the ball as it travels towards the player.
- Other… but again this is actually pretty deep and hard.
If we think just about ‘defensive AI’ then we have at least two choices (A and B above). In both cases we can make the AI better or worse simply by limiting it in some way:
- A/B - Limit velocity of paddle. Slower is easier. Faster is harder.
- A/B - Instead of using real-time AI (every frame), limit thinking to once every 500ms, 200ms, 100ms… faster is a ‘smarter’ AI
- B - Limit the accuracy of the intersection calculation or purposely introduce a small amount of error. More error equals dumber AI.
Thanks, these are some pretty interesting ideas!
Also, I am probably going to change how the paddles affect the linearVelocity of the ball. For example, if the paddle is moving up, the ball will be bounced back upward.
You could keep things real simple and make your easy, medium and hard difficulties speed dependent. Your logic stays the same you just adjust the relative game play speed. This could be the balls velocity or the players bat movement speed or both.
Actually, I already have code that makes the player move faster or slower depending on the game mode they selected.
This discussion came out of @sdktester15 wanting to make a pong clone as a learning experience: ( You rock! )
For future readers, I think it is important to know there were other posts in this topic/series that have related discussions.
Here they are:
- https://forums.coronalabs.com/topic/67870-ai-question/ (the starting point for these posts)
- https://forums.coronalabs.com/topic/67884-how-to-calculate-the-path-of-an-object/
- https://forums.coronalabs.com/topic/67871-losing-velocity-when-kinematic-object-and-dynamic-object-collides/#entry351260
- https://forums.coronalabs.com/topic/67880-having-trouble-changing-linearvelocities/#entry351301
- https://forums.coronalabs.com/topic/67900-ssk2-fundamentals-series/
Two other quick and easy AI tricks are to analyze the players habits and to add in plenty of random variations. If the player aims the ball at the top 65% of the time, have your AI hedge toward the top 65%. Add some randomness to the hedging in case the player catches on. Maybe instead of hedging toward the top 65% of the time you randomize it between 45% and 85%.
You will use a lot of “if” statements but add randomness to everything. It makes the AI less predictable and it will be perceived as smarter.
*Use prime numbers in your random calculations. Players will find it more difficult to detect patterns if you use primes.
[lua]
local num = 7
local chance = math.random(num)
if (chance == num) then
– do something unexpected
end
[/lua]
AI is more than just a bunch of if statements, but you need to roll back a bit.
Ask yourself,
- “What does it means for a game to display (artificial) intelligence?”
- “What kinds of actions does the game take?”
- “What constitutes an intelligent action?”
So, “What does it means for a PONG GAME to display (artificial) intelligence?”
- Defensive AI - The game tries to block the ball.
- A (Simple algorithm): Try to keep paddle in same y position as ball
- B (Better algorithm): Calculate the path of the ball and move the paddle to the point of intersection.
- Offensive AI - The game tries to make you miss the ball. (This is harder to program).
- Assuming the paddle movement can affect the path of the ball and it isn’t just a ‘frictionless bounce’, move the paddle in order to affect the path of the ball as it travels towards the player.
- Other… but again this is actually pretty deep and hard.
If we think just about ‘defensive AI’ then we have at least two choices (A and B above). In both cases we can make the AI better or worse simply by limiting it in some way:
- A/B - Limit velocity of paddle. Slower is easier. Faster is harder.
- A/B - Instead of using real-time AI (every frame), limit thinking to once every 500ms, 200ms, 100ms… faster is a ‘smarter’ AI
- B - Limit the accuracy of the intersection calculation or purposely introduce a small amount of error. More error equals dumber AI.
Thanks, these are some pretty interesting ideas!
Also, I am probably going to change how the paddles affect the linearVelocity of the ball. For example, if the paddle is moving up, the ball will be bounced back upward.
You could keep things real simple and make your easy, medium and hard difficulties speed dependent. Your logic stays the same you just adjust the relative game play speed. This could be the balls velocity or the players bat movement speed or both.
Actually, I already have code that makes the player move faster or slower depending on the game mode they selected.
This discussion came out of @sdktester15 wanting to make a pong clone as a learning experience: ( You rock! )
For future readers, I think it is important to know there were other posts in this topic/series that have related discussions.
Here they are:
- https://forums.coronalabs.com/topic/67870-ai-question/ (the starting point for these posts)
- https://forums.coronalabs.com/topic/67884-how-to-calculate-the-path-of-an-object/
- https://forums.coronalabs.com/topic/67871-losing-velocity-when-kinematic-object-and-dynamic-object-collides/#entry351260
- https://forums.coronalabs.com/topic/67880-having-trouble-changing-linearvelocities/#entry351301
- https://forums.coronalabs.com/topic/67900-ssk2-fundamentals-series/
Two other quick and easy AI tricks are to analyze the players habits and to add in plenty of random variations. If the player aims the ball at the top 65% of the time, have your AI hedge toward the top 65%. Add some randomness to the hedging in case the player catches on. Maybe instead of hedging toward the top 65% of the time you randomize it between 45% and 85%.
You will use a lot of “if” statements but add randomness to everything. It makes the AI less predictable and it will be perceived as smarter.
*Use prime numbers in your random calculations. Players will find it more difficult to detect patterns if you use primes.
[lua]
local num = 7
local chance = math.random(num)
if (chance == num) then
– do something unexpected
end
[/lua]