8 Replies - 246 Views - Last Post: 18 February 2019 - 04:32 PM Rate Topic: -----

#1 franky12   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 108
  • Joined: 09-June 15

How to create an automatic player using AI in C++

Posted 18 February 2019 - 09:50 AM

Hi all,

I'm dealing with a QML ping pong game.

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? Since QML can use C++ code, can I use AI in C++ to do the job? If so, how to start learning it in C++ please?

Is This A Good Question/Topic? 0
  • +

Replies To: How to create an automatic player using AI in C++

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14867
  • View blog
  • Posts: 59,320
  • Joined: 12-June 08

Re: How to create an automatic player using AI in C++

Posted 18 February 2019 - 10:08 AM

I wouldn't necessarily call it 'ai', but start by having a process that moves a paddle up and down. Then morph that into taking a ball's position, compare to the previous position, and estimate where to move the paddle.
Was This Post Helpful? 1
  • +
  • -

#3 Salem_c   User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2312
  • View blog
  • Posts: 4,419
  • Joined: 30-May 10

Re: How to create an automatic player using AI in C++

Posted 18 February 2019 - 10:17 AM

Cross-posted here -> http://www.cplusplus...general/250004/
Was This Post Helpful? 2
  • +
  • -

#4 franky12   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 108
  • Joined: 09-June 15

Re: How to create an automatic player using AI in C++

Posted 18 February 2019 - 10:21 AM

Thanks for your answer. Moving the paddle is easy, but I didn't understand the method you described, very well.
Would you explain it more so that I can figure it out well?
Was This Post Helpful? 0
  • +
  • -

#5 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: How to create an automatic player using AI in C++

Posted 18 February 2019 - 10:25 AM

It seems to me that if you have to ask how to create an AI you're probably not @ that level yet. Are you trying to create a bot or a genuine AI?

If you just want to create a pong opponent then why not have the bot check for whether the ball is + or - it's position and then just use loops to increase paddle position int or double? Have a timer running and then:

if (ballPosY > paddlePosY)
{
  paddlePosY++;
}
else 
{
  paddlePosY--;
}




If you want genuine machine learning then you're probably going to need some API:

https://dzone.com/ar...s-and-libraries

I don't know much about AI but I assume you would need a database to store the "learned" information and then algorithms to analyze probabilities of certain outcomes and adjust behavior accordingly. I'm not sure that a simple pong game would be complex enough to even benefit from a true AI. and keep in mind that typically people who work in the field of AI are absolute experts at data analytics/low level programming so if you're not those then it may not be time efficient to undertake such a project on your own.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14867
  • View blog
  • Posts: 59,320
  • Joined: 12-June 08

Re: How to create an automatic player using AI in C++

Posted 18 February 2019 - 10:35 AM

This isn't a giant undertaking, or advanced AI.. just feed the ball's location into a function for your 'computer player' class.. Have it compare any previous location to the current and then move the paddle, incrementally, that way. Save the location and wait for more input.

Plenty of 'pong computer opponent' tutorials out there.
Was This Post Helpful? 0
  • +
  • -

#7 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: How to create an automatic player using AI in C++

Posted 18 February 2019 - 10:47 AM

modi123_1 yeah that's what I was thinking. Definitely no need for a real AI in this instance
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6777
  • View blog
  • Posts: 23,085
  • Joined: 05-May 12

Re: How to create an automatic player using AI in C++

Posted 18 February 2019 - 12:51 PM

Just like one of the ghosts in PacMan. As I recall one of the ghosts was a straight on seeker, while the other two ghosts had extra logic applied to them. Then when PacMan eats the pill, they all switched their logic to try to move as away from PacMan as possible -- reverse seekers.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6777
  • View blog
  • Posts: 23,085
  • Joined: 05-May 12

Re: How to create an automatic player using AI in C++

Posted 18 February 2019 - 04:32 PM

And I forgot that the last ghost was completely random: Pac-Man Patterns Ghost Movement (Strategy Pattern)

Anyway, back on topic. For your paddle, you just need to implement a vanilla seeker. If your game supports adding "English" to the ball, then that's the time that you'll need to write code that does extra smarts to make sure that the paddle is in motion when the ball hits the paddle.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1