-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmastermind.java
More file actions
96 lines (82 loc) · 3.9 KB
/
mastermind.java
File metadata and controls
96 lines (82 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package mastermind;
import java.util.Scanner;
public class mastermind {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] colors = {"red", "green", "orange", "yellow", "purple", "blue"};
String[] answer = new String[4];
boolean gameOver = false;
int turn = 1;
// Genereer een willekeurige code
for (int i = 0; i < 4; i++) {
int rand = (int) (Math.random() * colors.length);
answer[i] = colors[rand];
}
// (optioneel) print de juiste code om te testen
System.out.println("DEBUG (secret code): " + String.join(" ", answer));
System.out.println("------------------------------------------------------------------------------------------------");
System.out.println("Welcome to Mastermind!");
System.out.println("Each round you get 2 hints:");
System.out.println("'Black' = correct color in correct place");
System.out.println("'White' = correct color in wrong place");
System.out.println("You can choose from the colors: Red, Green, Yellow, Orange, Purple, Blue");
System.out.println("------------------------------------------------------------------------------------------------");
System.out.println();
System.out.println("<---- GUESS THE CODE AND GOODLUCK ---->");
System.out.println();
while (!gameOver && turn <= 10) {
System.out.println("Turn " + turn + ": Choose 4 colors separated by spaces");
String input = scanner.nextLine();
String[] guess = input.trim().split("\\s+");
if (guess.length != 4) {
System.out.println("Please enter exactly 4 colors!\n");
continue;
}
int black = 0;
int white = 0;
boolean[] answerMatched = new boolean[4];
boolean[] guessMatched = new boolean[4];
// Eerst: zwarte pinnen (juiste kleur, juiste plek)
for (int i = 0; i < 4; i++) {
if (guess[i].equalsIgnoreCase(answer[i])) {
black++;
answerMatched[i] = true;
guessMatched[i] = true;
System.out.println("Position " + (i + 1) + " (" + guess[i] + ") is BLACK");
}
}
// Dan: witte pinnen (juiste kleur, verkeerde plek)
for (int i = 0; i < 4; i++) {
if (!guessMatched[i]) {
boolean foundWhite = false;
for (int j = 0; j < 4; j++) {
if (!answerMatched[j] && guess[i].equalsIgnoreCase(answer[j])) {
white++;
answerMatched[j] = true;
foundWhite = true;
System.out.println("Position " + (i + 1) + " (" + guess[i] + ") is WHITE");
break;
}
}
if (!foundWhite) {
System.out.println("Position " + (i + 1) + " (" + guess[i] + ") is NONE");
}
}
}
// Samenvatting van de beurt
System.out.println("\nBlack pins: " + black);
System.out.println("White pins: " + white + "\n");
// Win check
if (black == 4) {
System.out.println("<---- You have guessed the correct sequence! ---->");
gameOver = true;
} else if (turn == 10) {
System.out.println("GAME OVER! You have used all your turns!");
System.out.println("The correct sequence was: " + String.join(" ", answer));
gameOver = true;
}
turn++;
}
scanner.close();
}
}