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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| public class Suite {
static int c1;
static int c2;
static int c3;
static int c4;
public Suite (int c1,int c2,int c3,int c4)
{
Suite.c1=c1;
Suite.c2=c2;
Suite.c3=c3;
Suite.c4=c4;
}
public Suite (int N)
{
c1=(int) N/1000;
c2=(int)(N-(c1*1000))/100;
c3=(int)(N-(c1*1000)-(N-(c2*100)))/10;
c4=(int)(N-(c1*1000)-(N-(c2*100))-(N-(c3*10)));
}
public static Suite genereSecret()
{
int min = 1;
int max = 6;
c1 = (int)(Math.random() * (max-min)) + min;
c2= (int)(Math.random() * (max-min)) + min;
c3 = (int)(Math.random() * (max-min)) + min;
c4 = (int)(Math.random() * (max-min)) + min;
return Suite.genereSecret();
}
public Indication Compare (Suite c){
Indication res=new Indication(0,0);
if (c.c1==genereSecret().c1)
{
res.setNb_bien(res.getNb_bien()+1);
}
else
if ((c.c1==Suite.genereSecret().c2)||(c.c1==Suite.genereSecret().c3)||(c.c1==Suite.genereSecret().c4))
{
res.setNb_mal(res.getNb_mal()+1);
}
else
if (c.c2==genereSecret().c2)
{
res.setNb_bien(res.getNb_bien()+1);
}
else
if ((c.c2==Suite.genereSecret().c1)||(c.c2==Suite.genereSecret().c3)||(c.c2==Suite.genereSecret().c4))
{
res.setNb_mal(res.getNb_mal()+1);
}
else
if (c.c3==genereSecret().c3)
{
res.setNb_bien(res.getNb_bien()+1);
}
else
if ((c.c3==Suite.genereSecret().c2)||(c.c3==Suite.genereSecret().c1)||(c.c3==Suite.genereSecret().c4))
{
res.setNb_mal(res.getNb_mal()+1);
}
return res;
}
public String toString(){
return("les valeurs des 4 nombres de la suite sont :"+c1+c2+c3+c4);
}
}
et voila le code de la classe mastermind qui une classe de test
import java.io.IOException;
public class MasterMind {
static int ch=0;
public static void main(String[] args) {
while(true){
System.out.println("-----------Que desirez vous faire?----------");
System.out.println("Jouer contre l'ordinateur................1");
System.out.println(" Quitter..................................2");
int choix=0 ;
System.out.println("Entrer votre choix de 1 a 2");
try {
choix=StdInput.readInt();
} catch (IOException e) {
e.printStackTrace();
}
switch (choix)
{
case 1:play();
break;
case 2:System.exit(0);
break;
}
}
}
public static void play(){
Suite.genereSecret();
System.out.println("J'ai choisi une suite de 4 nombres");
System.out.println("A vous de deviner");
int i=0;
while (i<8){
System.out.println("Essai Numero"+i);
i=i+1;
System.out.println("veuillez saisir un nombre de 4 chiffres");
try {
ch=StdInput.readInt();
} catch (IOException e) {
e.printStackTrace();
}
}
Suite sh=new Suite(ch);
while (sh.Compare(c)){
Indication in=new Indication(0,0);
if (in.getNb_bien()==4){
System.out.println("vous avez gagne, vous avez faitle "+i+" Essais");
}
else{
System.out.println("le nombre de bien places est "+in.getNb_bien()+" le nombre de mal places est "+in.getNb_mal());
}
System.out.println("veuillez saisir de nouveau un nombre de 4 chiffres");
}
if (i==8){
System.out.println("Vous avez perdu");
}
}
} |
Partager