Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.liwlufe.sgp;
- import fr.liwlufe.shortcuts.Clavier;
- import fr.liwlufe.shortcuts.Ecran;
- public class Mastermind {
- private static boolean hasWon = false;
- private static final int CHANCES = 10;
- private static final char RED = 'R', BLUE = 'B', YELLOW = 'J', GREEN = 'V', ORANGE = 'O', GREY = 'G', INDIGO = 'I', FUCHSIA = 'F', BLANK = ' ';
- 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";
- private static class Combination {
- char first, second, third, fourth;
- }
- public static class Shot {
- char first, second, third, fourth;
- }
- public static void main(String[] args) {
- Combination c = createCombination();
- Ecran.afficherln("[" + c.first + c.second + c.third + c.fourth + "]");
- Shot s = createShot();
- for(int i = 1; i <= 10; ++i) {
- hasWon = compareCombination(s, c);
- int shot = CHANCES;
- shot--;
- if(hasWon) {
- end(shot,c, hasWon);
- break;
- }
- }
- }
- private static void end(int shot, Combination c, boolean hasWon) {
- if (hasWon) {
- Ecran.afficher("BRAVO, combinaison trouvée en " + (CHANCES-shot )+ " coup(s)");
- }else {
- Ecran.afficher("PERDU: La combinaison secrète était : " + "[" + c.first + c.second + c.third + c.fourth + "]");
- }
- }
- private static Combination random(Combination c, int counter) {
- char color;
- int randomNumber = randomNumber(8);
- switch(randomNumber) {
- case 0:
- color = RED;
- break;
- case 1:
- color = BLUE;
- break;
- case 2:
- color = YELLOW;
- break;
- case 3:
- color = GREEN;
- break;
- case 4:
- color = ORANGE;
- break;
- case 5:
- color = GREY;
- break;
- case 6:
- color = INDIGO;
- break;
- case 7:
- color = FUCHSIA;
- break;
- default:
- color = BLANK;
- break;
- }
- switch(counter) {
- case 1:
- c.first = color;
- case 2:
- c.second = color;
- case 3:
- c.third = color;
- case 4:
- c.fourth = color;
- }
- return c;
- }
- private static int randomNumber(int max) {
- return (int) (Math.random()*max);
- }
- private static Combination createCombination() {
- Combination c = new Combination();
- do {
- for(int i = 1; i <= 4; ++i) {
- c = random(c, i);
- }
- } 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);
- return c;
- }
- private static Shot createShot() {
- Shot s = new Shot();
- char first = ' ', second = ' ', third = ' ', fourth = ' ';
- do {
- Ecran.afficherln(FIRST_SHOT);
- first = Clavier.saisirChar();
- } while (first != RED && first != BLUE && first != YELLOW && first != GREEN && first != ORANGE && first != GREY && first != INDIGO && first != FUCHSIA);
- do {
- Ecran.afficherln(SECOND_SHOT);
- second = Clavier.saisirChar();
- } while (second != RED && second != BLUE && second != YELLOW && second != GREEN && second != ORANGE && second != GREY && second != INDIGO && second != FUCHSIA);
- do {
- Ecran.afficherln(THIRD_SHOT);
- third = Clavier.saisirChar();
- } while (third != RED && third != BLUE && third != YELLOW && third != GREEN && third != ORANGE && third != GREY && third != INDIGO && third != FUCHSIA);
- do {
- Ecran.afficherln(FOURTH_SHOT);
- fourth = Clavier.saisirChar();
- } while (fourth != RED && fourth != BLUE && fourth != YELLOW && fourth != GREEN && fourth != ORANGE && fourth != GREY && fourth != INDIGO && fourth != FUCHSIA);
- s.first = first;
- s.second = second;
- s.third = third;
- s.fourth = fourth;
- return s;
- }
- private static boolean compareCombination(Shot s, Combination c) {
- Ecran.afficherln("[" + s.first + s.second + s.third + s.fourth + "]");
- /*
- * GCWP : Good color well placed
- * GCBP : Good color bad placed
- */
- final char GCWP = '#';
- final char GCBP = '*';
- if(s.first == c.first && s.second == c.second && s.third == c.third && s.fourth == c.fourth) return true;
- if(s.first == c.first) Ecran.afficher(GCWP);
- if(s.second == c.second) Ecran.afficher(GCWP);
- if(s.third == c.third) Ecran.afficher(GCWP);
- if(s.fourth == c.fourth) Ecran.afficher(GCWP);
- if(s.first == c.second || s.first == c.third || s.first == c.fourth) Ecran.afficher(GCBP);
- if(s.second == c.first || s.second == c.third || s.second == c.fourth) Ecran.afficher(GCBP);
- if(s.third == c.first || s.third == c.second || s.third == c.fourth) Ecran.afficher(GCBP);
- if(s.fourth == c.first || s.fourth == c.second || s.fourth == c.third) Ecran.afficher(GCBP);
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement