# How to make AI dumber

• 07-23-2011, 02:21 AM
Reskaillev
How to make AI dumber
Well I'm programming a Pong clone and I'm wondering how to make the enemy paddle "dumber" or "more human" if you prefer it that way :o:

Fairly simple code:

Code:

```public void AI()         {                 if (middlepaddle-ycoordinateball<-5)                 {                         y2=y2+5;                 }                 else if (middlepaddle-ycoordinateball>5)                 {                         y2=y2-5;                 }         }```
The Ai checks if the middlepoint of the ball (y-coordinate) is higher or lower then the middle of the paddle. (with a fault of 5) and in response adjusts its position with 5 pixels.

At this moment the AI is unbeatable.

I have tried changing the pixels it adjusts itself to lower values. But that makes the aI not convincing, it makes it beatable though but I want a convincing aI.

Reskaillev
• 07-23-2011, 09:31 AM
JosAH
Maybe you can add some randomness to the y2 coordinate? e.g.

Code:

```public void AI()         {                 if (middlepaddle-ycoordinateball<-5)                 {                         y2=y2+r.nextInt(15);                 }                 else if (middlepaddle-ycoordinateball>5)                 {                         y2=y2-r.nextInt(15);                 }         }```
... where variable r is an instance of the Random class; you can play a bit with how big the random numbers should be to avoid turning your AI into a complete idiot ;-)

kind regards,

Jos
• 07-23-2011, 03:46 PM
Dark
Also you could check to see if the ball is on the AI's side of the court. If it is, then track it towards the ball. If its not, you could invoke a random movement on its side. Kind of like how players some times move their paddle while their waiting for the ball to come back.
• 07-24-2011, 03:11 AM
Reskaillev
Thnx a lot for those answers!

It's much appreciated
• 07-24-2011, 03:26 AM
Fubarable
No, no, no. I'm sorry, but I think that you're missing the boat. The key to making your AI dumber is to create and call the proper methods. For example:

Code:

```MyAI ai = new MyAI(); ai.setName("Jos"); while (true) {   ai.consumeGrolsch(); }```
This never fails for my apps, and will work for yours, guaranteed!
• 07-24-2011, 04:36 AM
pbrockway2
No! As written ai will never get to be used because of the infinite loop.

You shouldn't be calling consumeGrolsch() on the EDT anyway, the results are never pretty. And you MUST read the docs and catch the exceptions that this method is declared to throw. It is sufficient to respond to IntoxificationException by sending some random string to System.err. But you cannot suppress the OutOfGrolschException: use BeverageUtilities.replenishStock(), that's what it is there for.

There is no need to be overcareful about synchronising variables in MyAI that might be accessed by consumeGrolsch() from other threads as it is well known that after a few method calls the "synchronised" keyword no longer has any effect. Your code should note that the method is not thread safe, but that you don't really care.
• 07-24-2011, 08:02 AM
JosAH
I feel so flattered ...

kindest regards,

Jos ;-)
• 07-24-2011, 10:13 PM
Reskaillev
Ok thnx guys, I made it dumber using the methods dark and jos gave me.

The consumeGrolsch method was too smart for this....:o