Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- private static final Integer[] INITIAL_GUESS = {1, 1, 2, 2};
- public static void main(String[] args) {
- Main main = new Main();
- main.mastermind();
- }
- public void mastermind() {
- Scanner reader = new Scanner(System.in);
- int tries = 0;
- Set<Integer[]> combinations = this.fillCombinations(6);
- Integer[] guess = INITIAL_GUESS.clone();
- int redPins, whitePins;
- while (true) {
- if (combinations.size() <= 1) {
- System.out.println(String.format("GOT IT! Guesses: %d Its %s", tries, Arrays.toString(guess)));
- break;
- }
- System.out.println("My Guess: " + Arrays.toString(guess));
- System.out.println("Nr of red pins: ");
- redPins = reader.nextInt();
- System.out.println("Nr of white pins: ");
- whitePins = reader.nextInt();
- System.out.println(String.format("Red Pins: %d. White pins: %d.", redPins, whitePins));
- combinations = guessBestSet(combinations, guess, redPins, whitePins);
- guess = combinations.iterator().next();
- ++tries;
- }
- }
- private Set<Integer[]> guessBestSet(Set<Integer[]> fullSet, Integer[] myGuess, int redResult, int whiteResult) {
- Set<Integer[]> combinations = new LinkedHashSet<>(fullSet);
- Set<Set<Integer[]>> combinationSets = new LinkedHashSet<>();
- Set<Integer[]> bestSet;
- combinations.removeIf(integers -> !this.compareResults(integers, myGuess, redResult, whiteResult));
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 4; j++) {
- Set<Integer[]> set = new LinkedHashSet<>(combinations);
- int red = i;
- int white = j;
- set.removeIf(code -> this.compareResults(code, myGuess, red, white));
- if (set.size() > 0)combinationSets.add(set);
- }
- }
- if (combinationSets.size() > 1) {
- bestSet = combinationSets.stream().min(Comparator.comparing(Set::size)).get();
- return bestSet;
- }
- return combinations;
- }
- public boolean compareResults(Integer[] toCompare, Integer[] answer, int redResult, int whiteResult) {
- Integer[] toCompareCopy = toCompare.clone();
- Integer[] answerCopy = answer.clone();
- boolean resR = this.hasSameRedPins(toCompareCopy, answerCopy, redResult);
- boolean resW = this.hasSameWhitePins(toCompareCopy, answerCopy, whiteResult);
- return resR && resW;
- }
- private boolean hasSameRedPins(Integer[] toCompare, Integer[] answer, int redPinRef) {
- int pins = 0;
- for (int j = 0; j < toCompare.length; j++) {
- if (toCompare[j].equals(answer[j])) {
- toCompare[j] = null;
- answer[j] = null;
- ++pins;
- }
- }
- return pins == redPinRef;
- }
- private boolean hasSameWhitePins(Integer[] toCompare, Integer[] answer, int whitePinRef) {
- int pins = 0;
- for (int j = 0; j < toCompare.length; ++j) {
- for (int i = 0; i < toCompare.length; ++i) {
- if (answer[j] != null && toCompare[i] != null) {
- if (i != j) {
- if (answer[j].equals(toCompare[i])) {
- toCompare[i] = null;
- answer[j] = null;
- ++pins;
- break;
- }
- }
- }
- }
- }
- return pins == whitePinRef;
- }
- private Set<Integer[]> fillCombinations(int numberOfColors) { // TODO what if row is > 4?
- Set<Integer[]> result = new LinkedHashSet<>();
- for (int n1 = 1; n1 <= numberOfColors; ++n1) {
- for (int n2 = 1; n2 <= numberOfColors; ++n2) {
- for (int n3 = 1; n3 <= numberOfColors; ++n3) {
- for (int n4 = 1; n4 <= numberOfColors; ++n4) {
- Integer[] arr = {n1, n2, n3, n4};
- result.add(arr);
- }
- }
- }
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement