YakYak

Llamasoft baanter and moosings - It's like Deliverance with Sheep
It is currently Fri Jul 30, 2010 11:59 am 0

All times are UTC




Post new topic Reply to topic  [ 42 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Pong AI
PostPosted: Thu Jul 23, 2009 8:30 pm 0 
Offline
Sheepie Whore
Sheepie Whore
User avatar

Joined: Wed Apr 24, 2002 9:44 am 0
Posts: 20926
Location: Not here
I'm making a little Pong clone to test out some code on my DS. I can't quite work out a decent looking behaviour for the computer bat. Right now I just make the computer's bat track the ball's y position, so it never misses, and you can't ever win.

Since this is Pong, and it's probably the simplest game going, what's a nice simple routine for making the computer bat 'good enough' but sometimes miss? I had a go and ended up lost in a mess of 'if' statements trying to test for all sorts of things, which seemed too complex and messy.

_________________
exit(1);


Top
 Profile  
 
 Post subject: Re: Pong AI
PostPosted: Thu Jul 23, 2009 8:36 pm 0 
Offline
Baaaaaa!
Baaaaaa!

Joined: Sat Jun 24, 2006 8:15 pm 0
Posts: 2442
program in a deliberate miss every few turns (track -20 pixels or whatever)
miss interval could be a random number
miss interval gets shorter to ramp the difficulty up


Top
 Profile  
 
 Post subject: Re: Pong AI
PostPosted: Thu Jul 23, 2009 9:03 pm 0 
Offline
Baaaaa!
Baaaaa!
User avatar

Joined: Wed Dec 24, 2003 10:22 pm 0
Posts: 1188
Location: Hull on Earth
Here's how I'd do it:

Have the AI only "think" every n frames, so it doesn't have perfect reactions.

Each AI "think" tick, predict where the ball will be when it gets to the AI bat - include a randomised fudge factor so it's not perfectly accurate. For bonus points, make the prediction more accurate the closer the ball is to the AI bat.

The rest of the time, just move the bat towards the predicted target location. Maybe make this randomly overshoot the intended location for extra realism.

Tweak the reaction time and accuracy to adjust the difficulty level.

_________________
"even had I suspected then the truly horrifying suffering and amazing loss of life that would be caused by our well-meaning enterprise... I'd have done it anyway. Only more so."
The Beercave | Sheep Snaggers | Markie's Revenge


Top
 Profile  
 
 Post subject: Re: Pong AI
PostPosted: Thu Jul 23, 2009 9:09 pm 0 
Offline
Baaaaaaa!
Baaaaaaa!
User avatar

Joined: Mon Mar 01, 2004 8:24 pm 0
Posts: 5070
Location: Wakefield
Do a different game, Pong is shit. :P

Beerman's ideas seem pretty sound, as do the others.

_________________
Life... Don't talk to me about life...

http://www.tcksoft.co.uk
http://www.retroremakes.com


Top
 Profile  
 
 Post subject: Re: Pong AI
PostPosted: Thu Jul 23, 2009 9:33 pm 0 
Offline
Baaaaaa!
Baaaaaa!
User avatar

Joined: Tue Sep 10, 2002 7:28 pm 0
Posts: 3997
Location: County Durham
other ideas:

Limit it's speed, so if the ball is moving too quickly or if it thinks too late it cannot get to it (but it still tries). The speed can be tweaked as part of the difficulty.

Make it worse at guessing bounces off walls, i.e. make it so it mis-estimates where it has to be pre-bounce, then adjusts after the bounce, again potentially missing.

These are based on limitations of the player, and would give the player strategies, i.e. make the ball move sideways as much as possible and bounce it off the walls. The downside of such strategies is the ball might come back the same way, making the game more interesting.


Top
 Profile  
 
 Post subject: Re: Pong AI
PostPosted: Thu Jul 23, 2009 11:28 pm 0 
Offline
Baaaaaa!
Baaaaaa!
User avatar

Joined: Sun Sep 22, 2002 9:35 am 0
Posts: 3927
Location: Nessy's Bra
If Racing the Beam is correct, then just have a pop at the VCS algo, it's bog simple:

When the ball is served, the top of the AI bat is aligned with the ball. The AI bat tracks the ball's Y movement once it's served, but skips every 8th update. It'll reset itself and recover from the drift when the ball hits the top or bottom of the screen, as long as the ball is aligned with part of the bat. Otherwise it'll carry on drifting and eventually miss.

You could do something more fancy with angle prediction but it's probably more faff than you need to do. Just fiddle about with skipping frames and catch-ups and you'll probably have something that works for sod all code.

_________________
"Don't go into a trance boys..." :: My Sets at Mixdepot :: Radio K Podcast & Blog :: BallZup! For Mac, iPhone and iPod Touch. Get it you thumb jockeys!


Top
 Profile  
 
 Post subject: Re: Pong AI
PostPosted: Fri Jul 24, 2009 7:42 am 0 
Offline
Baaaa!
Baaaa!
User avatar

Joined: Tue Dec 12, 2006 10:05 am 0
Posts: 581
Location: Brizzle
I remember playing a pong that evidently used the algorithm:

bat.y = ball.y

It wasn't all that much fun, oddly enough.


Top
 Profile  
 
 Post subject: Re: Pong AI
PostPosted: Fri Jul 24, 2009 8:43 am 0 
Offline
Sheepie Whore
Sheepie Whore
User avatar

Joined: Wed Apr 24, 2002 9:44 am 0
Posts: 20926
Location: Not here
korruptor wrote:
If Racing the Beam is correct, then just have a pop at the VCS algo, it's bog simple.


I'll have a look at that, ta :) Racing the Beam was a good book, hope they write more.

GoatFoam wrote:
I remember playing a pong that evidently used the algorithm:

bat.y = ball.y

It wasn't all that much fun, oddly enough.


That's what mine currently does. It's great for debugging, but gets boring after about half a second :) However, when both bats do it, it makes a nice attract screen.

_________________
exit(1);


Top
 Profile  
 
 Post subject: Re: Pong AI
PostPosted: Fri Jul 24, 2009 12:23 pm 0 
Offline
Sheepie Whore
Sheepie Whore
User avatar

Joined: Wed Apr 24, 2002 9:44 am 0
Posts: 20926
Location: Not here
korruptor wrote:
When the ball is served, the top of the AI bat is aligned with the ball. The AI bat tracks the ball's Y movement once it's served, but skips every 8th update. It'll reset itself and recover from the drift when the ball hits the top or bottom of the screen, as long as the ball is aligned with part of the bat. Otherwise it'll carry on drifting and eventually miss.


I had a go with this and it produces something good enough. Right now it's not possible to influence the ball by hitting it with different parts of the bat, so the game is quite hard to beat - but it can be beaten if you get lucky.

Now to add some 'spin' to the ball depending on where it lands on the bat.

_________________
exit(1);


Top
 Profile  
 
 Post subject: Re: Pong AI
PostPosted: Fri Jul 24, 2009 2:44 pm 0 
Offline
Baaaaa!
Baaaaa!
User avatar

Joined: Fri Dec 13, 2002 2:46 am 0
Posts: 1762
Location: A large grassy field.
What you need is some fuzzy logic. :D

First, Beerman's right. Computers react too quickly. Therefore, only run the AI every x frames - slow down its reactions.

Second, computers are far too precise - and this is where the fuzziness comes in. Deliberately retard its precision, so it only has a rough idea of what it's doing in a round about way.

This is easier to achieve than it sounds. As an example, I used this kind of code in my "Ping Pong" example (see attachment):

Code:
RoughY = (int(BallY / 20) * 20) + 10

Where "BallY" is where the ball actually is, and "RoughY" is the fuzzy version that the AI sees - it divides the vertical into 20 pixel high "zones" and then aims the AI at the centre of whatever "zone" the ball is in. So the AI doesn't know precisely where the ball is, only where it roughly is. Give or take 20 pixels or so.

You can, of course, change the degree of fuzziness by making the "zones" bigger or smaller. What I did in my example game is that it has a 160 pixel "zone" when the ball is in the third closest to the player - so it only knows which third of the screen (e.g. "top", "middle" or "bottom") to roughly aim for. As it gets half-way across, the precision improves to 100 pixel "zones", then 80 pixels then 25 pixels, as it reaches the CPU's bat.

Third, don't control the AI's Y coordinate directly. Control its speed (or its second derivative, in maths speak).

This also gives the game something to do in those frames when the AI ain't running. It simply continually does this every frame:

Code:
EnemyY = EnemyY + EnemyDY

Where "EnemyY" is the AI's bat Y coordinate and "EnemyDY" is the "delta" (or speed / direction) added to it every frame.

The AI does not touch its Y coordinate directly, but rather looks at its "fuzzy" view of the world (only every x frames too) and tries to work out by how much it needs to speed up / slow down the bat to get to the ball in time.

To purposefully allow for overshooting and undershooting, the speed is fuzzified too.

This time around, though, I actually work it out precisely, then make it fuzzy afterwards just before applying it to the bat - the sole reason for that is that though I want the AI to sometimes be a klutz, I don't want it to be too stupid and thus present no challenge for the player. So the fuzziness of the speed delta is only applied when it wants to move over a certain speed.

That is, when it's moving slowly, it has better speed control than when it's rushing like a madman to reach the ball on the other side of the screen - which also neatly emulates what human beings tend to do as well, as we tend to lose all our precision with the mouse when we're flinging the pointer around at a rate of knots. And, thus, the AI tends to be more likely to fuck up on exactly the kind of shots that the player will typically fuck up on - the mad dash across half the screen to get there in time.

Another real simple addition, which makes it fairer on the player (and, again, actually makes it look a bit more human) is that it only actively chases down the ball when it's coming towards the AI's bat. When the ball is heading towards the player, the AI "relaxes" and then either moves up or down a bit or roughly re-centres itself near the middle of the screen (this is chosen randomly).

The most interesting thing about this "fuzzy" approach is that the AI isn't just letting you win randomly. That's just, well, too random.

Indeed, except for the random choice of whether to re-centre itself or move up or down a bit when the ball is heading away from it, there is no randomness in the AI routine at all. It's genuinely trying to do its best at all times.

But it "fails in the gaps", so to speak.

That is, it only reacts every x frames - if the action is too fast, then it can fail in that reaction time gap. It doesn't know where things precisely are, only where they roughly are - that loss of precision creates a "gap" in which it can fail. It only has "fuzzy" control of its speed and sends it roughly at the right speed in the right direction - but not precisely so, that it can fail in the gap of its imperfect control of the bat.

And, of course, all of these impediments are exactly the impediments that humans have while playing the game too - which makes it fairer and makes the AI seem a touch more "human". Our reaction times can be a bit slow. We don't have any clue how many pixels exactly we are away from something. Even the best eye-to-hand coordination with the mouse suffers from its imprecisions.

You see, Mr.Spock was wrong. Humans are not illogical at all. We're just "fuzzy logical".:wink:

Use the mouse to move your bat. Left mouse button starts a game. Esc to quit a game in progress and also quits out of the program when on the title screen.

First to reach ten wins. Enjoy! :D


Edit - maybe the CPU player is a bit too stupid sometimes. Oh, well, demonstrates its capacity to fail. Just example code, and not exactly fine-tuned and well-polished (the text is not lined up symmetrically on the screen and it is possible, though rare, for the ball to get "stuck" in the bats or the side walls - I realise these things but can't be bothered to fix them).


Attachments:
File comment: Fuzzy Ping Pong!
PingPong.zip [565.94 KiB]
Downloaded 29 times

_________________
"Words ought to be a little wild - for they are assaults of thought on the unthinking."
- John Maynard Keynes
Top
 Profile  
 
 Post subject: Re: Pong AI
PostPosted: Fri Jul 24, 2009 7:56 pm 0 
Offline
Sheepie Whore
Sheepie Whore
User avatar

Joined: Thu Apr 25, 2002 11:30 am 0
Posts: 12000
Location: In a maze of twisty passages, all alike.
KlaxonCow wrote:
What you need is some fuzzy logic. :D


Otherwise known as Artificial Stupidity. ;)

_________________
Muttley's Blog: Rarely updated, occasionally interesting… maybe
RetroRemakes Framework: Open Source BlitzMax Game Framework
MuttMod: My Open Source BlitzMax Modules
Llamascores: Llamasoftie Hi-Score Tables


Top
 Profile  
 
 Post subject: Re: Pong AI
PostPosted: Sat Jul 25, 2009 12:36 am 0 
Offline
Sheepie Whore
Sheepie Whore
User avatar

Joined: Thu Jun 13, 2002 12:07 am 0
Posts: 11513
I did the (dodgy) AI for an air hockey mini game a few months ago.

The basic rules are obvious: when the puck is to the left of the paddle, make the paddle go left. When it's to the right, make it go right.

Then you add some inertia so the paddle takes some time to change direction. This is 100% of the gameplay. Implement it so that when you want it to go left then it applies an acceleration to the left, etc. There's a maximum speed the paddle can travel. The acceleration and maximum speed directly affect the difficulty of the AI, from easy to virtually impossible.

For the y-coordinates, I made it so the paddle moved towards the middle of the AI playing area when the puck was on the opponent's side. When the puck passed onto the AI's side, the paddle moved back towards the goal area. Again the speed of this this movement affects the difficulty. If the puck was directly in front of the paddle I applied a large forward acceleration to shoot.

And that was it. Bearing in mind that the puck and paddle were circular, all sorts of fun can be had. Fuzzy logic can fuck off. It was really popular around the office until someone higher up kept losing and knobbled the human controls. :(

_________________
What wrongs with IE the exclaim with a puzzle look.


Top
 Profile  
 
 Post subject: Re: Pong AI
PostPosted: Sat Jul 25, 2009 11:55 am 0 
Offline
Baaaaa!
Baaaaa!
User avatar

Joined: Mon Oct 27, 2003 5:05 am 0
Posts: 1590
Location: New Zealand
How about offsetting the accuracy of the bat's location by a random amount, but with values that map to a normal distribution(bell curve)? It will hit bang on or close to the mark most of the time, only just catch it some, and every now and then will miss. On the rare occasion it will miss by a margin. Then you can scale the difficulty by just changing the standard deviation of the distribution, perhaps during play if the ball increases in speed.
It would be easy to calculate exactly where the bat will need to be while the ball is still traveling. Humans 'guess' where the ball will be and move into position, offsetting that calculation will make the bat seem to guess as well. Building on that, changing the predicted angle of a wall rebound randomly might make the bat seem more human as well.


Top
 Profile  
 
 Post subject: Re: Pong AI
PostPosted: Sun Jul 26, 2009 12:31 am 0 
Offline
Sheepie Whore
Sheepie Whore
User avatar

Joined: Wed Apr 24, 2002 9:44 am 0
Posts: 20926
Location: Not here
I was going to have different game modes, in the style of the Atari 2600 carts. I think I might also have different computer opponents to play against too, each using a different AI routine.

_________________
exit(1);


Top
 Profile  
 
 Post subject: Re: Pong AI
PostPosted: Sun Jul 26, 2009 5:22 am 0 
Offline
Baaaaaa!
Baaaaaa!
User avatar

Joined: Sun May 23, 2004 5:54 am 0
Posts: 2231
Location: Sandy Eggo
Have you considered using neural nets and training them?
http://www.karlsims.com/evolved-virtual-creatures.html

_________________
>>>3d Live-coding in JavaScript <<<
>>>also available in fat-free<<<
"We are working on a new model that has flashing lights."


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 42 posts ]  Go to page 1, 2, 3  Next

All times are UTC


Who is online

Users browsing this forum: lis0r and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group