Skip to content

Commit b51a026

Browse files
authored
My Mastermind in Java
1 parent 999c290 commit b51a026

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

GameLoop.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.lang.Math;
2+
import java.util.Scanner;
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Iterator;
6+
public class GameLoop {
7+
public static final String YELLOW = "\u001B[33m";
8+
public static final String RED = "\u001B[31m";
9+
public static final String GREEN = "\u001B[32m";
10+
public static final String WHITE = "\u001B[37m";
11+
public static final String PURPLE = "\u001B[35m";
12+
private int rounds;
13+
private String secretCode;
14+
private MastermindAlgorithm mastermindAlgorithm;
15+
16+
public GameLoop(int rounds, String secretCode) {
17+
this.rounds = rounds;
18+
this.secretCode = secretCode;
19+
}
20+
21+
public void run() {
22+
int round = 1;
23+
String inputCode;
24+
System.out.println(GREEN + "Will you find the secret code?");
25+
Scanner input = new Scanner(System.in);
26+
while (round <= rounds) {
27+
System.out.printf(YELLOW + "-------🏁 Round %d/%d 🏁 -------\n" + PURPLE + " ", round, rounds);
28+
inputCode = input.nextLine();
29+
this.mastermindAlgorithm = new MastermindAlgorithm(
30+
this.secretCode,
31+
inputCode
32+
);
33+
if(GameFunctions.wrongInput(inputCode)) {
34+
System.out.println(RED + "------❌ Wrong input!❌ -------");
35+
continue;
36+
} else if (secretCode.equals(inputCode)) {
37+
System.out.println(GREEN + "Congrats! You did it!🥳 🥳 🥳");
38+
break;
39+
} else {
40+
mastermindAlgorithm.wellPiecesAlgo();
41+
mastermindAlgorithm.misPiecesAlgo();
42+
}
43+
if (round == this.rounds)
44+
System.out.printf(RED + "-------❌ Game Over ❌ -------\n"+ GREEN +"----- Secret code: %s -----\n", this.secretCode);
45+
round++;
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)