Advertisement
makispaiktis

Decoding Mastermind

Aug 7th, 2022 (edited)
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.06 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.event.ListSelectionEvent;
  5. import javax.swing.event.ListSelectionListener;
  6. import java.security.KeyStore;
  7. import java.util.*;
  8.  
  9. public class Gui extends JFrame {
  10.  
  11.     // *******************************************************************************
  12.     // *******************************************************************************
  13.     // Variables
  14.     private static int tries = 8;
  15.     private static int codelength = 4;
  16.  
  17.     private JList list;
  18.     private static Color[] colors = {Color.YELLOW, Color.ORANGE, Color.CYAN, Color.BLUE, Color.PINK, new Color(85, 34, 132)};
  19.     private static String[] colornames = {"yellow", "orange", "cyan", "blue", "pink", "purple"};
  20.     private static Character[] colortags = {'y', 'o', 'c', 'b', 'p', 'm'};
  21.     private static String[] colortags2 = {"y", "o", "c", "b", "p", "m"};
  22.     public static JButton[][] buttons;
  23.  
  24.  
  25.  
  26.     // Constructor
  27.     public Gui(){
  28.  
  29.         super("----  y = yellow  ----  o = orange  ----  c = cyan  ----  b = blue  ----  p = pink  ----  m = purple  ----");
  30.         setLayout(new GridLayout(tries, 2*codelength+1));
  31.         buttons = new JButton[tries][2*codelength+1];
  32.  
  33.         list = new JList(colornames);
  34.         list.setVisibleRowCount(4);
  35.         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);    // Select only 1 at a time
  36.  
  37.         for(int i=0; i<tries; i++){
  38.             for(int j=0; j<2*codelength+1; j++){
  39.                 JButton b;
  40.                 if(j == codelength){
  41.                     b = new JButton("Check!");
  42.                     b.addActionListener(new ActionListener() {
  43.                         @Override
  44.                         public void actionPerformed(ActionEvent e) {
  45.  
  46.                         }
  47.                     });
  48.                 }
  49.                 else{
  50.                     b = new JButton();
  51.  
  52.                 }
  53.                 buttons[i][j] = b;
  54.                 add(b);
  55.                 // Container container = b;
  56.                 // container.add(list);
  57.             }
  58.         }
  59.  
  60.  
  61.  
  62.  
  63.     }   // END OF CONSTRUCTOR
  64.  
  65.  
  66.  
  67.     // Auxiliary Functions
  68.     public static int uniqueChars(String str){
  69.  
  70.         String temp="";
  71.         for (int i = 0; i < str.length(); i++) {
  72.             if(temp.indexOf(str.charAt(i)) == -1 ){
  73.                 temp = temp + str.charAt(i);
  74.             }
  75.         }
  76.        return temp.length();
  77.  
  78.     }
  79.  
  80.     public static boolean hasValidChars(String str){
  81.  
  82.         for(int i=0; i<str.length(); i++){
  83.             Character ch = str.charAt(i);
  84.             boolean flag = false;
  85.             for(int j=0; j<colortags.length; j++){
  86.                 Character ch2 = colortags[j];
  87.                 if(ch == ch2){
  88.                     flag = true;
  89.                     break;
  90.                 }
  91.             }
  92.             if(flag == false) {
  93.                 // Flag 'stayed' false ----> invalid characters
  94.                 return false;
  95.             }
  96.         }
  97.         return true;
  98.     }
  99.  
  100.     public static String createCode(){
  101.         // Yellow-Orange-Cyan-Blue ----> yocb
  102.         String code = "aaaa";
  103.         Random r = new Random();
  104.         while(uniqueChars(code) != codelength){
  105.             code = "";
  106.             for(int i=0; i<codelength; i++){
  107.                 code += colortags[r.nextInt(colortags.length)];
  108.             }
  109.         }
  110.         return code;
  111.     }
  112.  
  113.  
  114.  
  115.     public static ArrayList<Color> convertIntoColors(String guess){
  116.         // Guess is valid
  117.         ArrayList<Color> guessColors = new ArrayList<Color>();
  118.         for(int i=0; i<guess.length(); i++){
  119.             Character ch = guess.charAt(i);
  120.             for(int j=0; j<colortags.length; j++){
  121.                 if(ch == colortags[j]){
  122.                     // j is the desired index
  123.                     guessColors.add(colors[j]);
  124.                 }
  125.             }
  126.         }
  127.         return guessColors;
  128.     }
  129.  
  130.  
  131.  
  132.     public static void makeColors(ArrayList<Color> guessColors, int round){
  133.         // I will the the row = round - 1 in buttons and columns 0 until codelength - 1
  134.         int row = round - 1;
  135.         for(int column=0; column<codelength; column++){
  136.             buttons[row][column].setBackground(guessColors.get(column));
  137.         }
  138.     }
  139.  
  140.  
  141.     public static ArrayList<Color> check(String guess, String code){
  142.         ArrayList<Color> hints = new ArrayList<Color>();
  143.         int reds = 0;
  144.         int whites = 0;
  145.         int blacks = 0;
  146.  
  147.         // 1. Find how many reds we have
  148.         for(int i=0; i<guess.length(); i++){
  149.             Character ch1 = guess.charAt(i);
  150.             Character ch2 = code.charAt(i);
  151.             if(ch1 == ch2){
  152.                 reds += 1;
  153.                 // JOptionPane.showMessageDialog(null, "Red at pos = " + (i+1));
  154.             }
  155.         }
  156.         for(int i=0; i<reds; i++){
  157.             hints.add(Color.RED);
  158.         }
  159.  
  160.         // 2. Find how many whites we have
  161.         for(int i=0; i<guess.length(); i++){
  162.             for(int j=0; j<code.length(); j++){
  163.                 if(guess.charAt(i) == code.charAt(j) && i != j){
  164.                     whites += 1;
  165.                     // JOptionPane.showMessageDialog(null, "White at pos = " + (i+1));
  166.                 }
  167.             }
  168.         }
  169.         for(int i=0; i<whites; i++){
  170.             hints.add(Color.WHITE);
  171.         }
  172.  
  173.         // 3. Find the blacks
  174.         for(int i=0; i<codelength-reds-whites; i++){
  175.             hints.add(Color.BLACK);
  176.         }
  177.  
  178.         return hints;
  179.     }
  180.  
  181.  
  182.     public static void makeHints(ArrayList<Color> hints, int round){
  183.         int row = round - 1;
  184.         for(int i=0; i<hints.size(); i++){
  185.             int column = codelength + 1 + i;
  186.             buttons[row][column].setBackground(hints.get(i));
  187.         }
  188.     }
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.     // *******************************************************************************
  204.     // *******************************************************************************
  205.     // Main Function
  206.     public static void main(String[] args){
  207.  
  208.  
  209.         // 1. Basics
  210.         Gui gui = new Gui();
  211.         gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
  212.         gui.setSize(800, 800);
  213.         gui.setVisible(true);
  214.  
  215.  
  216.         // 2. Displays
  217.         String display1 = "Welcome to the famous board game: 'Decoding' or 'Mastermind'\n";
  218.         String display2 = "You have to try to guess the secret color code. The code consists of\n";
  219.         String display3 = codelength + " different colors: The available colors are " + colors.length + ":\n";
  220.         String display4 = (String.join(", ", colornames)) + "\n";
  221.         String display5 = (String.join(" -- ", colortags2)) + "\n";
  222.         String displays = display1 + display2 + display3 + display4 + display5 + "\n\n";
  223.         System.out.println(displays);
  224.         JOptionPane.showMessageDialog(null, displays, "Decoding - Introduction", JOptionPane.PLAIN_MESSAGE);
  225.  
  226.  
  227.         // 3. Rules about input
  228.         String rules1 = "You have " + tries + " tries to decode the message. Your input must be in this form: \n";
  229.         String rules2 = "abcd, where a, b, c and d are the colortags (y, o, c, b, p, m)";
  230.         String rules = rules1 + rules2 + "\n\n";
  231.         System.out.println(rules);
  232.         JOptionPane.showMessageDialog(null, rules, "Decoding - The rules", JOptionPane.PLAIN_MESSAGE);
  233.  
  234.  
  235.         // 4. Random selection of code - colors
  236.         String code = createCode();
  237.         System.out.println(code);
  238.         JOptionPane.showMessageDialog(null, code, "Decoding - The code", JOptionPane.PLAIN_MESSAGE);
  239.  
  240.  
  241.  
  242.  
  243.         // 5. User tries via JOptionPane input messages (String)
  244.         int round = 1;
  245.         String guess = "aaaa";
  246.  
  247.         while(round <= tries && !guess.equals(code)){
  248.             String title = "Decoding - Try " + round + "\n";
  249.             System.out.println(title);
  250.             boolean flag1, flag2, flag3;
  251.  
  252.             // guess is a 4-char string ----> example:      guess = 'yocb'
  253.             // I have to check 3 conditions: length = 4, 4 unique characters, 4 valid characters
  254.             guess = JOptionPane.showInputDialog("Guess " + round + " ----> ");
  255.             flag1 = false;
  256.             flag2 = false;
  257.             flag3 = false;
  258.             if(guess.length() == codelength){
  259.                 flag1 = true;
  260.             }
  261.             if(uniqueChars(guess) == codelength){
  262.                 flag2 = true;
  263.             }
  264.             if(hasValidChars(guess)){
  265.                 flag3 = true;
  266.             }
  267.  
  268.             // Logs about errors
  269.             if(!flag1){
  270.                 String error1 = "Length must be " + codelength + "\n";
  271.                 System.out.println(error1);
  272.                 JOptionPane.showMessageDialog(null, error1, "Error in try " + round, JOptionPane.PLAIN_MESSAGE);
  273.             }
  274.             if(!flag2){
  275.                 String error2 = "Unique characters must be " + codelength + "\n" ;
  276.                 System.out.println(error2);
  277.                 JOptionPane.showMessageDialog(null, error2, "Error in try " + round, JOptionPane.PLAIN_MESSAGE);
  278.             }
  279.             if(!flag3){
  280.                 String error3 = "You must have insrted some invalid characters (valid = y, o, c, b, p, m) \n";
  281.                 System.out.println(error3);
  282.                 JOptionPane.showMessageDialog(null, error3, "Error in try " + round, JOptionPane.PLAIN_MESSAGE);
  283.             }
  284.  
  285.  
  286.  
  287.  
  288.             // **********************************************************************************************
  289.             // *************************    * CORRECT INPUT *****************************************************
  290.             // **********************************************************************************************
  291.             // COPY IN NEXT LINES
  292.             if(flag1 && flag2 && flag3){
  293.  
  294.                 ArrayList<Color> guessColors = convertIntoColors(guess);
  295.                 makeColors(guessColors, round);
  296.                 // !!!! Until this line OK !!!!
  297.                 ArrayList<Color> hints = check(guess, code);
  298.                 makeHints(hints, round);
  299.  
  300.             }
  301.  
  302.  
  303.  
  304.  
  305.             // **********************************************************************************************
  306.             // *************************   WRONG INPUT   ****************************************************
  307.             // **********************************************************************************************
  308.             else{
  309.  
  310.                 // guess is a 4-char string ----> example:      guess = 'yocb'
  311.                 // I have to check 3 conditions: length = 4, 4 unique characters, 4 valid characters
  312.                 guess = JOptionPane.showInputDialog("Guess " + round + " ----> ");
  313.                 flag1 = false;
  314.                 flag2 = false;
  315.                 flag3 = false;
  316.                 if(guess.length() == codelength){
  317.                     flag1 = true;
  318.                 }
  319.                 if(uniqueChars(guess) == codelength){
  320.                     flag2 = true;
  321.                 }
  322.                 if(hasValidChars(guess)){
  323.                     flag3 = true;
  324.                 }
  325.  
  326.                 // Logs about errors
  327.                 if(!flag1){
  328.                     String error1 = "Length must be " + codelength + "\n";
  329.                     System.out.println(error1);
  330.                     JOptionPane.showMessageDialog(null, error1, "Error in try " + round, JOptionPane.PLAIN_MESSAGE);
  331.                 }
  332.                 if(!flag2){
  333.                     String error2 = "Unique characters must be " + codelength + "\n" ;
  334.                     System.out.println(error2);
  335.                     JOptionPane.showMessageDialog(null, error2, "Error in try " + round, JOptionPane.PLAIN_MESSAGE);
  336.                 }
  337.                 if(!flag3){
  338.                     String error3 = "You must have insrted some invalid characters (valid = y, o, c, b, p, m) \n";
  339.                     System.out.println(error3);
  340.                     JOptionPane.showMessageDialog(null, error3, "Error in try " + round, JOptionPane.PLAIN_MESSAGE);
  341.                 }
  342.  
  343.                 if(flag1 && flag2 && flag3){
  344.  
  345.                     ArrayList<Color> guessColors = convertIntoColors(guess);
  346.                     makeColors(guessColors, round);
  347.                     // !!!! Until this line OK !!!!
  348.                     ArrayList<Color> hints = check(guess, code);
  349.                     makeHints(hints, round);
  350.  
  351.                 }
  352.  
  353.             }
  354.  
  355.  
  356.             round++;
  357.         }
  358.  
  359.  
  360.         // Here we have 2 possibilities: User won or tries left are 0
  361.         if(guess.equals(code)){
  362.             String bravo = "Congratulations! You needed " + (round-1) + " tries to win the game! \n";
  363.             System.out.println(bravo);
  364.             JOptionPane.showMessageDialog(null, bravo, "Congratulations", JOptionPane.PLAIN_MESSAGE);
  365.         }
  366.         else{
  367.             String bravo2 = "You wasted all your tries (" + tries + "). Do you want to see the correct? \n";
  368.             System.out.println(bravo2);
  369.             JOptionPane.showMessageDialog(null, bravo2, "Lost...", JOptionPane.PLAIN_MESSAGE);
  370.         }
  371.  
  372.     }   // END OF MAIN FUNCTION
  373.  
  374.  
  375.  
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement