Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Created August 1, 2018 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssaurel/7ac885f2db25732d19aab07bd3c4c346 to your computer and use it in GitHub Desktop.
Save ssaurel/7ac885f2db25732d19aab07bd3c4c346 to your computer and use it in GitHub Desktop.
AI for our Rock Paper Scissors Lizard Spock game in Java on the SSaurel's Channel
private Item nextMove(Item prev) {
if (nbThrows < 1) {
// first move => we can't use Markov Chain prediction
// so we use a random on the Item list
return Item.values()[RANDOM.nextInt(Item.values().length)];
}
// we try to predict next Item choosen by the user by reading data in our Markov Chain
// for the prev entry in the array
int nextIndex = 0;
for (int i = 0; i < Item.values().length; i++) {
int prevIndex = prev.ordinal();
if (markovChain[prevIndex][i] > markovChain[prevIndex][nextIndex]) {
nextIndex = i;
}
}
// Item probably played by the user is in nextIndex
Item predictedNext = Item.values()[nextIndex];
// we choose amongst item for which this probably item loses
List<Item> losesTo = predictedNext.losesTo;
return losesTo.get(RANDOM.nextInt(losesTo.size()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment