NOTE: None of the following is actually tested, this is all just a theory.
Recently there has been a lot of talk on the forums about game AI. So far, all of it has been rule based AI, meaning "When player does x, do y"
Which works well for most games, but what if you can't account for all those rules? What if you don't know where your player is going to go or do at what time? This happens to me in many games whenever there are multiple things the computer has to account for in making a decision on what to do. In that case, you can't just use rule based AI. You need to computer to adapt to it's situation. But how could you possibly do that? You can't make a fully functioning brain in Hopscotch! And that is true, making a human scale intelligence is pretty much impossible. But, I introduce to you a new way to make enemy AI where the enemies learn and adapt from past experiences.
But, what is a genetic algorithm?
Well, to answer this question I will quote a passage from Dawkin's book The Blind Watchmaker which does a great job of describing Evolutionary/Genetic Algorithms.
"I don't know who it was first pointed out that, given enough time, a monkey bashing away at random on a typewriter could produce all the works of Shakespeare. The operative phrase is, of course, given enough time. Let us limit the task facing our monkey somewhat. Suppose that he has to produce, not the complete works of Shakespeare but just the short sentence 'Methinks it is like a weasel', and we shall make it relatively easy by giving him a typewriter with a restricted keyboard, one with just the 26 (capital) letters, and a space bar. How long will he take to write this one little sentence?"
Well, it turns out that a computer program made to do this task would not produce this phrase, even given the lifetime of the universe to run. So Dawkins describes another way to produce this phrase
We again use our computer monkey, but with a crucial difference in its program. It again begins by choosing a random sequence of 28 letters, just as before ... it duplicates it repeatedly, but with a certain chance of random error – 'mutation' – in the copying. The computer examines the mutant nonsense phrases, the 'progeny' of the original phrase, and chooses the one which, however slightly, most resembles the target phrase, METHINKS IT IS LIKE A WEASEL.
By continuously repeating this process, a 28 character string of gibberish will soon turn into the target phrase.
This is what a genetic algorithm is. Taking an original parent, creating copies of it that are slightly different, taking the best one out of those, and creating mutations off of that, etc.
But we want enemy AI, not to produce phrases!
Well, we can apply genetic algorithms to enemy AI. We first start out with the "skeleton" of the AI. For my example I will use a rock paper scissors AI. We need 3 variables in this case, one for the probability of rock, one for the probability of scissors, and one for the probability of paper.
Paper=33% Rock=33% Scissors=33%
This is our "seed"
Then, we play 16 games against the player. Each 4 times we use a slightly different probability.
Paper=35% Rock=30% Scissors=35%
Paper=29% Rock=38% Scissors=33%
How much the probabilities are changed is up to you.
Then we take each of the probabilities and how many games they won, and compare them. Lets say that with this probability:
Paper=29% Rock=38% Scissors=33%
We won 3/4 games, our best score out of all the others. We then take that probability and go back to step one, except instead of all 33%, we use our new best.
And there you have it! Rock paper scissors AI! You can then apply this to any game with some tweaking of the variables!
That's great! But what are the downsides?
Here is where it gets tricky. There are two main downsides to genetic algorithms, which I will explain here.
They are slow
If you have a complicated game, your AI is not going to adapt very fast. With rock paper scissors it is ok because a totally random algorithm will work, but in a defense game or rpg type thing, your enemies are going to act very weirdly at first while they try to learn what is happening and what they are supposed to do. It won't be until you fight them enough for them to learn how to fight you back.
Implementation is hard
Your main problem when coding it will be comparing all the wins and finding which set of variables did the best. This is because Hopscotch does not have support for lists/arrays, so you can either:
Make a bunch of variables to manually store each one, which will slow down your code and make it considerably larger
Find a way to compare as you go using some sort of temporary variable wizardry
Hopscotch being a turing complete language, we know it is possible to implement a genetic algorithm for enemy AI. And in some cases, it works great! But, when you have a complex game and your enemy has to immediately know what to do, I would say to use a rule based AI or a mix of genetic and rule based.
So the moral of the story is:
Hopscotch, please add lists!