Skip to main content
2 of 6
added 7 characters in body
Roy van Rijn
  • 1.1k
  • 9
  • 13

Gazzr

This bot looks at the played games and tries to guess how to beat the enemy:

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class Gazzr { // <- Tries to guess

    private static final int DEPTH = 3; // <- amount of moves analysed
    private final List<String> moves = Arrays.asList(new String[] { "R", "L", "V", "S", "P" });
    private final String[] beats = new String[] { "LS", "VP", "SR", "PL", "RV" };
    private final Random random = new Random();

    public static void main(final String[] args) {
        System.out.println(new Gazzr().play(args));
    }

    private String play(final String[] args) {

        if (args == null || args.length == 0 || args[0].length() < DEPTH) {  // Start with paper or lizard, because I hate Spock
            return random.nextBoolean() ? "P" : "L";
        }

        final Map<String, int[]> completeHistory = new HashMap<String, int[]>();

        final char[] myMoves = args[0].toCharArray();
        final char[] oppMoves = args[1].toCharArray();

        int score = 0;
        final StringBuilder history = new StringBuilder();
        for (int i = 0; i < myMoves.length; i++) {
            final int myIndex = moves.indexOf("" + myMoves[i]);
            final int oppIndex = moves.indexOf("" + oppMoves[i]);

            //Update database:
            if(history.length() == (DEPTH*2)) {
                final String historicMoves = normalize(history.toString());
                int[] distr = completeHistory.get(historicMoves);
                if(distr == null) {
                    distr = new int[5];
                }
                distr[oppIndex]++;
                completeHistory.put(historicMoves, distr);
                history.delete(0, 2);
            }
            history.append(""+oppIndex);
            history.append(""+myIndex);

            //Calculate current score:
            if (myMoves[i] != oppMoves[i]) {
                // Not same:
                final String beatingMoves = beats[myIndex];
                if (beatingMoves.contains(oppMoves[i] + "")) {
                    score++;
                } else {
                    score--;
                }
            }
        }
        if(score < -6) {
            //Panic
            return moves.get(random.nextInt(moves.size()));
        } else {
            final String historicMoves = normalize(history.toString());
            final int[] distr = completeHistory.get(historicMoves);

            if(distr == null) {
                return moves.get(random.nextInt(moves.size()));
            }

            int bestIndex = 0;
            for(int i = 1;i<distr.length;i++) {
                if(distr[i] > distr[bestIndex]) {
                    bestIndex = i;
                }
            }
            //Opponent will play:
            if(distr[bestIndex] == 0) {
                //No idea, play random:
                return moves.get(random.nextInt(moves.size()));
            }
            final int toAdd = history.charAt(0)-48;
            bestIndex = (bestIndex+toAdd)%5;

            //Denormalize:
            return "" + beats[bestIndex].charAt(random.nextInt(2));
        }
    }

    // attempt to do smart stuff here, probably faulty
    private String normalize(final String input) {
        final int remove = input.charAt(0)-48;
        final StringBuilder norm = new StringBuilder();
        for(final char c:input.toCharArray()) {
            // magic
            norm.append((char)(((((c-48)+5)-remove)%5)+48));
        }
        return norm.toString();
    }
}

This code is pretty untested, I hope it works...

Roy van Rijn
  • 1.1k
  • 9
  • 13