1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
import java.util.Scanner;
/**
* Class qui ....
* @author XXX
*
*/
public class MastermindHo {
private static Scanner sc = new Scanner(System.in);
private static String[] TAB_REF_COLORS = { "rouge", "jaune", "vert", "bleu", "orange", "blanc", "violet",
"fuchsia" };
private static int NB_COLORS = 4;
private static int ColCor = 0;
private static int indexCor = 0;
private static String[] tabPlayer = { " ", " ", " ", " " };
private static String[] combination = new String[NB_COLORS];
private static String[] tabJoueur = new String[NB_COLORS];
private static int compteur = 0;
private static String[] tabCorrect = { " ", " ", " ", " " };
public static void main(String[] args) {
// appel 1ère fonction pour générer des couleurs aléatoires :
String[] combinaisonSecrete = generateRandomCombination();
for (int i = 0; i < 4; i++) {
System.out.print(combinaisonSecrete[i] + "|");
}
while (compteur <= 12 && (12 - compteur) > 0) {
// appel 2ème fonction pour récupérer la combinaison du joueur et la comparer
// avec celle de l'ordi :
choixNumerote();
compareColor(tabJoueur, combinaisonSecrete);
for (int x = 0; x < 4; x++) {
System.out.print(tabCorrect[x] + "|");
}
sc.close();
}
}
/**
* Generate random combination of 4 colors
* @return
*/
private static String[] generateRandomCombination() {
String[] combination = new String[NB_COLORS];
int currentPosition = 0;
while (currentPosition != NB_COLORS) {
int indexRandom = (int) (Math.random() * TAB_REF_COLORS.length);
String color = TAB_REF_COLORS[indexRandom];
if (!isIn(color, combination)) {
combination[currentPosition] = color;
currentPosition++;
}
}
return combination;
}
/**
*
* @param iStringToFind
* @param iTab
* @return
*/
private static boolean isIn(String iStringToFind, String[] iTab) {
int size = iTab.length;
for (int i = 0; i < size; i++) {
if (iStringToFind.equalsIgnoreCase(iTab[i]))
return true;
}
return false;
}
/**
* Collect user combination of 4 colors
* @return
*/
private static String[] choixNumerote() {
for (int i = 0; i < NB_COLORS; i++) {
System.out.println("\nChoisissez une couleur : ");
tabJoueur[i] = sc.nextLine();
}
for (int k = 0; k < NB_COLORS; k++) {
System.out.print(tabJoueur[k] + "|");
}
return tabJoueur;
}
/**
* Fonction pour faire la comparaison entre les 2 tableaux et stocker les élément en communs.
* @param tabJ
* @param comb
* @return
*/
private static String[] compareColor(String[] tabJ, String[] comb) {
int k = 0;
for (int i = 0; i < tabJ.length; i++) {
for (int j = 0; j < comb.length; j++) {
if (comb[j].equals(tabJ[i]) && j == i) {
ColCor++;
indexCor++;
tabCorrect[k] = tabJ[i];
k++;
break;
} else if (comb[j].equals(tabJ[i]) && j != i) {
ColCor++;
tabCorrect[k] = tabJ[i];
k++;
break;
} else {
continue;
}
}
}
System.out.println("Vous avez trouver " + ColCor + " couleur(s) correctes");
System.out.println("Vous avez trouver " + indexCor + " emplacement(s) correctes");
compteur++;
return tabCorrect;
}
} |
Partager