-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mastermind.java
93 lines (74 loc) · 3.27 KB
/
Mastermind.java
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
import java.util.*;
public class MasterMind {
public static void main(String[] args) {
//inp and rand declarations.
String[] colors = {"r","y","b","g","w","o"};
String[] inp = new String[4];
String[] rand = new String[4];
//populate the rand array with random colors from colors.
for (int i = 0; i < rand.length; i++) {
int val = new Random().nextInt(colors.length);
rand[i] = colors[val];
}
//new scanner declaration.
Scanner console = new Scanner(System.in);
//welcome messages
System.out.println("Welcome to the Mastermind game!");
System.out.println("You have 10 guesses to guess the randomly generated combination of the following six colors: \n red(r) yellow(y) blue(b) green(g) white(w) orange(o)");
//method call that starts the entire game.
enterColors(inp, rand, console);
}
public static void enterColors(String[] inp, String[] rand, Scanner console) {
//for loop to enter each guess; 10 guess limit.
int guess = 1;
System.out.print("This is guess " + guess + " out of 10" + "\n");
while (guess <= 10) {
for (int i = 0; i < inp.length; i++) {
System.out.print("Enter value " + (i + 1) + ": ");
inp[i] = console.next();
System.out.println("");
}
guess++;
checkPos(rand, inp); //method that checks the guesses
//loser sequence initiates if user does not win in the guess limit; and correct values are printed.
if (guess > 10) {
System.out.println("");
System.out.print("you lose" + " here is the right answer: " + Arrays.toString(rand));
System.exit(0);
}
//prints after each guess to remind them of the options and the amount of guesses taken.
System.out.println("This is guess " + (guess) + " out of 10" + "\n" + "red(r) yellow(y) blue(b) green(g) white(w) orange(o)" + "\n");
}
}
public static void checkPos(String[] rand, String[] inp) {
//red pegs, white pegs, and blank variable declarations
int red = 0;
int white = 0;
int blank = 0;
int count = 0;
boolean x = false;
//win statement
if (Arrays.equals(rand, inp)) {
System.out.println("YOU WIN!");
System.exit(0); //exit statement after game is won
}
//red and white pegs calculations
for (int i = 0; i < inp.length; i++) {
if (inp[i].equals(rand[i])) {
red++;
}
else {
for (int y = 0; y < rand.length; y++) {
if (inp[i].equals(rand[y])) {
white++;
break;
}
}
}
}
blank = 4 - red - white; //blank pegs calculation
System.out.println("");
//tells the user information about their guess. After these print, the next try will appear.
System.out.println(red + " red pegs, " + white + " white pegs, and " + blank + " blank spaces.");
}
}