Monday, January 2, 2012

Does the AI cheat?

I noticed that there was a comment on both Android market as well as on Amazon appstore about AI cheating with battle results. I'll try to clear out the confusion about this.

Short explanation: No, AI really does not try to cheat. But apparently, on some devices (quite rare, it seems!) the random number generator in Android does not provide numbers that are random enough, but is occasionally "stuck" with giving out similar numbers over and over again ... A bit like imbalanced dice, which show always the same face up when thrown. This is a software or hardware bug which I can't directly fix. However, I'm making a workaround for these devices, and will upload it as version 1.2.1 when I have done a little smoke test for the release (will be available on Android Market immediately after upload, on Amazon Appstore in about one week as it needs to be approved first).

Long, boring and technical explanation: The battle resolver in the game does not even know or care, which side is controlled by AI and which by human. It is completely separate from AI logic. It just adds attacker and defender strength together, we shall call this number the "total strength" of the battle. Then the battle resolver retrieves the next random number between 0.0 (inclusive) and 1.0 (exclusive) from a random number generator, and multiplies it by the total strength of battle. If the resulting number is less than attacker strength, attacker is declared to be the winner. Now then ... if the random number generator consistently gives out numbers that are closer to 1.0 than 0.0, the battle results start to be skewed, in favor of the defender (being stuck with numbers near 0.0 would favor attacker instead). Now I'm switching away from ordinary "Random" to use the "SecureRandom" random number generator, which should provide randomness good enough to be cryptographically secure, so should be perfectly fine for this purpose also. As additional measure, I also added now code to shuffle that random number around by using the millisecond part from system current time. This has no effect on an already correctly working random number generation, but should provide a bit of "emergency" randomization in the case where even the SecureRandom would by some miracle fail to provide decent enough level of randomness.

If there's anyone who had encountered the "cheating AI" problem and happens to read this, please confirm after updating, if this workaround in v1.2.1 cures it for good, I'd really appreciate it! Just post a comment here or send email to poprominfo AT