Facebook
From aseruneko, 3 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 130
  1. package cantstop;
  2.  
  3. import java.util.Comparator;
  4. import java.util.Random;
  5. import java.util.stream.Stream;
  6.  
  7. public class CantStop {
  8.  
  9.     static final int[] ROAD_LENGTH = {0,0,3,5,7,9,11,13,11,9,7,5,3};
  10.     static final int TRYING = 10000;
  11.     static int goaled = 0;
  12.  
  13.     public static void main(String[] args){
  14.  
  15.         for(int i = 0; i < TRYING; i ++){
  16.             boolean isGoal = false;
  17.             boolean isGameOver = false;
  18.             int round = 0;
  19.             int[] dice;
  20.             int[][] dividedDice;
  21.             int[] chosenDice;
  22.             int[][] chosenRoad = {{-1,0},{-1,0},{-1,0}};
  23.  
  24.             round ++;
  25. //            System.out.println("[ROUND " + round + "]");
  26.             dice = diceRoll();
  27.             dividedDice = divideDice(dice);
  28.             chosenDice = initialChoice(dividedDice);
  29.             chosenRoad = solveDice(chosenRoad, chosenDice);
  30.  
  31. //            printDice(chosenDice);
  32. //            printProgress(chosenRoad);
  33.  
  34.             while(!isGoal && !isGameOver) {
  35.                 round ++;
  36. //                System.out.println("[ROUND " + round + "]");
  37.                 dice = diceRoll();
  38.                 dividedDice = divideDice(dice);
  39.                 chosenDice = nonInitialChoice(chosenRoad, dividedDice);
  40.                 if(chosenDice == null){
  41. //                    System.out.println("GAMEOVER");
  42.                     isGameOver = true;
  43.                 } else {
  44.                     chosenRoad = solveDice(chosenRoad, chosenDice);
  45.  
  46. //                    printDice(chosenDice);
  47. //                    printProgress(chosenRoad);
  48.                 }
  49.  
  50.                 isGoal = isGoal(chosenRoad);
  51.                 if(isGoal) goaled++;
  52.  
  53.             }
  54.         }
  55.         System.out.println(goaled + "/" + TRYING);
  56.         System.out.println((float)goaled / TRYING * 100 + "%");
  57.  
  58.  
  59.  
  60.     }
  61.  
  62.     private static int[] diceRoll(){
  63.         Random rand = new Random();
  64.         return new int[]{
  65.                 rand.nextInt(6) + 1,
  66.                 rand.nextInt(6) + 1,
  67.                 rand.nextInt(6) + 1,
  68.                 rand.nextInt(6) + 1
  69.         };
  70.     }
  71.  
  72.     private static int[][] divideDice(int[] dice){
  73.         return new int[][] {
  74.                 {dice[0] + dice[1], dice[2] + dice[3]},
  75.                 {dice[0] + dice[2], dice[1] + dice[3]},
  76.                 {dice[0] + dice[3], dice[1] + dice[2]}
  77.         };
  78.     }
  79.  
  80.     private static int[] initialChoice(int[][] dividedDice){
  81.         return Stream.of(dividedDice)
  82.                 .min(Comparator
  83.                     .<int[], Integer>comparing(a -> (Math.abs(a[0] - 7) + Math.abs(a[1] - 7)))
  84.                     .thenComparing( a -> (Math.abs(a[0] - a[1])))
  85.                 )
  86.                 .orElse(null);
  87.     }
  88.  
  89.     private static int[][] solveDice(int[][] chosenRoad, int[] chosenDice){
  90.         for(int i = 0; i < 2; i ++){
  91.             for(int j = 0; j < chosenRoad.length; j ++){
  92.                 if(chosenRoad[j][0] == chosenDice[i] || chosenRoad[j][0] == -1){
  93.                     chosenRoad[j][0] = chosenDice[i];
  94.                     chosenRoad[j][1] ++;
  95.                     break;
  96.                 }
  97.             }
  98.         }
  99.         return chosenRoad;
  100.     }
  101.  
  102.     private static int[] nonInitialChoice(int[][] chosenRoad, int[][] dividedDice){
  103.         int[] matchRoad = {0,0,0};
  104.         for(int i = 0; i < dividedDice.length; i++){
  105.             for(int[] road : chosenRoad){
  106.                 if (road[0] == -1){
  107.                     matchRoad[i]++;
  108.                 } else {
  109.                     for(int dice : dividedDice[i]){
  110.                         if (road[0] == dice) matchRoad[i] += 5;
  111.                     }
  112.                 }
  113.             }
  114.         }
  115.         if(matchRoad[0] + matchRoad[1] + matchRoad[2] == 0){
  116.             return null;
  117.         }else{
  118.             int maxId = 0;
  119.             for(int i = 0; i < matchRoad.length; i++){
  120.                 maxId = matchRoad[i] > matchRoad[maxId] ? i : maxId;
  121.             }
  122.             return dividedDice[maxId];
  123.         }
  124.     }
  125.  
  126.     private static boolean isGoal(int[][] chosenRoad){
  127.         return Stream.of(chosenRoad)
  128.                 .filter(road -> road[0] >= 0)
  129.                 .anyMatch(road -> road[1] >= ROAD_LENGTH[road[0]]);
  130.     }
  131.  
  132.     private static void printProgress(int[][] chosenRoad){
  133.         String output = "";
  134.         for(int[] road : chosenRoad){
  135.             output += road[0] + ":" + road[1] + ",";
  136.         }
  137.         System.out.println(output);
  138.     }
  139.  
  140.     private static void printDice(int[] chosenDice){
  141.         String output = "";
  142.         for(int dice : chosenDice){
  143.             output += dice + ",";
  144.         }
  145.         System.out.println(output);
  146.     }
  147.  
  148.  
  149. }
  150.