• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

doppelganger9 / mastermind / #173

11 Apr 2024 07:28PM UTC coverage: 0.0%. First build
#173

push

web-flow
Merge a15e8f6c6 into cbbf3a44e

0 of 13 new or added lines in 3 files covered. (0.0%)

0 of 91 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/com/doppelganger9/mastermind/App.java
1
package com.doppelganger9.mastermind;
2

3
import java.io.BufferedReader;
4
import java.io.IOException;
5
import java.io.InputStreamReader;
6
import java.util.ArrayList;
7
import java.util.List;
8
import java.util.stream.Collectors;
9
import java.util.stream.Stream;
10

11
/**
12
 * Java Mastermind game though STDIN/STDOUT
13
 */
NEW
14
class App {
×
15

16
    private static String solution;
17
    private static List<Turn> turns;
18

19
    void main() {
20

21
        turns = new ArrayList<Turn>();
×
22
        solution = generateRandomSolution();
×
23

24
        // game loop
25
        int n = 0;
×
26
        BufferedReader br = null;
×
27
        boolean win = false;
×
28
        try {
29
            br = new BufferedReader(new InputStreamReader(System.in));
×
30

31
            while (n < Turn.MAX_NB_TURNS && !win) {
×
32
                System.out.println(grid(turns, n));
×
33
                System.out.print("Your turn! enter a combination of 4 colors in [R,B,V,J,O,N] or 'q' to quit : ");
×
34
                String input = br.readLine();
×
35

36
                final Turn t = new Turn(input, 0, 0, n);
×
37

38
                // parse input
39
                if ("q".equals(input)) {
×
40
                    System.out.println("Exit!");
×
41
                    System.exit(0);
×
42
                }
43
                if (input == null || input.length() < 4) {
×
44
                    System.out.print("Please enter a combination of 4 colors in [R,B,V,J,O,N] or 'q' to quit, you entered : '" + input + "'\n");
×
45
                    continue;
×
46
                }
47

48
                turns.add(t);
×
49

50
                System.out.println("input : " + input);
×
51
                System.out.println("-----------\n");
×
52

53
                turns.set(n, computeGameAnswer(n, turns));
×
NEW
54
                if(t.nbCorrect() == solution.length()) {
×
55
                    win = true;
×
56
                }
57
                n++;
×
58
            }
×
59
            System.out.println(grid(turns, n));
×
60

61
            if (win) {
×
62
                System.out.println("You won, congratulations!");
×
63
            } else {
64
                System.out.println("You lost.");
×
65
            }
66

67
        } catch (IOException e) {
×
68
            e.printStackTrace();
×
69
        } finally {
70
            if (br != null) {
×
71
                try {
72
                    br.close();
×
73
                } catch (IOException e) {
×
74
                    e.printStackTrace();
×
75
                }
×
76
            }
77
        }
78
    }
×
79

80
    protected static String generateRandomSolution() {
81
        String s = "";
×
82
        for (int i = 0; i<4; i++) {
×
NEW
83
            int index = Double.valueOf(Math.floor((Math.random() * ColorEnum.values().length))).intValue();
×
84
            ColorEnum c = ColorEnum.values()[index];
×
85
            s += c.name();
×
86
        }
87
                return s;
×
88
        }
89

90
        protected static Turn computeGameAnswer(int n, List<Turn> turns) {
91
        Turn current = turns.get(n);
×
NEW
92
        return current.toBuilder() // Turn is a record, so it is immutable, we need to recreate a new one to mutate it.
×
NEW
93
            .nbCorrect(countCorrectPositionsAndColors(solution, current.userInput()))
×
NEW
94
            .nbMisplaced(countMisplacedColors(solution, current.userInput()))
×
NEW
95
            .build();
×
96
    }
97

98
    protected static int countMisplacedColors(String expected, String given) {
99
        int total = 0;
×
100
        try {
101
            String dedup = Stream.of(expected.split("")).distinct().collect(Collectors.joining(""));
×
102
            for (int i = 0; i < dedup.length(); i++) {
×
103
                if (given.contains(dedup.substring(i,i+1))) {
×
104
                    total++;
×
105
                }
106
            }
107
        } catch (Exception e) {
×
108
            // to quickly handle all edge cases... sorry ;-P
109
            e.printStackTrace();
×
110
        }
×
111
        return total;
×
112
    }
113

114
    protected static int countCorrectPositionsAndColors(String expected, String given) {
115
        int total = 0;
×
116
        try {
117
            for (int i = 0; i < expected.length(); i++) {
×
118
                if (expected.charAt(i) == given.charAt(i)) {
×
119
                    total ++;
×
120
                }
121
            }
122
        } catch (Exception e) {
×
123
            // to quickly handle all edge cases... sorry ;-P
124
            e.printStackTrace();
×
125
        }
×
126
        return total;
×
127
    }
128

129
    protected static String grid(List<Turn> turns, int n) {
130
        String s = "|-------------------|\n";
×
131
        for (Turn turn : turns) {
×
132
            s += turn.toBoardLineOutput();
×
133
        }
×
134
        if (n < Turn.MAX_NB_TURNS) {
×
135
            s+= "|....| . | . | " + (n+1) + "/10 |\n";
×
136
        }
137
        return s + "|-------------------|\n";
×
138
    }
139
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc