bulls and cows

Solutions on MaxInterview for bulls and cows by the best coders in the world

showing results for - "bulls and cows"
Moritz
10 Jan 2021
1import java.util.*;
2class Main {
3    static ArrayList<String> possib = new ArrayList<String>();
4    public static void main(String args[]) {
5        System.out.println("COWS AND BULLS SOLVER:");
6        init();
7        Scanner in = new Scanner(System.in);
8        while (possib.size() > 1) {
9            String guess = possib.get((int)(Math.random() * possib.size()));
10            System.out.print(guess+" ");
11            int bulls = in.nextInt();
12            int cows = in.nextInt();
13            for (int j = 0; j < possib.size(); j++)
14                if (!check(possib.get(j),guess,cows,bulls)) {
15                    possib.remove(j);
16                    j--;
17                }
18        }
19        for (int j = 0; j < possib.size(); j++)
20            System.out.println(possib.get(j));
21    }
22    
23    static void init() {
24        for (int a = 1; a <= 9; a++) { 
25            for (int b = 1; b <= 9; b++) {
26                if (b == a) continue;
27                for (int c = 1; c <= 9; c++) {
28                    if (c == b || c == a) continue;
29                    for (int d = 1; d <= 9; d++) {
30                        if (d == a || d == b || d == c) continue;
31                        String cnt = ""+a+b+c+d;
32                        possib.add(cnt);
33                    }
34                }
35            }
36        }
37    }
38    
39    static boolean check(String ans,String guess,int cows,int bulls) {
40        for (int i = 0; i < guess.length(); i++) {
41            int ind = ans.indexOf(guess.charAt(i));
42            if (ind == i) bulls--;
43            else if (ind >= 0) cows--;
44        }
45        return (cows == 0) && (bulls == 0);
46    }
47}
48
Isidora
19 May 2019
1import java.util.*;
2class Main {
3    public static void main(String args[]) {
4        final String target = getNum();
5        System.out.println("COWS AND BULLS:");
6        Scanner in = new Scanner(System.in);
7        for (int i = 1; i <= 7; i++) {
8            System.out.print(i+". ");
9            String guess = in.next();
10            int feedback = feedback(target, guess);
11            System.out.println(guess+" - "+(feedback/10)+" bulls, "+(feedback%10)+" cows");
12            if (feedback == 40) {System.out.println("CONGRATULATIONS! YOU WIN!"); return;}
13        }
14        System.out.println("You have run out of moves. The number was - "+target);
15    }
16
17    static String getNum() {
18        ArrayList<String> possib = new ArrayList<String>();
19        for (int a = 1; a <= 9; a++) { 
20            for (int b = 1; b <= 9; b++) {
21                if (b == a) continue;
22                for (int c = 1; c <= 9; c++) {
23                    if (c == b || c == a) continue;
24                    for (int d = 1; d <= 9; d++) {
25                        if (d == a || d == b || d == c) continue;
26                        String cnt = ""+a+b+c+d;
27                        possib.add(cnt);
28                    }
29                }
30            }
31        }
32        String guess = possib.get((int)(Math.random() * possib.size()));
33        return guess;
34    }
35
36    static int feedback(String ans,String guess) {
37        int bulls = 0, cows = 0;
38        for (int i = 0; i < guess.length(); i++) {
39            int ind = ans.indexOf(guess.charAt(i));
40            if (ind == i) bulls++;
41            else if (ind >= 0) cows++;
42        }
43        return bulls * 10 + cows;
44    }
45}
46