Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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