Computer Science Canada [tutorial] AI for beginners |
Author: | Hackmaster [ Wed Jan 10, 2007 10:55 pm ] | ||||
Post subject: | [tutorial] AI for beginners | ||||
Hello ladies and gentlemen, and welcome to AI in Turing 101. ever wanted to spice up pong? ever wanted to re-create pacman, but run into problems with ghosts? how about the computers in tron? AI solves all of your problems!!! the goal of this tutorial is to teach you the basics of AI. it's simple really, once you get the hang of it. but so is vector calculas. So what is AI? let's start with a definition, shall we? AI, or Artificial Intellegence, is essentially giving the computer the power to make it's own decisions. the computer has a mind of it's own!! it's ALIVE!!! Muahahahaha.... Ahem. AI is a powerful thing. it can become extremely complex, and there are university courses, and degrees in it. but, you can trace it to the roots, which is what we will do. I am only going to teach the bare bones lessons that are needed to make AI for basic games like pong, or pacman, or whatever. if it has an enemy that is computer controlled, it needs AI in one form or another. the most basic form of AI I can think of is an if statement. you are giving the computer power, aren't you? you are letting it make your decisions for you! think about it. in that cash register program, it's figuring out wheather or not you need to give back change or not. it's saving you work by making decisions, albeit an easy one, but still a decision. the AI that I will be showing you is all about if statements and randints. (or Rand.Int(), you elitists*). let's have a look at pong. when you first start programming, In my opinion, Pong is a right of passage. if you can make pong, you may not be the best. you may not even be all that good. but you need to know at least a bit about coding to make something like pong come together. At that point, you should be happy with that two player version you made. works just like the original. if not better. but what if your friends aren't around, and you feel a need to be retro? you can't play both sides at once, so why not get the computer to play the other side for you? there are many ways to go about this, but I will show you my two favourites. you can: 1) make a procedure to always get the ball, and one to miss it. then, have a random number determine which one happens. this way you can have difficulty settings, and it makes for easy coding.
ugh. this code isn't all that pretty, but it illustrates my point. you have one procedure, comp_get_ball, which makes the paddle randomly appear right where the ball is going to be, and the other function, which dosen't do anything. it just keeps the paddle in one place. then, we have a randint. it determines a number between 1 and 10. then we use this number however we want. we want this computer to be reasonable at this game, so we have it get it 50% of the time. we do this by saying "if the number is greater than 5, it means we hit it. " half the numbers are greater than five, so we say that is a fifty percent chance. when you do this kind of thing, a number from 1- 10 is great, because the percents are easy to figure out. now, you probably want something better than this. in real life, you would make the hit program move up to hit the ball by either following it's moves, or just moving rapidly to meet it when it's on your side. having it spawn randomly will have players yelling about how rigged your game is. there is a better way. that would be way number 2. 2) make a function that increments or decrements the computer's paddle's y co-ordinate. if you don't throw in the random, it will just follow the ball around exactly. when the ball goes up, so does the paddle. you could have it do that, and that way it would never miss. but again, you have people screaming about fairness. people are whiners. but you generally need to apease whiners in some way. so, make a random statement. it has a chance that it will call that function and increase the paddle's y by 1. or 7. or 38. whatever you like. if it fails, then it doesn't, and so it falls behind the ball. now, the great thing about this system is that the paddle is, say, 100 pixels long, and you only need to touch the ball on one. so, if you start tracking in the middle, you can have that function miss 50 times before it isn't able to reach the ball in time. and to top it off, it looks a lot more realistic. to make it look even more realistic, you could have it start doing this loop when it crosses the halfway line, or when it starts moving in your direction again. whatever. in more advanced AI, you could have it anticipate the bounces. calculate how many pixels from the ball to the bottom of the screen, which you could then calculate where it will be when it bounces. and then you could adjust the paddle to go to the place where the next bounce will occur. the thing about pong is that if anything, it's predictable. if the opponent hits it, you know it's coming back, and you can reasonabley anticiapate where it's going to be. this would probably be the best way to make an AI for pong. use number 2 to make a realistic system for moving, and add a random element, but instead of following the ball move for move, you would anticapate the ball's movement, and try to move there instead. you could do quite advanced calculations, and know where the ball will be the moment it leaves your paddle, if your opponent hits it. or where it's going to be in five turns. or ten. or five thousand. but, to make it so it isn't too rigged, you would probably only start trying to get to your waypoint when the ball crosses the line. as for things like pac-man, it's more complex. you need to have 3 individual AIs going, and you have to have if statements. a crappy example for ghost AI in psuedo code:
very basic. very. I have poor naming conventions, but you get the point. if pacman is with a certain range, I would home in on him. or something. and it wouldn't be 100% of the time, or else we come up with the whole "rigged" issue with the "whiners" again. oh well. well, there you have it. these basic few princibles will work anywhere in basic AI. if you are making an rpg, and you need to decide what an enemy does, it's just ifs and randints. if health < 50 then heal. 50% of the time. if health < 25 then heal. 75% of the time. pretty basic, once you get the hang of it. that's about it! also, just as a finishing note, if you didn't see the asterisk, Rand.Int() people read the disclaimer. thanks guys! feedback appreciated!! *disclaimer: the reason I am using randint instead of Rand.Int() is because that is what they teach in schools. This is my side of the argument, and if you haven't already, you might read the argument on the post "help with randint" in turing help, and "what is Compsci.ca?" in General discussion. please don't comment about it or flame me. thanks! |
Author: | gugiey [ Thu Jan 11, 2007 9:10 am ] |
Post subject: | Re: [tutorial] AI for beginners |
nice now i can finaly finish my pong in class... +karma |
Author: | tootz91 [ Thu Jan 11, 2007 9:10 am ] |
Post subject: | RE:[tutorial] AI for beginners |
yea i can finish the pong in class but it still wont hlp me on my final project cause im like the worst programmer ever......... so yah! |
Author: | ericfourfour [ Thu Jan 11, 2007 5:18 pm ] | ||
Post subject: | Re: [tutorial] AI for beginners | ||
What about AI using object orientation? An example you might want to use in an RPG:
This particular example would work very will with mix-ins in Ruby. There may be other things in the world that want to choose an attack not just characters. Just something I thought I'd throw out there. |
Author: | CodeMonkey2000 [ Sun Jan 14, 2007 2:20 pm ] |
Post subject: | Re: [tutorial] AI for beginners |
Hackmaster @ Wed Jan 10, 2007 10:55 pm wrote: *disclaimer: the reason I am using randint instead of Rand.Int() is because that is what they teach in schools. This is my side of the argument, and if you haven't already, you might read the argument on the post "help with randint" in turing help, and "what is Compsci.ca?" in General discussion. please don't comment about it or flame me. thanks! Why on earth does turing even have randint as a procedure? Isn't that considered as a bad programming convention? |
Author: | Skynet [ Sun Jan 14, 2007 2:43 pm ] |
Post subject: | Re: [tutorial] AI for beginners |
Hackmaster @ Wed Jan 10, 2007 10:55 pm wrote: AI solves all of your problems!!!
I've been wondering for a while what to call these techniques that you describe here. As a rule, I think calling anything "AI" is a bad idea, since as a co-worker said, "it describes the set of algorithms people don't know how to write- once the problem gets solved, it's not called AI anymore". However, it's a really good blanket statement for getting across in few words "Here lies code which makes the computer appear to solve problems." I wonder if there's a way to describe this part of a game in a more accurate way, without having to resort to overly technical language. |
Author: | ericfourfour [ Sun Jan 14, 2007 6:23 pm ] |
Post subject: | RE:[tutorial] AI for beginners |
Artificial intelligence is essential man-made intelligence. The program is given information and it finds an answer using a man-made algorithm. The only difference between man-made intelligence and regular intelligence is that the algorithm for regular intelligence comes from our brain, not from a chunk of code. |
Author: | Prince Pwn [ Sun Jan 14, 2007 11:50 pm ] | ||
Post subject: | Re: [tutorial] AI for beginners | ||
Quote: Why on earth does turing even have randint as a procedure? Isn't that considered as a bad programming convention?
I'll fix that for you, use this instead:
|
Author: | Clayton [ Sun Jan 14, 2007 11:58 pm ] |
Post subject: | Re: [tutorial] AI for beginners |
that's just as bad. The reason is because it's still a procedure that mutilates a variable that you pass to it. the point of a function is to take parameters and return a value, which you can then do anythin with. |
Author: | BenLi [ Tue Jan 16, 2007 6:30 pm ] |
Post subject: | RE:[tutorial] AI for beginners |
you fools learned nothing from the matrix trilogies |
Author: | Agner [ Sat Feb 21, 2009 2:55 pm ] |
Post subject: | RE:[tutorial] AI for beginners |
Thanks, really helped clear up some things on AI and examples of it in games/such. |
Author: | evildaddy911 [ Fri Jan 06, 2012 3:30 pm ] |
Post subject: | Re: [tutorial] AI for beginners |
Quote: people are whiners.
so true good job, messy, but it gets the point across well |
Author: | Aange10 [ Fri Jan 06, 2012 5:28 pm ] |
Post subject: | RE:[tutorial] AI for beginners |
Evildaddy, you've necroed so many posts on the forums I've lost count. Can you please check the dates before you post on a 3 year old thread? |
Author: | mirhagk [ Fri Jan 06, 2012 5:36 pm ] |
Post subject: | RE:[tutorial] AI for beginners |
Or.... just don't go post on topics that you dug up from past years. |
Author: | Raknarg [ Fri Jan 06, 2012 11:04 pm ] |
Post subject: | RE:[tutorial] AI for beginners |
Unless you have a legit question. then it's ok |