├── GameController.java ├── GameFunctions.java ├── GameLoop.java ├── Makefile ├── MastermindAlgorithm.java ├── My_Mastermind.java └── README.md /GameController.java: -------------------------------------------------------------------------------- 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 GameController { 7 | public static final String YELLOW = "\u001B[33m"; 8 | public static final String RED = "\u001B[31m"; 9 | private String[] args; 10 | private String secretCode; 11 | private int rounds; 12 | 13 | public GameController(String[] args) { 14 | this.args = args; 15 | } 16 | 17 | public String getSecretCode() { 18 | return this.secretCode; 19 | } 20 | 21 | public int getRounds() { 22 | return this.rounds; 23 | } 24 | 25 | public void initialize() { 26 | if (this.args.length > 1){ 27 | ArrayList option_list = new ArrayList(); 28 | option_list.addAll(Arrays.asList(this.args)); 29 | Iterator itr = null; 30 | itr = option_list.listIterator(); 31 | while (itr.hasNext()) { 32 | String option = itr.next(); 33 | if (option.equals("-t")) { 34 | try { 35 | this.rounds = Integer.parseInt(itr.next()); 36 | if(this.rounds < 0 && -15 < this.rounds) { 37 | this.rounds *= -1; 38 | System.out.printf(YELLOW + "Rounds number set: %d (not -%d)\n", this.rounds, this.rounds); 39 | } else if(this.rounds > 15 || this.rounds < 1){ 40 | System.out.println(RED + "Rounds cannot be more then 15 or less then 1 (15 ≥ rounds ≥ 1)"); 41 | this.rounds = 10; 42 | } 43 | } catch (Exception e) { 44 | System.out.println(YELLOW + "You should type number of rounds! (-t)\n"); 45 | }; 46 | } 47 | 48 | if (option.equals("-c")) { 49 | String code = itr.next(); 50 | if(GameFunctions.wrongInput(code)) { 51 | System.out.printf(RED + "Error in setting secret code: ( -c %s )\n" + 52 | "Secret code set automatic by 'generator'\n", code); 53 | this.secretCode = GameFunctions.getSecretCode(); 54 | } else { 55 | this.secretCode = code; 56 | } 57 | } 58 | } 59 | } 60 | if (this.secretCode == null) { 61 | this.secretCode = GameFunctions.getSecretCode(); 62 | } 63 | if (this.rounds == 0) { 64 | this.rounds = 10; 65 | } 66 | } 67 | 68 | 69 | } 70 | -------------------------------------------------------------------------------- /GameFunctions.java: -------------------------------------------------------------------------------- 1 | public class GameFunctions { 2 | static String getSecretCode() { 3 | String code = ""; 4 | while(code.length() < 4) { 5 | int rand = (int)(Math.random() * 7) + 1; 6 | if(!(code.contains(""+rand))) { 7 | code += rand; 8 | } 9 | } 10 | return code; 11 | } 12 | 13 | static boolean isDuplicated(char arg[]) { 14 | int count = 0; 15 | for(int i = 0; i 0; 23 | } 24 | 25 | static boolean wrongInput(String text) { 26 | int len = text.length(); 27 | String block = "01234567"; 28 | if(len != 4) 29 | return true; 30 | char arr[] = text.toCharArray(); 31 | for(int i=0; i < arr.length; i++) { 32 | char c = arr[i]; 33 | if(!(block.contains(""+c)) || isDuplicated(arr)) { 34 | return true; 35 | } 36 | } 37 | return false; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /GameLoop.java: -------------------------------------------------------------------------------- 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 | } 49 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | default: My_Mastermind.java 2 | javac My_Mastermind.java 3 | 4 | play: My_Mastermind.class 5 | java My_Mastermind 6 | 7 | auto: My_Mastermind.class 8 | java My_Mastermind 9 | 10 | all: My_Mastermind.clss 11 | java My_Mastermind 12 | 13 | clean: 14 | rm -rf src/*.class 15 | -------------------------------------------------------------------------------- /MastermindAlgorithm.java: -------------------------------------------------------------------------------- 1 | public class MastermindAlgorithm { 2 | private String guestCode; 3 | private String randomCode; 4 | public static final String WHITE = "\u001B[37m"; 5 | public static final String GREEN = "\u001B[32m"; 6 | public static final String YELLOW = "\u001B[33m"; 7 | public static final String BLUE = "\u001B[34m"; 8 | public static final String PURPLE = "\u001B[35m"; 9 | public static final String CYAN = "\u001B[36m"; 10 | public MastermindAlgorithm(String randomCode, String guestCode) { 11 | this.guestCode = guestCode; 12 | this.randomCode = randomCode; 13 | } 14 | 15 | public void wellPiecesAlgo() { 16 | int wp = 0; 17 | for(int i=0; i < this.randomCode.length(); i++) { 18 | if(this.randomCode.charAt(i) == this.guestCode.charAt(i)) 19 | wp++; 20 | } 21 | System.out.printf(CYAN + "--- Well placed pieces: %d ---\n", wp); 22 | } 23 | public void misPiecesAlgo() { 24 | int mp = 0; 25 | for(int i=0; i < this.randomCode.length(); i++) { 26 | if(this.randomCode.contains("" + this.guestCode.charAt(i)) && this.randomCode.charAt(i) != this.guestCode.charAt(i)) 27 | mp++; 28 | } 29 | System.out.printf(BLUE + "---- Misplaced pieces: %d ----\n", mp); 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /My_Mastermind.java: -------------------------------------------------------------------------------- 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 | 7 | public class My_Mastermind { 8 | public static void main(String[] args) { 9 | GameController gameController = new GameController(args); 10 | gameController.initialize(); 11 | GameLoop gameLoop = new GameLoop( 12 | gameController.getRounds(), 13 | gameController.getSecretCode() 14 | ); 15 | gameLoop.run(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Welcome to My Java Mastermind 2 | Welcome to my projects Java Mastermind 3 | 4 | ## Task 5 | This is my GitHub 6 | 7 | ## Description 8 | Mastermind is a game composed of 9 pieces of different colors. 9 | A secret code is then composed of 4 distinct pieces. 10 | 11 | The player has 10 attempts to find the secret code. 12 | After each input, the game indicates to the player the number of well placed pieces and the number of misplaced pieces. 13 | 14 | Pieces will be '0' '1' '2' '3' '4' '5' '6' '7' '8'. 15 | 16 | If the player finds the code, they win, and the game stops. 17 | A misplaced piece is a piece that is present in the secret code but is not in a good position. 18 | 19 | You must read the player's input from the standard input. 20 | 21 | Your program will also receive the following parameters: 22 | -c [CODE]: specifies the secret code. If no code is specified, a random code will be generated. 23 | -t [ATTEMPTS]: specifies the number of attempts; by default, the player has 10 attempts. 24 | 25 | ## Installation 26 | No need install anything, only need write in terminal 27 | ``` 28 | java My_Mastermind.java 29 | ``` 30 | and the game begins! 31 | --------------------------------------------------------------------------------