Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.25 KB | None | 0 0
  1. public class Main {
  2.  
  3.     private static final Integer[] INITIAL_GUESS = {1, 1, 2, 2};
  4.  
  5.     public static void main(String[] args) {
  6.         Main main = new Main();
  7.         main.mastermind();
  8.     }
  9.  
  10.     public void mastermind() {
  11.         Scanner reader = new Scanner(System.in);
  12.         int tries = 0;
  13.         Set<Integer[]> combinations = this.fillCombinations(6);
  14.         Integer[] guess = INITIAL_GUESS.clone();
  15.         int redPins, whitePins;
  16.         while (true) {
  17.             if (combinations.size() <= 1) {
  18.                 System.out.println(String.format("GOT IT! Guesses: %d Its %s", tries, Arrays.toString(guess)));
  19.                 break;
  20.             }
  21.             System.out.println("My Guess: " + Arrays.toString(guess));
  22.             System.out.println("Nr of red pins: ");
  23.             redPins = reader.nextInt();
  24.             System.out.println("Nr of white pins: ");
  25.             whitePins = reader.nextInt();
  26.             System.out.println(String.format("Red Pins: %d. White pins: %d.", redPins, whitePins));
  27.             combinations = guessBestSet(combinations, guess, redPins, whitePins);
  28.             guess = combinations.iterator().next();
  29.             ++tries;
  30.         }
  31.     }
  32.  
  33.     private Set<Integer[]> guessBestSet(Set<Integer[]> fullSet, Integer[] myGuess, int redResult, int whiteResult) {
  34.         Set<Integer[]> combinations = new LinkedHashSet<>(fullSet);
  35.         Set<Set<Integer[]>> combinationSets = new LinkedHashSet<>();
  36.         Set<Integer[]> bestSet;
  37.         combinations.removeIf(integers -> !this.compareResults(integers, myGuess, redResult, whiteResult));
  38.         for (int i = 0; i < 4; i++) {
  39.             for (int j = 0; j < 4; j++) {
  40.                 Set<Integer[]> set = new LinkedHashSet<>(combinations);
  41.                 int red = i;
  42.                 int white = j;
  43.                 set.removeIf(code -> this.compareResults(code, myGuess, red, white));
  44.                 if (set.size() > 0)combinationSets.add(set);
  45.             }
  46.         }
  47.         if (combinationSets.size() > 1) {
  48.             bestSet = combinationSets.stream().min(Comparator.comparing(Set::size)).get();
  49.             return bestSet;
  50.         }
  51.         return combinations;
  52.     }
  53.  
  54.     public boolean compareResults(Integer[] toCompare, Integer[] answer, int redResult, int whiteResult) {
  55.         Integer[] toCompareCopy = toCompare.clone();
  56.         Integer[] answerCopy = answer.clone();
  57.         boolean resR = this.hasSameRedPins(toCompareCopy, answerCopy, redResult);
  58.         boolean resW = this.hasSameWhitePins(toCompareCopy, answerCopy, whiteResult);
  59.         return resR && resW;
  60.     }
  61.  
  62.     private boolean hasSameRedPins(Integer[] toCompare, Integer[] answer, int redPinRef) {
  63.         int pins = 0;
  64.         for (int j = 0; j < toCompare.length; j++) {
  65.             if (toCompare[j].equals(answer[j])) {
  66.                 toCompare[j] = null;
  67.                 answer[j] = null;
  68.                 ++pins;
  69.             }
  70.         }
  71.         return pins == redPinRef;
  72.     }
  73.  
  74.     private boolean hasSameWhitePins(Integer[] toCompare, Integer[] answer, int whitePinRef) {
  75.         int pins = 0;
  76.         for (int j = 0; j < toCompare.length; ++j) {
  77.             for (int i = 0; i < toCompare.length; ++i) {
  78.                 if (answer[j] != null && toCompare[i] != null) {
  79.                     if (i != j) {
  80.                         if (answer[j].equals(toCompare[i])) {
  81.                             toCompare[i] = null;
  82.                             answer[j] = null;
  83.                             ++pins;
  84.                             break;
  85.                         }
  86.                     }
  87.                 }
  88.             }
  89.         }
  90.         return pins == whitePinRef;
  91.     }
  92.  
  93.     private Set<Integer[]> fillCombinations(int numberOfColors) { // TODO what if row is > 4?
  94.         Set<Integer[]> result = new LinkedHashSet<>();
  95.         for (int n1 = 1; n1 <= numberOfColors; ++n1) {
  96.             for (int n2 = 1; n2 <= numberOfColors; ++n2) {
  97.                 for (int n3 = 1; n3 <= numberOfColors; ++n3) {
  98.                     for (int n4 = 1; n4 <= numberOfColors; ++n4) {
  99.                         Integer[] arr = {n1, n2, n3, n4};
  100.                         result.add(arr);
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.         return result;
  106.     }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement