I originally wrote this game in Flash 4 using pseudo-arrays (normal variables with dynamic names). Practically at the same time I finished it I got Flash MX and decided to add some things to it that I couldn't add in Flash 4. The game stayed pretty much the same, I just got rid of all the parts where it converted the variables to and from String's since that's what Flash 4 works with, used the Array object instead of pseudo-arrays, made it so it loads an HTML file for the high scores instead of a plain text file, and also changed the preloader to use getBytesLoaded() instead of the _framesloaded property. So it's only Flash 5 and up compatible now. But I don't mind.
Probably the most interesting thing about the game is the A.I.
I don't want to go into details about thecode I used, because it's pretty messed up, especially since I wrote it in Flash 4 initially. But I'd like to explain some aspects of how it works.
I actually cheated later to make the game challenging, where I made it so there's a small percentage that the computer will have a direct hit on one of your ships. But I only put that in later to make it challenging.
It's probably obvious when you play the game, the A.I. is split into 2 modes: where it searches for a ship around the game board, and where it found the ship and is now trying to destroy it entirely. I implemented the latter first. I don't know why. It's pretty simple and I'm sure you've found the pattern in computer's moves when it destroys your ships. Once it hits a part of your ship, it knows that there must be at least 1 adjacent cell next to it that's also a part of the ship. So it tries to find this cell by systematically going through every adjacent cell. Once it finds it depending on whether the cell is above or below, or to the right or left, it starts going in that direction, hitting every cell until it hits water.
Upon encountering water it starts going in the opposite direction from the first cell it hit.
While doing all this it keeps a tab on which ship is the longest that still wasn't hit, so that it knows to stop trying
to destroy a ship if it already hit the number of cells that the longest ship has, since continuing trying to destroy the
whole ship in that case would be a waste of a turn. This may seem simple, but I spent many evenings yelling at Flash
while trying to get this part to work.
The other mode was easier to do than the first, because it doesn't need to keep track of so many things.
But non-the-less I didn't make it so it stupidly guessed coordinates using the pseudo-random number generator. I
obviously made it so that it didn't pick coordinates that were already picked :D .
I also tried to reduce the ramdomness of it's guessed, so that it would make educated guesses rather than just random. What I did was I set up a very simple Finite State Automata (they're used to model regular expressions in many applications, like grep and PERL), where it would go through every cell checking a number of conditions that would make it possible for a part of a ship
to be there and these coordinates that were found were put in an array. But I made it so the randomness would be
reduced even more by making it check that only the whole biggest, undestroyed ship would be able to fit wholly into
an area on the grid and by storing the coordinates of only 1 cell from that area. So this way the number of cells being
picked from reduced drastically.
To show this, at the very start of the game there are 100 unhit cells to choose from.
Using my method this number is reduced to 36! That's a lot better than just guessing randomly.
When testing the game I could still easily beat the computer, but the computer's score was almost nearly the
same as mine everytime. Therefore, it was playing practically on the same level as me. I figured the fact that the computer
still lost most of the time was due to people still having more intellect that doesn't rely solely on mechanization.
And I didn't feel like trying to come up with a way to make the computer have something like that when playing, so I made it
cheat... a little bit. :-) |