Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.84 KB | None | 0 0
  1. package fr.liwlufe.sgp;
  2.  
  3. import fr.liwlufe.shortcuts.Clavier;
  4. import fr.liwlufe.shortcuts.Ecran;
  5.  
  6. public class Mastermind {
  7.  
  8.     private static boolean hasWon = false;
  9.     private static final int CHANCES = 10;
  10.     private static final char RED = 'R', BLUE = 'B', YELLOW = 'J', GREEN = 'V', ORANGE = 'O', GREY = 'G', INDIGO = 'I', FUCHSIA = 'F', BLANK = ' ';
  11.     private static final String FIRST_SHOT = "Veuillez saisir la première lettre de la combinaison", SECOND_SHOT = "Veuillez saisir la seconde lettre de la combinaison", THIRD_SHOT = "Veuillez saisir la troisième lettre de la combinaison", FOURTH_SHOT = "Veuillez saisir la quatrième lettre de la combinaison";
  12.    
  13.     private static class Combination {
  14.  
  15.         char first, second, third, fourth;
  16.  
  17.     }
  18.  
  19.     public static class Shot {
  20.        
  21.         char first, second, third, fourth;
  22.        
  23.     }
  24.    
  25.     public static void main(String[] args) {
  26.  
  27.         Combination c = createCombination();
  28.         Ecran.afficherln("[" + c.first + c.second + c.third + c.fourth + "]");
  29.         Shot s = createShot();
  30.         for(int i = 1; i <= 10; ++i) {
  31.             hasWon = compareCombination(s, c);
  32.             int shot  = CHANCES;
  33.             shot--;
  34.            
  35.             if(hasWon) {
  36.                 end(shot,c, hasWon);
  37.                 break;
  38.             }
  39.            
  40.         }
  41.  
  42.     }
  43.  
  44.     private static void end(int shot, Combination c, boolean hasWon) {
  45.        
  46.         if (hasWon) {
  47.             Ecran.afficher("BRAVO, combinaison trouvée en " + (CHANCES-shot )+ " coup(s)");
  48.         }else {
  49.             Ecran.afficher("PERDU: La combinaison secrète était : " + "[" + c.first + c.second + c.third + c.fourth + "]");
  50.         }
  51.        
  52.     }
  53.  
  54.     private static Combination random(Combination c, int counter) {
  55.  
  56.         char color;
  57.         int randomNumber = randomNumber(8);
  58.  
  59.         switch(randomNumber) {
  60.  
  61.         case 0:
  62.             color = RED;
  63.             break;
  64.         case 1:
  65.             color = BLUE;
  66.             break;
  67.         case 2:
  68.             color = YELLOW;
  69.             break;
  70.         case 3:
  71.             color = GREEN;
  72.             break;
  73.         case 4:
  74.             color = ORANGE;
  75.             break;
  76.         case 5:
  77.             color = GREY;
  78.             break;
  79.         case 6:
  80.             color = INDIGO;
  81.             break;
  82.         case 7:
  83.             color = FUCHSIA;
  84.             break;
  85.         default:
  86.             color = BLANK;
  87.             break;
  88.  
  89.         }
  90.  
  91.        
  92.         switch(counter) {
  93.        
  94.         case 1:
  95.             c.first = color;
  96.         case 2:
  97.             c.second = color;
  98.         case 3:
  99.             c.third = color;
  100.         case 4:
  101.             c.fourth = color;
  102.        
  103.         }
  104.         return c;
  105.  
  106.     }
  107.  
  108.     private static int randomNumber(int max) {
  109.  
  110.         return (int) (Math.random()*max);
  111.  
  112.     }
  113.  
  114.     private static Combination createCombination() {
  115.        
  116.         Combination c = new Combination();
  117.  
  118.         do {
  119.             for(int i = 1; i <= 4; ++i) {
  120.                 c = random(c, i);  
  121.             }
  122.         } while (c.first == c.second || c.first == c.third || c.first == c.fourth || c.second == c.third || c.second == c.fourth || c.third == c.fourth);
  123.  
  124.         return c;
  125.        
  126.     }
  127.    
  128.     private static Shot createShot() {
  129.        
  130.         Shot s = new Shot();
  131.        
  132.         char first = ' ', second = ' ', third = ' ', fourth = ' ';
  133.        
  134.         do {
  135.             Ecran.afficherln(FIRST_SHOT);
  136.             first = Clavier.saisirChar();
  137.         } while (first != RED && first != BLUE && first != YELLOW && first != GREEN && first != ORANGE && first != GREY && first != INDIGO && first != FUCHSIA);
  138.  
  139.         do {
  140.             Ecran.afficherln(SECOND_SHOT);
  141.             second = Clavier.saisirChar();
  142.         } while (second != RED && second != BLUE && second != YELLOW && second != GREEN && second != ORANGE && second != GREY && second != INDIGO && second != FUCHSIA);
  143.  
  144.         do {
  145.             Ecran.afficherln(THIRD_SHOT);
  146.             third = Clavier.saisirChar();
  147.         } while (third != RED && third != BLUE && third != YELLOW && third != GREEN && third != ORANGE && third != GREY && third != INDIGO && third != FUCHSIA);
  148.  
  149.         do {
  150.             Ecran.afficherln(FOURTH_SHOT);
  151.             fourth = Clavier.saisirChar();
  152.         } while (fourth != RED && fourth != BLUE && fourth != YELLOW && fourth != GREEN && fourth != ORANGE && fourth != GREY && fourth != INDIGO && fourth != FUCHSIA);
  153.  
  154.         s.first = first;
  155.         s.second = second;
  156.         s.third = third;
  157.         s.fourth = fourth;
  158.        
  159.         return s;
  160.        
  161.     }
  162.    
  163.     private static boolean compareCombination(Shot s, Combination c) {
  164.    
  165.         Ecran.afficherln("[" + s.first + s.second + s.third + s.fourth + "]");
  166.        
  167.         /*
  168.          *  GCWP : Good color well placed
  169.          *  GCBP : Good color bad placed
  170.          */
  171.        
  172.         final char GCWP = '#';
  173.         final char GCBP = '*';
  174.        
  175.         if(s.first == c.first && s.second == c.second && s.third == c.third && s.fourth == c.fourth) return true;
  176.        
  177.         if(s.first == c.first) Ecran.afficher(GCWP);
  178.        
  179.         if(s.second == c.second) Ecran.afficher(GCWP);
  180.        
  181.         if(s.third == c.third) Ecran.afficher(GCWP);
  182.        
  183.         if(s.fourth == c.fourth) Ecran.afficher(GCWP);
  184.        
  185.        
  186.         if(s.first == c.second || s.first == c.third || s.first == c.fourth) Ecran.afficher(GCBP);
  187.        
  188.         if(s.second == c.first || s.second == c.third || s.second == c.fourth) Ecran.afficher(GCBP);
  189.        
  190.         if(s.third == c.first || s.third == c.second || s.third == c.fourth) Ecran.afficher(GCBP);
  191.        
  192.         if(s.fourth == c.first || s.fourth == c.second || s.fourth == c.third) Ecran.afficher(GCBP);
  193.        
  194.        
  195.         return false;
  196.        
  197.        
  198.     }
  199.  
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement