Created
August 1, 2018 11:34
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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