Skip to main content
bugfixing
Source Link
Roy van Rijn
  • 1.1k
  • 9
  • 13
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

// Aggressive predictor using own and opponent history
public class Gazzr {

    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[] beatsbeatIndex = new String[] { "LS", "PV", "RS", "PL", "RV", "LS" };
    private final List<String> losesFromloseFromIndex = Arrays.asList(new String[] { "LS", "PV", "RS", "PL", "RV", "LS" });
    private final Random random = new Random();

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

    public String play(final String[] args) {
        if (args == null || args.length == 0 || args[0].length() < DEPTH) {
            return moves.get(random.nextInt(moves.size()));
        }
        final Map<String, int[]>float[]> oppHistDb = new HashMap<String, int[]>float[]>();
        final Map<String, int[]>float[]> myHistDb = new HashMap<String, int[]>();
        final char[] myMoves = args[0].toCharArrayfloat[]>();
        final char[] oppMoves = args[1].toCharArray();
        int i = Math.max(DEPTH, myMoves.length-50); // Limit to 50 previous movesDEPTH;
        while(i < myMovesargs[0].length()) {
            updateDB(args[0].substring(i-DEPTH, i), oppMoves[i], myHistDb);
            updateDB(args[1].substring(i-DEPTH, i), oppMoves[i], oppHistDb);
            i++;
        }
        //Predict based on own and opponent moves:
        Integer p1 = predict(args[0].substring(i-DEPTH, i), myHistDb);
        Integer p2 = predict(args[1].substring(i-DEPTH, i), oppHistDb);
        if(p1 == null && p2 == null) {
            return moves.get(random.nextInt(moves.size()));
        }
        if(p2 == null) {
            return "" + beats[p1]beatIndex[p1].charAt(random.nextInt(2));
        } else if(p1 == null) {
            return "" + beats[p2]beatIndex[p2].charAt(random.nextInt(2));
        }
        String s1 = moves.get(p1)+moves.get(p2); //Try to find common ground
        String s2 = moves.get(p2)+moves.get(p1);
        if(losesFromloseFromIndex.contains(s1)) {
            return moves.get(losesFromloseFromIndex.indexOf(s1));
        }
        if(losesFromloseFromIndex.contains(s2)) {
            return moves.get(losesFromloseFromIndex.indexOf(s2));
        }
        if(random.nextBoolean()) {
            return ""+ beats[p1]beatIndex[p1].charAt(random.nextInt(2));
        } else {
            return ""+ beats[p2]beatIndex[p2].charAt(random.nextInt(2));
        }
    }
    
    private void updateDB(String lastMoves, char nextMove, Map<String, int[]>float[]> database) {
        int oppIndex = moves.indexOf("" + nextMove);
        int[]float[] distr = database.get(lastMoves);
        if(distr == null) {
            distr = new int[5];float[5];
        }
        distr[oppIndex]++;distr[oppIndex] += 1.0f;
        database.put(lastMoves, distr);
    }
    
    public Integer predict(String historicMoves, Map<String, int[]>float[]> database) {
        final int[]float[] distr = database.get(historicMoves);
 
        if(distr == null) {
            return null;
        }
        return predictFromArray(distr);
    }

    private Integer predictFromArray(final float[] distr) {
        float total = 0f;
        for(int bestIndexi = 0;0;i<distr.length;i++) {
            total += distr[i];
        }
        float target = total * random.nextFloat();
        total = 0f;
        for(int i = 1;i<distr0;i<distr.length;i++) {
            total += distr[i];
            if(distr[i]total > distr[bestIndex]target) {
                bestIndex =return i;
            }
        }
        return bestIndex;distr.length - 1;
    }
}

Bug fixes:

  • Tournament round #1: Bot came in last, a bug was causing it to try to lose instead of win
  • (X beats Y, bot predicts opponent plays X, play Y!) #epic
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

// Aggressive predictor using own and opponent history
public class Gazzr {

    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", "PV", "RS", "PL", "RV" };
    private final List<String> losesFrom = Arrays.asList(new String[] { "PV", "RS", "PL", "RV", "LS" });
    private final Random random = new Random();

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

    public String play(final String[] args) {
        if (args == null || args.length == 0 || args[0].length() < DEPTH) {
            return moves.get(random.nextInt(moves.size()));
        }
        final Map<String, int[]> oppHistDb = new HashMap<String, int[]>();
        final Map<String, int[]> myHistDb = new HashMap<String, int[]>();
        final char[] myMoves = args[0].toCharArray();
        final char[] oppMoves = args[1].toCharArray();
        int i = Math.max(DEPTH, myMoves.length-50); // Limit to 50 previous moves
        while(i < myMoves.length) {
            updateDB(args[0].substring(i-DEPTH, i), oppMoves[i], myHistDb);
            updateDB(args[1].substring(i-DEPTH, i), oppMoves[i], oppHistDb);
            i++;
        }
        //Predict based on own and opponent moves:
        Integer p1 = predict(args[0].substring(i-DEPTH, i), myHistDb);
        Integer p2 = predict(args[1].substring(i-DEPTH, i), oppHistDb);
        if(p1 == null && p2 == null) {
            return moves.get(random.nextInt(moves.size()));
        }
        if(p2 == null) {
            return "" + beats[p1].charAt(random.nextInt(2));
        } else if(p1 == null) {
            return "" + beats[p2].charAt(random.nextInt(2));
        }
        String s1 = moves.get(p1)+moves.get(p2); //Try to find common ground
        String s2 = moves.get(p2)+moves.get(p1);
        if(losesFrom.contains(s1)) {
            return moves.get(losesFrom.indexOf(s1));
        }
        if(losesFrom.contains(s2)) {
            return moves.get(losesFrom.indexOf(s2));
        }
        if(random.nextBoolean()) {
            return ""+ beats[p1].charAt(random.nextInt(2));
        } else {
            return ""+ beats[p2].charAt(random.nextInt(2));
        }
    }
    
    private void updateDB(String lastMoves, char nextMove, Map<String, int[]> database) {
        int oppIndex = moves.indexOf("" + nextMove);
        int[] distr = database.get(lastMoves);
        if(distr == null) {
            distr = new int[5];
        }
        distr[oppIndex]++;
        database.put(lastMoves, distr);
    }
    
    public Integer predict(String historicMoves, Map<String, int[]> database) {
        final int[] distr = database.get(historicMoves);
 
        if(distr == null) {
            return null;
        }
        int bestIndex = 0;
        for(int i = 1;i<distr.length;i++) {
            if(distr[i] > distr[bestIndex]) {
                bestIndex = i;
            }
        }
        return bestIndex;
    }
}
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class Gazzr {

    private static final int DEPTH = 3;
    private final List<String> moves = Arrays.asList(new String[] { "R", "L", "V", "S", "P" });
    private final String[] beatIndex = new String[] { "PV", "RS", "PL", "RV", "LS" };
    private final List<String> loseFromIndex = Arrays.asList(new String[] { "LS", "PV", "RS", "PL", "RV" });
    private final Random random = new Random();

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

    public String play(final String[] args) {
        if (args == null || args.length == 0 || args[0].length() < DEPTH) {
            return moves.get(random.nextInt(moves.size()));
        }
        final Map<String, float[]> oppHistDb = new HashMap<String, float[]>();
        final Map<String, float[]> myHistDb = new HashMap<String, float[]>();
        final char[] oppMoves = args[1].toCharArray();
        int i = DEPTH;
        while(i < args[0].length()) {
            updateDB(args[0].substring(i-DEPTH, i), oppMoves[i], myHistDb);
            updateDB(args[1].substring(i-DEPTH, i), oppMoves[i], oppHistDb);
            i++;
        }
        //Predict based on own and opponent moves:
        Integer p1 = predict(args[0].substring(i-DEPTH, i), myHistDb);
        Integer p2 = predict(args[1].substring(i-DEPTH, i), oppHistDb);
        if(p1 == null && p2 == null) {
            return moves.get(random.nextInt(moves.size()));
        }
        if(p2 == null) {
            return "" + beatIndex[p1].charAt(random.nextInt(2));
        } else if(p1 == null) {
            return "" + beatIndex[p2].charAt(random.nextInt(2));
        }
        String s1 = moves.get(p1)+moves.get(p2);
        String s2 = moves.get(p2)+moves.get(p1);
        if(loseFromIndex.contains(s1)) {
            return moves.get(loseFromIndex.indexOf(s1));
        }
        if(loseFromIndex.contains(s2)) {
            return moves.get(loseFromIndex.indexOf(s2));
        }
        if(random.nextBoolean()) {
            return ""+ beatIndex[p1].charAt(random.nextInt(2));
        } else {
            return ""+ beatIndex[p2].charAt(random.nextInt(2));
        }
    }
    
    private void updateDB(String lastMoves, char nextMove, Map<String, float[]> database) {
        int oppIndex = moves.indexOf("" + nextMove);
        float[] distr = database.get(lastMoves);
        if(distr == null) {
            distr = new float[5];
        }
        distr[oppIndex] += 1.0f;
        database.put(lastMoves, distr);
    }
    
    public Integer predict(String historicMoves, Map<String, float[]> database) {
        final float[] distr = database.get(historicMoves);
        if(distr == null) {
            return null;
        }
        return predictFromArray(distr);
    }

    private Integer predictFromArray(final float[] distr) {
        float total = 0f;
        for(int i = 0;i<distr.length;i++) {
            total += distr[i];
        }
        float target = total * random.nextFloat();
        total = 0f;
        for(int i = 0;i<distr.length;i++) {
            total += distr[i];
            if(total > target) {
                return i;
            }
        }
        return distr.length - 1;
    }
}

Bug fixes:

  • Tournament round #1: Bot came in last, a bug was causing it to try to lose instead of win
  • (X beats Y, bot predicts opponent plays X, play Y!) #epic
improved code, now more aggressive using own and opponent history
Source Link
Roy van Rijn
  • 1.1k
  • 9
  • 13
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public// classAggressive Gazzrpredictor {using //own <-and Triesopponent tohistory
public guessclass Gazzr {

    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", "PV", "RS", "PL", "RV" };
    private final List<String> losesFrom = Arrays.asList(new String[] { "PV", "RS", "PL", "RV", "LS" });
    private final Random random = new Random();

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

    public String play(final String[] args) {
        if (args == null || args.length == 0 || args[0].length() < DEPTH) {
            return moves.get(random.nextInt(moves.size()));
        }
        final Map<String, int[]> databaseoppHistDb = new HashMap<String, int[]>();
        final Map<String, int[]> myHistDb = new HashMap<String, int[]>();
        final char[] myMoves = args[0].toCharArray();
        final char[] oppMoves = args[1].toCharArray();
        final StringBuilderint historyi = new StringBuilderMath.max(DEPTH, myMoves.length-50);
  // Limit to 50 previous moves
  for (int i = 0;  while(i < myMoves.length; i++length) {
            finalupdateDB(args[0].substring(i-DEPTH, inti), oppIndexoppMoves[i], =myHistDb);
 moves           updateDB(args[1].indexOfsubstring(""i-DEPTH, +i), oppMoves[i], oppHistDb);
            i++;
        }
        //UpdatePredict databasebased on own and opponent moves:
         Integer p1 = ifpredict(historyargs[0].lengthsubstring() == i-DEPTH, i), {myHistDb);
        Integer p2 = predict(args[1].substring(i-DEPTH, i), oppHistDb);
   final String lookup = history.toString if();
p1 == null && p2 == null) {
         int[] distr = databasereturn moves.get(lookuprandom.nextInt(moves.size()));
        }
        if(distrp2 == null) {
                    distrreturn ="" new+ int[5];beats[p1].charAt(random.nextInt(2));
         } else if(p1 == null) {
  }
          return "" + beats[p2].charAt(random.nextInt(2));
   distr[oppIndex]++;
     }
        String s1 = databasemoves.putget(lookup, distrp1)+moves.get(p2);
  //Try to find common ground
        String s2 = historymoves.deleteget(0, 1p2)+moves.get(p1);
           if(losesFrom.contains(s1)) }{
            historyreturn moves.appendget(""+oppIndexlosesFrom.indexOf(s1));
 
        }
        if(losesFrom.contains(s2)) {
        String move = predict(history, databasereturn moves.get(losesFrom.indexOf(s2));
        if(move}
 != null      if(random.nextBoolean()) {
            return move;""+ beats[p1].charAt(random.nextInt(2));
        } else {
            return moves""+ beats[p2].getcharAt(random.nextInt(moves.size()2));
        }
    }
    
    publicprivate Stringvoid predictupdateDB(StringBuilderString historylastMoves, char nextMove, Map<String, int[]> database) {
        finalint StringoppIndex historicMoves= moves.indexOf("" + nextMove);
        int[] distr = historydatabase.toStringget(lastMoves);
        if(distr == null) {
            distr = new int[5];
        }
        distr[oppIndex]++;
        database.put(lastMoves, distr);
    }
    
    public Integer predict(String historicMoves, Map<String, int[]> database) {
        final int[] distr = database.get(historicMoves);

        if(distr == null) {
            return null;
        }
        int bestIndex = 0;
        for(int i = 1;i<distr.length;i++) {
            if(distr[i] > distr[bestIndex]) {
                bestIndex = i;
            }
        }
        //Play move:
        return "" + beats[bestIndex].charAt(random.nextInt(2));bestIndex;
    }
}
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", "PV", "RS", "PL", "RV" };
    private final Random random = new Random();

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

    String play(final String[] args) {
        if (args == null || args.length == 0 || args[0].length() < DEPTH) {
            return moves.get(random.nextInt(moves.size()));
        }
        final Map<String, int[]> database = new HashMap<String, int[]>();
        final char[] myMoves = args[0].toCharArray();
        final char[] oppMoves = args[1].toCharArray();
        final StringBuilder history = new StringBuilder();
         for (int i = 0; i < myMoves.length; i++) {
            final int oppIndex = moves.indexOf("" + oppMoves[i]);
            
            //Update database:
            if(history.length() == DEPTH) {
                final String lookup = history.toString();
                int[] distr = database.get(lookup);
                if(distr == null) {
                    distr = new int[5];
                }
                distr[oppIndex]++;
                database.put(lookup, distr);
                 history.delete(0, 1);
            }
            history.append(""+oppIndex);
 
        }
        
        String move = predict(history, database);
        if(move != null) {
            return move;
        } else {
            return moves.get(random.nextInt(moves.size()));
        }
    }
    
    public String predict(StringBuilder history, Map<String, int[]> database) {
        final String historicMoves = history.toString();
        final int[] distr = database.get(historicMoves);

        if(distr == null) {
            return null;
        }
        int bestIndex = 0;
        for(int i = 1;i<distr.length;i++) {
            if(distr[i] > distr[bestIndex]) {
                bestIndex = i;
            }
        }
        //Play move:
        return "" + beats[bestIndex].charAt(random.nextInt(2));
    }
}
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

// Aggressive predictor using own and opponent history
public class Gazzr {

    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", "PV", "RS", "PL", "RV" };
    private final List<String> losesFrom = Arrays.asList(new String[] { "PV", "RS", "PL", "RV", "LS" });
    private final Random random = new Random();

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

    public String play(final String[] args) {
        if (args == null || args.length == 0 || args[0].length() < DEPTH) {
            return moves.get(random.nextInt(moves.size()));
        }
        final Map<String, int[]> oppHistDb = new HashMap<String, int[]>();
        final Map<String, int[]> myHistDb = new HashMap<String, int[]>();
        final char[] myMoves = args[0].toCharArray();
        final char[] oppMoves = args[1].toCharArray();
        int i = Math.max(DEPTH, myMoves.length-50); // Limit to 50 previous moves
        while(i < myMoves.length) {
            updateDB(args[0].substring(i-DEPTH, i), oppMoves[i], myHistDb);
            updateDB(args[1].substring(i-DEPTH, i), oppMoves[i], oppHistDb);
            i++;
        }
        //Predict based on own and opponent moves:
        Integer p1 = predict(args[0].substring(i-DEPTH, i), myHistDb);
        Integer p2 = predict(args[1].substring(i-DEPTH, i), oppHistDb);
        if(p1 == null && p2 == null) {
            return moves.get(random.nextInt(moves.size()));
        }
        if(p2 == null) {
            return "" + beats[p1].charAt(random.nextInt(2));
        } else if(p1 == null) {
            return "" + beats[p2].charAt(random.nextInt(2));
        }
        String s1 = moves.get(p1)+moves.get(p2); //Try to find common ground
        String s2 = moves.get(p2)+moves.get(p1);
        if(losesFrom.contains(s1)) {
            return moves.get(losesFrom.indexOf(s1));
        }
        if(losesFrom.contains(s2)) {
            return moves.get(losesFrom.indexOf(s2));
        }
        if(random.nextBoolean()) {
            return ""+ beats[p1].charAt(random.nextInt(2));
        } else {
            return ""+ beats[p2].charAt(random.nextInt(2));
        }
    }
    
    private void updateDB(String lastMoves, char nextMove, Map<String, int[]> database) {
        int oppIndex = moves.indexOf("" + nextMove);
        int[] distr = database.get(lastMoves);
        if(distr == null) {
            distr = new int[5];
        }
        distr[oppIndex]++;
        database.put(lastMoves, distr);
    }
    
    public Integer predict(String historicMoves, Map<String, int[]> database) {
        final int[] distr = database.get(historicMoves);

        if(distr == null) {
            return null;
        }
        int bestIndex = 0;
        for(int i = 1;i<distr.length;i++) {
            if(distr[i] > distr[bestIndex]) {
                bestIndex = i;
            }
        }
        return bestIndex;
    }
}
removed smartypants non working code, turning this into a simple predictor
Source Link
Roy van Rijn
  • 1.1k
  • 9
  • 13
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 = 2;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", "PV", "RS", "PL", "RV" };
    private final List<String> losesFrom = Arrays.asList(new String[] { "PV", "RS", "PL", "RV", "LS" });
    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) {
            return moves.get(random.nextInt(moves.size()));
        }
        final Map<String, int[]> database = new HashMap<String, int[]>();
        final char[] myMoves = args[0].toCharArray();
        final char[] oppMoves = args[1].toCharArray();

        int score = 0;
        boolean losing = false;
        int playMode = 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]);

            String predictedMove = predict(history, database, playMode);

            if(predictedMove != null) {
                if (i > DEPTH && myMoves[i] != oppMoves[i]) {
                    final String beatingMoves = beats[myIndex];
                    if (beatingMoves.contains(oppMoves[i] + "")) {
                        score--;
                    } else {
                        score++;
                    }
                }
                if(!losing && score < -2) {
                    losing = true;
                    playMode++;
                } else if(losing && score > 0) {
                    losing = false;
                }
            }
            
            //Update database:
            if(history.length() == DEPTH) {
                final String lookup = history.toString();
                int[] distr = database.get(lookup);
                if(distr == null) {
                    distr = new int[5];
                }
                distr[oppIndex]++;
                database.put(lookup, distr);
                history.delete(0, 1);
            }
            history.append(""+oppIndex);

        }
        
        String move = predict(history, database, playMode);
        if(move != null) {
            return move;
        } else {
            return moves.get(random.nextInt(moves.size()));
        }
    }
    
    public String predict(StringBuilder history, Map<String, int[]> database, int playMode) {
        final String historicMoves = history.toString();
        final int[] distr = database.get(historicMoves);

        if(distr == null) {
            return null;
        }
        int bestIndex = 0;
        for(int i = 1;i<distr.length;i++) {
            if(distr[i] > distr[bestIndex]) {
                bestIndex = i;
            }
        }
        //Play move:
        if(playMode%2 == 0) {
            return "" + beats[bestIndex].charAt(random.nextInt(2));
        } else {
            //He's on to me, try to iocaine!
            String illPick = beats[bestIndex];
            int opponentMove = losesFrom.indexOf(illPick);
            return "" + beats[opponentMove].charAt(random.nextInt(2));
        }
    }
}
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 = 2; // <- 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", "PV", "RS", "PL", "RV" };
    private final List<String> losesFrom = Arrays.asList(new String[] { "PV", "RS", "PL", "RV", "LS" });
    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) {
            return moves.get(random.nextInt(moves.size()));
        }
        final Map<String, int[]> database = new HashMap<String, int[]>();
        final char[] myMoves = args[0].toCharArray();
        final char[] oppMoves = args[1].toCharArray();

        int score = 0;
        boolean losing = false;
        int playMode = 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]);

            String predictedMove = predict(history, database, playMode);

            if(predictedMove != null) {
                if (i > DEPTH && myMoves[i] != oppMoves[i]) {
                    final String beatingMoves = beats[myIndex];
                    if (beatingMoves.contains(oppMoves[i] + "")) {
                        score--;
                    } else {
                        score++;
                    }
                }
                if(!losing && score < -2) {
                    losing = true;
                    playMode++;
                } else if(losing && score > 0) {
                    losing = false;
                }
            }
            
            //Update database:
            if(history.length() == DEPTH) {
                final String lookup = history.toString();
                int[] distr = database.get(lookup);
                if(distr == null) {
                    distr = new int[5];
                }
                distr[oppIndex]++;
                database.put(lookup, distr);
                history.delete(0, 1);
            }
            history.append(""+oppIndex);

        }
        
        String move = predict(history, database, playMode);
        if(move != null) {
            return move;
        } else {
            return moves.get(random.nextInt(moves.size()));
        }
    }
    
    public String predict(StringBuilder history, Map<String, int[]> database, int playMode) {
        final String historicMoves = history.toString();
        final int[] distr = database.get(historicMoves);

        if(distr == null) {
            return null;
        }
        int bestIndex = 0;
        for(int i = 1;i<distr.length;i++) {
            if(distr[i] > distr[bestIndex]) {
                bestIndex = i;
            }
        }
        //Play move:
        if(playMode%2 == 0) {
            return "" + beats[bestIndex].charAt(random.nextInt(2));
        } else {
            //He's on to me, try to iocaine!
            String illPick = beats[bestIndex];
            int opponentMove = losesFrom.indexOf(illPick);
            return "" + beats[opponentMove].charAt(random.nextInt(2));
        }
    }
}
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", "PV", "RS", "PL", "RV" };
    private final Random random = new Random();

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

    String play(final String[] args) {
        if (args == null || args.length == 0 || args[0].length() < DEPTH) {
            return moves.get(random.nextInt(moves.size()));
        }
        final Map<String, int[]> database = new HashMap<String, int[]>();
        final char[] myMoves = args[0].toCharArray();
        final char[] oppMoves = args[1].toCharArray();
        final StringBuilder history = new StringBuilder();
        for (int i = 0; i < myMoves.length; i++) {
            final int oppIndex = moves.indexOf("" + oppMoves[i]);
            
            //Update database:
            if(history.length() == DEPTH) {
                final String lookup = history.toString();
                int[] distr = database.get(lookup);
                if(distr == null) {
                    distr = new int[5];
                }
                distr[oppIndex]++;
                database.put(lookup, distr);
                history.delete(0, 1);
            }
            history.append(""+oppIndex);

        }
        
        String move = predict(history, database);
        if(move != null) {
            return move;
        } else {
            return moves.get(random.nextInt(moves.size()));
        }
    }
    
    public String predict(StringBuilder history, Map<String, int[]> database) {
        final String historicMoves = history.toString();
        final int[] distr = database.get(historicMoves);

        if(distr == null) {
            return null;
        }
        int bestIndex = 0;
        for(int i = 1;i<distr.length;i++) {
            if(distr[i] > distr[bestIndex]) {
                bestIndex = i;
            }
        }
        //Play move:
        return "" + beats[bestIndex].charAt(random.nextInt(2));
    }
}
deleted 52 characters in body
Source Link
Roy van Rijn
  • 1.1k
  • 9
  • 13
Loading
added 7 characters in body
Source Link
Roy van Rijn
  • 1.1k
  • 9
  • 13
Loading
Source Link
Roy van Rijn
  • 1.1k
  • 9
  • 13
Loading