|
| 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