-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMasterMind.java
More file actions
88 lines (66 loc) · 2.47 KB
/
MasterMind.java
File metadata and controls
88 lines (66 loc) · 2.47 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
/**
*@param args
*@author Mirghani
*@course CPSC 220
*@section 1
*@description This programis a game in which the user must select
*the correct order and color of the pegs
*/
import java.util.Scanner;
class MasterMind {
public static void main (String [] args) {
final int MAX_ATTEMPTS = 10;
Scanner scanner = new Scanner(System.in);
SecretPattern secretPegs, secretPegsCopy, guessPegs;
String guess;
int attempt = 0;
int patternCorrect = 0, colorCorrect = 0;
System.out.println("Welcome to MasterMind!");
System.out.println("Enter a string consisting of 4 letters, which can be");
System.out.println("R (Red), B (Blue), G (Green), Y (Yellow), C (Cyan) or M (Magenta).");
System.out.println("Enjoy!");
secretPegs = new SecretPattern();
secretPegs.generateSecretCode();
//Uncomment next line to see the pattern!
System.out.println(secretPegs.toString());
secretPegsCopy = new SecretPattern();
secretPegsCopy.setColorPeg1(secretPegs.getColorPeg1());
secretPegsCopy.setColorPeg2(secretPegs.getColorPeg2());
secretPegsCopy.setColorPeg3(secretPegs.getColorPeg3());
secretPegsCopy.setColorPeg4(secretPegs.getColorPeg4());
//System.out.println(secretPegsCopy.toString());
do {
attempt += 1;
secretPegsCopy.setColorPeg1(secretPegs.getColorPeg1());
secretPegsCopy.setColorPeg2(secretPegs.getColorPeg2());
secretPegsCopy.setColorPeg3(secretPegs.getColorPeg3());
secretPegsCopy.setColorPeg4(secretPegs.getColorPeg4());
System.out.print("Enter your guess: ");
guess = scanner.nextLine();
while (guess.length() != 4) {
System.out.println("Incorrect guess!");
System.out.print("Enter your guess: ");
guess = scanner.nextLine();
}
guessPegs = new SecretPattern();
guessPegs.setColorPeg1(guess.charAt(0));
guessPegs.setColorPeg2(guess.charAt(1));
guessPegs.setColorPeg3(guess.charAt(2));
guessPegs.setColorPeg4(guess.charAt(3));
patternCorrect = secretPegs.countPattern(guessPegs);
colorCorrect = secretPegs.countColors(guessPegs);
System.out.print("Colors correct: " + colorCorrect + "; ");
System.out.print("Pattern correct: " + patternCorrect);
System.out.println(" ");
System.out.println(" ");
} while (attempt < MAX_ATTEMPTS && patternCorrect < 4);
if(patternCorrect == 4)
{
System.out.print("You Won! Congratulations");
}
else
{
System.out.print("You Lost! Try Again!");
}
}
}