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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
|
import javax.swing.* ;
import java.awt.* ;
class MaFenetre_3 extends JFrame{
// Construction de ma fenetre
public MaFenetre_3 (){
// Avec respectivement le titre, la taille,et la position
setTitle ("Tableau Mastermind") ;
setSize (1000, 728) ;
// setLocationRelativeTo(null);
setLocation(300, 30);
// Empêche de redimentionner la fenêtre manuellement
setResizable(false);
setVisible(true) ;
// Construction de mon paneau
Paneau_3 pan=new Paneau_3();
pan.setLayout(null);// sert à gérer les dispositions des composantes???
// couleur de l'arrière plan, puis de la police
pan.setBackground(Color.cyan);
pan.setForeground(Color.black);
// Ajout de mon panneau dans la fenêtre
getContentPane().add(pan) ;
}
}
class Carre1 {
int xCarre,yCarre; // Cooord. du carre
Color couleur; // sa couleur
int choice1; // nombre ecrit dessus
final int cote = 30; // taille du carre
public void remplir(int x, int y, Color c, int nb) {
xCarre = 100;
yCarre = 50;
couleur = c;
choice1 = nb;
}}
class Carre2 {
int xCarre,yCarre; // Cooord. du carre
Color couleur; // sa couleur
int choice2; // nombre ecrit dessus
final int cote = 30; // taille du carre
public void remplir(int x, int y, Color c, int nb) {
xCarre = 100;
yCarre = 50;
couleur = c;
choice2 = nb;
}}
class Carre3 {
int xCarre,yCarre; // Cooord. du carre
Color couleur; // sa couleur
int choice3; // nombre ecrit dessus
final int cote = 30; // taille du carre
public void remplir(int x, int y, Color c, int nb) {
xCarre = 100;
yCarre = 50;
couleur = c;
choice3 = nb;
}}
class Carre4 {
int xCarre,yCarre; // Cooord. du carre
Color couleur; // sa couleur
int choice4; // nombre ecrit dessus
final int cote = 30; // taille du carre
public void remplir(int x, int y, Color c, int nb) {
xCarre = 100;
yCarre = 50;
couleur = c;
choice4 = nb;
}}
class PetitCarre1{
int xCarre,yCarre;
Color couleur1;
final int cote=15;
public void remplir(int x, int y, Color c1){
xCarre= 50;
yCarre=25;
couleur1=c1;
}
}
class PetitCarre2{
int xCarre,yCarre;
Color couleur2;
final int cote=15;
public void remplir(int x, int y,Color c2){
xCarre= 50;
yCarre=25;
couleur2=c2;
}
}
class PetitCarre3{
int xCarre,yCarre;
Color couleur3;
final int cote=15;
public void remplir(int x, int y,Color c3){
xCarre= 50;
yCarre=25;
couleur3=c3;
}
}
class PetitCarre4{
int xCarre,yCarre;
Color couleur4;
final int cote=15;
public void remplir(int x, int y,Color c4){
xCarre= 50;
yCarre=25;
couleur4=c4;
}
}
//création de la classe graphique avec le support JPanel
class Paneau_3 extends JPanel{
public Paneau_3 (){
smile = new ImageIcon("smiley.gif");}
// choix[]leschoix = new choix[48];
Carre1[] lesCarres1 = new Carre1[12];
int nombreCarres1Existants = 0;
Carre2[] lesCarres2 = new Carre2[12];
int nombreCarres2Existants = 0;
Carre3[] lesCarres3 = new Carre3[12];
int nombreCarres3Existants = 0;
Carre4[] lesCarres4 = new Carre4[12];
int nombreCarres4Existants = 0;
PetitCarre1[] lesPetitsCarres1=new PetitCarre1[12];
int nombrePetitCarres1Existants=0;
PetitCarre2[] lesPetitsCarres2=new PetitCarre2[12];
int nombrePetitCarres2Existants=0;
PetitCarre3[] lesPetitsCarres3=new PetitCarre3[12];
int nombrePetitCarres3Existants=0;
PetitCarre4[] lesPetitsCarres4=new PetitCarre4[12];
int nombrePetitCarres4Existants=0;
public void paintComponent(Graphics g){
super.paintComponent(g) ;
// création des lignes verticales, du point(50, 10) au point(50, 700)
g.drawLine (50, 0, 50, 700) ;
g.drawLine (150, 0, 150, 700) ;
g.drawLine (250, 0, 250, 700) ;
g.drawLine (350, 0, 350, 700) ;
g.drawLine (450, 0, 450, 700) ;
g.drawLine (500, 0, 500, 700) ;
g.drawLine (550, 0, 550, 700) ;
// création des lignes horizontales
g.drawLine (0, 100, 550, 100) ;
g.drawLine (0, 150, 550, 150) ;
g.drawLine (0, 200, 550, 200) ;
g.drawLine (0, 250, 550, 250) ;
g.drawLine (0, 300, 550, 300) ;
g.drawLine (0, 350, 550, 350) ;
g.drawLine (0, 400, 550, 400) ;
g.drawLine (0, 450, 550, 450) ;
g.drawLine (0, 500, 550, 500) ;
g.drawLine (0, 550, 550, 550) ;
g.drawLine (0, 600, 550, 600) ;
g.drawLine (0, 650, 550, 650) ;
g.drawLine (0, 700, 550, 700) ;
// création des lignes horizontales plus courtes
g.drawLine (450, 125, 550, 125) ;
g.drawLine (450, 175, 550, 175) ;
g.drawLine (450, 225, 550, 225) ;
g.drawLine (450, 275, 550, 275) ;
g.drawLine (450, 325, 550, 325) ;
g.drawLine (450, 375, 550, 375) ;
g.drawLine (450, 425, 550, 425) ;
g.drawLine (450, 475, 550, 475) ;
g.drawLine (450, 525, 550, 525) ;
g.drawLine (450, 575, 550, 575) ;
g.drawLine (450, 625, 550, 625) ;
g.drawLine (450, 675, 550, 675) ;
g.drawImage (smile.getImage(),820,620, null) ;
// création d'une nouvelle police de caractère pour le texte suivant
Font fonte=new Font("Verdana",Font.PLAIN,30);
g.setFont(fonte);
// dessin des chiffres à gauche sur la page graphique, avec les caractères et les coordonnées
g.drawString("12", 5, 685);
g.drawString("11", 5, 635);
g.drawString("10", 5, 585);
g.drawString("9", 13, 535);
g.drawString("8", 13, 485);
g.drawString("7", 13, 435);
g.drawString("6", 13, 385);
g.drawString("5", 13, 335);
g.drawString("4", 13, 285);
g.drawString("3", 13, 235);
g.drawString("2", 13, 185);
g.drawString("1", 13, 135);
// Changement de couleur pour le texte en-haut à droite
g.setColor(Color.red);
g.drawString("Jeu de Mastermind", 650, 60);
g.drawString("Regles", 730, 210);
// Création d'une nouvelle police de caractère
Font fonte_2=new Font("Verdana",Font.PLAIN,20);
g.setFont(fonte_2);
// position de départ pour le texte
int x=620,y=100;
// redéfinition de la couleur
g.setColor(Color.black);
// texte
String ch_1="Le but de ce jeu est de trouver de ";
String ch_2="quelles couleurs sont les pions ";
String ch_3="ainsi que l' ordre de ceux-ci. ";
// position finale du texte avec les intelignes.
// On utilise cette méthode ici, car c'est un texte de plusieurs lignes contrairement à précèdemment
// Affichage première ligne
g.drawString(ch_1,x,y);
// fm contient les caractéristiques de la fonte employée
FontMetrics fm=g.getFontMetrics();
// getHeight contient la hauteur de la police et tient compte de l'espace pour l'interligne
y+=fm.getHeight();
g.drawString(ch_2, x, y);
y+=fm.getHeight();
g.drawString(ch_3, x, y);
int x_2=620,y_2=250;
String ch_4="Vous avez droit au maximum";
String ch_5="à douze essais afin de trouver";
String ch_6="la solution.";
String ch_7="Sur le cote droite,";
String ch_8="une couleur rouge indique";
String ch_9="qu'il y a un pion";
String ch_10="de bonne couleur au bon endroit.";
String ch_11="Alors qu'une couleur blanche";
String ch_12="indique un pion de bonne couleur";
String ch_13="mais au mauvais endroit.";
String ch_14="Si il n'y a pas de couleur,";
String ch_15="cela signifie que";
String ch_16="les couleurs proposees ne sont pas";
String ch_17="dans la solution.";
g.drawString(ch_4, x_2, y_2);
FontMetrics fm_2=g.getFontMetrics();
y_2+=fm.getHeight();
g.drawString(ch_5,x_2,y_2);
y_2+=fm_2.getHeight();
g.drawString(ch_6, x_2, y_2);
y_2+=fm_2.getHeight();
g.drawString(ch_7, x_2, y_2);
y_2+=fm_2.getHeight();
g.drawString(ch_8,x_2,y_2);
y_2+=fm_2.getHeight();
g.drawString(ch_9, x_2, y_2);
y_2+=fm_2.getHeight();
g.drawString(ch_10, x_2, y_2);
y_2+=fm_2.getHeight();
g.drawString(ch_11,x_2,y_2);
y_2+=fm_2.getHeight();
g.drawString(ch_12, x_2, y_2);
y_2+=fm_2.getHeight();
g.drawString(ch_13, x_2, y_2);
y_2+=fm_2.getHeight();
g.drawString(ch_14, x_2, y_2);
y_2+=fm_2.getHeight();
g.drawString(ch_15, x_2, y_2);
y_2+=fm_2.getHeight();
g.drawString(ch_16, x_2, y_2);
y_2+=fm_2.getHeight();
g.drawString(ch_17, x_2, y_2);
int x_3=620,y_3=650;
String ch_18="Bonne chance !";
g.drawString(ch_18, x_3, y_3);
// carrés de couleurs
g.translate(540, 200); // Utilisez la methode carre plusieurs fois au lieu de toutes ces instructions
g.setColor(Color.white);
g.fillRect(30, 30, 30, 30);
g.setColor(Color.black);
g.drawString("0",37,53);
g.translate(+0, +50);
g.setColor(Color.yellow);
g.fillRect(30, 30, 30, 30);
g.setColor(Color.black);
g.drawString("1",38,52);
g.translate(+0, +50);
g.setColor(Color.orange);
g.fillRect(30, 30, 30, 30);
g.setColor(Color.black);
g.drawString("2",38,52);
g.translate(+0, +50);
g.setColor(Color.pink);
g.fillRect(30, 30, 30, 30);
g.setColor(Color.black);
g.drawString("3",38,52);
g.translate(+0, +50);
g.setColor(Color.red);
g.fillRect(30, 30, 30, 30);
g.setColor(Color.black);
g.drawString("4",38,53);
g.translate(+0, +50);
g.setColor(Color.magenta);
g.fillRect(30, 30, 30, 30);
g.setColor(Color.black);
g.drawString("5",38,53);
g.translate(+0, +50);
g.setColor(Color.green);
g.fillRect(30, 30, 30, 30);
g.setColor(Color.black);
g.drawString("6",39,54);
g.translate(+0, +50);
g.setColor(Color.blue);
g.fillRect(30, 30, 30, 30);
g.setColor(Color.black);
g.drawString("7",39,52);
g.translate(-590, -650);
g.setColor(Color.gray);
g.fillRect(100, 100, 100, 100);
g.translate(+100, 0);
g.setColor(Color.gray);
g.fillRect(100, 100, 100, 100);
g.translate(+100, 0);
g.setColor(Color.gray);
g.fillRect(100, 100, 100, 100);
g.translate(+100, 0);
g.setColor(Color.gray);
g.fillRect(100, 100, 100, 100);
Font fonte_3=new Font("Verdana",Font.PLAIN,50);
g.setFont(fonte_3);
int x_4=-160,y_4=165;
g.setColor(Color.yellow);
String ch_19="?";
String ch_20="?";
String ch_21="?";
String ch_22="?";
g.drawString(ch_19,x_4,y_4);
g.drawString(ch_20,x_4+100,y_4);
g.drawString(ch_21,x_4+200,y_4);
g.drawString(ch_22,x_4+300,y_4);
Font fonte_4=new Font("Verdana",Font.PLAIN,20);
g.setFont(fonte_4);
g.translate(-200, 180);
// Redessiner les carres existants
{ for (int i = 0; i < nombreCarres1Existants; i++){
if(i==1)g.translate(0, 50);
if(i==2)g.translate(0, 100);
if(i==3)g.translate(0, 150);
if(i==4)g.translate(0, 200);
if(i==5)g.translate(0, 250);
if(i==6)g.translate(0, 300);
if(i==7)g.translate(0, 350);
if(i==8)g.translate(0, 400);
if(i==9)g.translate(0, 450);
if(i==10)g.translate(0, 500);
if(i==11)g.translate(0, 550);
g.setColor(lesCarres1[i].couleur);
g.fillRect(lesCarres1[i].cote, lesCarres1[i].cote, lesCarres1[i].cote, lesCarres1[i].cote);
g.setColor(Color.black);
g.drawString(""+lesCarres1[i].choice1,39,52);
if(i==1)g.translate(0, -50);
if(i==2)g.translate(0, -100);
if(i==3)g.translate(0, -150);
if(i==4)g.translate(0, -200);
if(i==5)g.translate(0, -250);
if(i==6)g.translate(0, -300);
if(i==7)g.translate(0, -350);
if(i==8)g.translate(0, -400);
if(i==9)g.translate(0, -450);
if(i==10)g.translate(0, -500);
if(i==11)g.translate(0, -550);
}
g.translate(100, -100);
for (int i = 0; i < nombreCarres2Existants; i++){
if(i==0)g.translate(0, 100);
if(i==1)g.translate(-100, 150);
if(i==2)g.translate(-100, 200);
if(i==3)g.translate(-100, 250);
if(i==4)g.translate(-100, 300);
if(i==5)g.translate(-100, 350);
if(i==6)g.translate(-100, 400);
if(i==7)g.translate(-100, 450);
if(i==8)g.translate(-100, 500);
if(i==9)g.translate(-100, 550);
if(i==10)g.translate(-100, 600);
if(i==11)g.translate(-100, 650);
g.setColor(lesCarres2[i].couleur);
g.fillRect(lesCarres2[i].cote, lesCarres2[i].cote, lesCarres2[i].cote, lesCarres2[i].cote);
g.setColor(Color.black);
g.drawString(""+lesCarres2[i].choice2,39,52);
g.translate(+100, -100);
if(i==1)g.translate(0, -50);
if(i==2)g.translate(0, -100);
if(i==3)g.translate(0, -150);
if(i==4)g.translate(0, -200);
if(i==5)g.translate(0, -250);
if(i==6)g.translate(0, -300);
if(i==7)g.translate(0, -350);
if(i==8)g.translate(0, -400);
if(i==9)g.translate(0, -450);
if(i==10)g.translate(0, -500);
if(i==11)g.translate(0, -550);
}
for (int i = 0; i < nombreCarres3Existants; i++){
if(i==0)g.translate(0, 100);
if(i==1)g.translate(-100, 50);
if(i==2)g.translate(-100, 100);
if(i==3)g.translate(-100, 150);
if(i==4)g.translate(-100, 200);
if(i==5)g.translate(-100, 250);
if(i==6)g.translate(-100, 300);
if(i==7)g.translate(-100, 350);
if(i==8)g.translate(-100, 400);
if(i==9)g.translate(-100, 450);
if(i==10)g.translate(-100, 500);
if(i==11)g.translate(-100, 550);
g.setColor(lesCarres3[i].couleur);
g.fillRect(lesCarres3[i].cote, lesCarres3[i].cote, lesCarres3[i].cote, lesCarres3[i].cote);
g.setColor(Color.black);
g.drawString(""+lesCarres3[i].choice3,39,52);
g.translate(+100, 0);
if(i==1)g.translate(0, -50);
if(i==2)g.translate(0, -100);
if(i==3)g.translate(0, -150);
if(i==4)g.translate(0, -200);
if(i==5)g.translate(0, -250);
if(i==6)g.translate(0, -300);
if(i==7)g.translate(0, -350);
if(i==8)g.translate(0, -400);
if(i==9)g.translate(0, -450);
if(i==10)g.translate(0, -500);
if(i==11)g.translate(0, -550);
}
for (int i = 0; i < nombreCarres4Existants; i++){
if(i==1)g.translate(0, 50);
if(i==2)g.translate(0, 50);
if(i==3)g.translate(0, 50);
if(i==4)g.translate(0, 50);
if(i==5)g.translate(0, 50);
if(i==6)g.translate(0, 50);
if(i==7)g.translate(0, 50);
if(i==8)g.translate(0, 50);
if(i==9)g.translate(0, 50);
if(i==10)g.translate(0, 50);
if(i==11)g.translate(0, 50);
g.setColor(lesCarres4[i].couleur);
g.fillRect(lesCarres4[i].cote, lesCarres4[i].cote, lesCarres4[i].cote, lesCarres4[i].cote);
g.setColor(Color.black);
g.drawString(""+lesCarres4[i].choice4,39,52);
}
}
// position de départ
g.translate(+105,10);
{
for(int i=0; i<nombrePetitCarres1Existants; i++){
if(i>0)g.translate(0, -50);
if(i==1)g.translate(0, 50);
if(i==2)g.translate(0, 100);
if(i==3)g.translate(0, 150);
if(i==4)g.translate(0, 200);
if(i==5)g.translate(0, 250);
if(i==6)g.translate(0, 300);
if(i==7)g.translate(0, 350);
if(i==8)g.translate(0, 400);
if(i==9)g.translate(0, 450);
if(i==10)g.translate(0, 500);
if(i==11)g.translate(0, 550);
g.setColor(lesPetitsCarres1[i].couleur1);
g.fillRect(lesPetitsCarres1[i].cote, lesPetitsCarres1[i].cote, lesPetitsCarres1[i].cote, lesPetitsCarres1[i].cote);
if(i==1)g.translate(0, -50);
if(i==2)g.translate(0, -100);
if(i==3)g.translate(0, -150);
if(i==4)g.translate(0, -200);
if(i==5)g.translate(0, -250);
if(i==6)g.translate(0, -300);
if(i==7)g.translate(0, -350);
if(i==8)g.translate(0, -400);
if(i==9)g.translate(0, -450);
if(i==10)g.translate(0, -500);
if(i==11)g.translate(0, -550);
}
g.translate(100, -100);
for(int i=0; i<nombrePetitCarres2Existants; i++){
if(i==0)g.translate(-50, 100);
if(i==1)g.translate(-100, 150);
if(i==2)g.translate(-100, 200);
if(i==3)g.translate(-100, 250);
if(i==4)g.translate(-100, 300);
if(i==5)g.translate(-100, 350);
if(i==6)g.translate(-100, 400);
if(i==7)g.translate(-100, 450);
if(i==8)g.translate(-100, 500);
if(i==9)g.translate(-100, 550);
if(i==10)g.translate(-100, 600);
if(i==11)g.translate(-100, 650);
g.setColor(lesPetitsCarres2[i].couleur2);
g.fillRect(lesPetitsCarres2[i].cote, lesPetitsCarres2[i].cote, lesPetitsCarres2[i].cote, lesPetitsCarres2[i].cote);
g.translate(+100, -100);
if(i==1)g.translate(0, -50);
if(i==2)g.translate(0, -100);
if(i==3)g.translate(0, -150);
if(i==4)g.translate(0, -200);
if(i==5)g.translate(0, -250);
if(i==6)g.translate(0, -300);
if(i==7)g.translate(0, -350);
if(i==8)g.translate(0, -400);
if(i==9)g.translate(0, -450);
if(i==10)g.translate(0, -500);
if(i==11)g.translate(0, -550);
}
for(int i=0; i<nombrePetitCarres3Existants; i++){
if(i==0)g.translate(-150, 125);
if(i==1)g.translate(-100, 50);
if(i==2)g.translate(-100, 100);
if(i==3)g.translate(-100, 150);
if(i==4)g.translate(-100, 200);
if(i==5)g.translate(-100, 250);
if(i==6)g.translate(-100, 300);
if(i==7)g.translate(-100, 350);
if(i==8)g.translate(-100, 400);
if(i==9)g.translate(-100, 450);
if(i==10)g.translate(-100, 500);
if(i==11)g.translate(-100, 550);
g.setColor(lesPetitsCarres3[i].couleur3);
g.fillRect(lesPetitsCarres3[i].cote, lesPetitsCarres3[i].cote, lesPetitsCarres3[i].cote, lesPetitsCarres3[i].cote);
g.translate(+100, 0);
if(i==1)g.translate(0, -50);
if(i==2)g.translate(0, -100);
if(i==3)g.translate(0, -150);
if(i==4)g.translate(0, -200);
if(i==5)g.translate(0, -250);
if(i==6)g.translate(0, -300);
if(i==7)g.translate(0, -350);
if(i==8)g.translate(0, -400);
if(i==9)g.translate(0, -450);
if(i==10)g.translate(0, -500);
if(i==11)g.translate(0, -550);
}
for(int i=0; i<nombrePetitCarres4Existants; i++){
if(i==0)g.translate(-50, 0);
if(i==1)g.translate(0, 50);
if(i==2)g.translate(0, 50);
if(i==3)g.translate(0, 50);
if(i==4)g.translate(0, 50);
if(i==5)g.translate(0, 50);
if(i==6)g.translate(0, 50);
if(i==7)g.translate(0, 50);
if(i==8)g.translate(0, 50);
if(i==9)g.translate(0, 50);
if(i==10)g.translate(0, 50);
if(i==11)g.translate(0, 50);
g.setColor(lesPetitsCarres4[i].couleur4);
g.fillRect(lesPetitsCarres4[i].cote, lesPetitsCarres4[i].cote, lesPetitsCarres4[i].cote, lesPetitsCarres4[i].cote);
}
}
}
private ImageIcon smile;
}
public class forum_1
{ public static void main (String args[]){
// Affichage dans main de la fenetre
MaFenetre_3 fen = new MaFenetre_3() ;
fen.setVisible(true) ;
// huit couleurs, donc entre zéro et sept
// définitions pour le choix de l'ordi des maxi et mini
int lower = 0;
int higher = 7;
// Définition des variables
int random1;
int random2;
int random3;
int random4;
int choice1;
int choice2;
int choice3;
int choice4;
{
// Choix de l'ordinateur au hasard de quatre chiffres entre zéro et sept, avec la méthode Math.random
random1 = (int)(Math.random() * (higher-lower)) + lower;
random2 = (int)(Math.random() * (higher-lower)) + lower;
random3 = (int)(Math.random() * (higher-lower)) + lower;
random4 = (int)(Math.random() * (higher-lower)) + lower;
// random1 = 2;
// random2 = 4;
// random3 = 5;
// random4 = 6;
// Boucle pour jouer au mastermind
for(int indice=0; indice<12; indice ++){
// boite de dialogue où l'utilisateur doit entrer un chiffre
choice1=Integer.parseInt(JOptionPane.showInputDialog("donnez votre choix 1:"));
// boucle si il y a une erreur par rapport au chiffre entré
while(choice1<0|choice1>7){
JOptionPane.showMessageDialog(fen, " Chiffre entré incorrect.\n Rappel, entrez un chiffre entre zéro et sept",
"Message d'erreur",JOptionPane.ERROR_MESSAGE);
choice1=Integer.parseInt(JOptionPane.showInputDialog("donnez votre choix 1:"));
}
//choix 2
choice2=Integer.parseInt(JOptionPane.showInputDialog("donnez votre choix 2:"));
while(choice2<0|choice2>7){
JOptionPane.showMessageDialog(fen, " Chiffre entré incorrect.\n Rappel, entrez un chiffre entre zéro et sept",
"Message d'erreur",JOptionPane.ERROR_MESSAGE);
choice2=Integer.parseInt(JOptionPane.showInputDialog("donnez votre choix 2:"));}
// choix 3
choice3=Integer.parseInt(JOptionPane.showInputDialog("donnez votre choix 3:"));
while(choice3<0|choice3>7){
JOptionPane.showMessageDialog(fen, " Chiffre entré incorrect.\n Rappel, entrez un chiffre entre zéro et sept",
"Message d'erreur",JOptionPane.ERROR_MESSAGE);
choice3=Integer.parseInt(JOptionPane.showInputDialog("donnez votre choix 3:"));}
// choix 4
choice4=Integer.parseInt(JOptionPane.showInputDialog("donnez votre choix 4:"));
while(choice4<0|choice4>7){
JOptionPane.showMessageDialog(fen, " Chiffre entré incorrect.\n Rappel, entrez un chiffre entre zéro et sept",
"Message d'erreur",JOptionPane.ERROR_MESSAGE);
choice4=Integer.parseInt(JOptionPane.showInputDialog("donnez votre choix 4:"));}
System.out.println(+choice1+""+choice2+""+choice3+""+choice4);
// recuperer le panneau cree
Paneau_3 copiePanneau = (Paneau_3) fen.getContentPane().getComponent(0);
Carre1 nouveau1 = new Carre1(); // Un carre de plus
Carre2 nouveau2 = new Carre2();
Carre3 nouveau3 = new Carre3();
Carre4 nouveau4 = new Carre4();
copiePanneau.repaint();
switch(choice1){
case 0:nouveau1.remplir(100, 100, Color.white, 0); break;
case 1:nouveau1.remplir(100, 100, Color.yellow, 1);break;
case 2:nouveau1.remplir(100, 100, Color.orange, 2);break;
case 3:nouveau1.remplir(100, 100, Color.pink, 3);break;
case 4:nouveau1.remplir(100, 100, Color.red, 4);break;
case 5:nouveau1.remplir(100, 100, Color.magenta, 5);break;
case 6:nouveau1.remplir(100, 100, Color.green, 6);break;
case 7:nouveau1.remplir(100, 100, Color.blue, 7);break;
}
switch(choice2){
case 0:nouveau2.remplir(100, 100, Color.white, 0);break;
case 1:nouveau2.remplir(100, 100, Color.yellow, 1);break;
case 2:nouveau2.remplir(100, 100, Color.orange, 2);break;
case 3:nouveau2.remplir(100, 100, Color.pink, 3);break;
case 4:nouveau2.remplir(100, 100, Color.red, 4);break;
case 5:nouveau2.remplir(100, 100, Color.magenta, 5);break;
case 6:nouveau2.remplir(100, 100, Color.green, 6);break;
case 7:nouveau2.remplir(100, 100, Color.blue, 7);break;
}
switch(choice3){
case 0:nouveau3.remplir(100, 100, Color.white, 0);break;
case 1:nouveau3.remplir(100, 100, Color.yellow, 1);break;
case 2:nouveau3.remplir(100, 100, Color.orange, 2);break;
case 3:nouveau3.remplir(100, 100, Color.pink, 3);break;
case 4:nouveau3.remplir(100, 100, Color.red,4);break;
case 5:nouveau3.remplir(100, 100, Color.magenta, 5);break;
case 6:nouveau3.remplir(100, 100, Color.green, 6);break;
case 7:nouveau3.remplir(100, 100, Color.blue, 7);break;
}
switch(choice4){
case 0:nouveau4.remplir(100, 100, Color.white, 0);break;
case 1:nouveau4.remplir(100, 100, Color.yellow, 1);break;
case 2:nouveau4.remplir(100, 100, Color.orange, 2);break;
case 3:nouveau4.remplir(100, 100, Color.pink, 3);break;
case 4:nouveau4.remplir(100, 100, Color.red, 4);break;
case 5:nouveau4.remplir(100, 100, Color.magenta, 5);break;
case 6:nouveau4.remplir(100, 100, Color.green, 6);break;
case 7:nouveau4.remplir(100, 100, Color.blue, 7);break;
}
PetitCarre1 nouveau01= new PetitCarre1();
PetitCarre2 nouveau02= new PetitCarre2();
PetitCarre3 nouveau03= new PetitCarre3();
PetitCarre4 nouveau04= new PetitCarre4();
{copiePanneau.nombreCarres1Existants++;
copiePanneau.lesCarres1[copiePanneau.nombreCarres1Existants-1] = nouveau1;}
{copiePanneau.nombreCarres2Existants++;
copiePanneau.lesCarres2[copiePanneau.nombreCarres2Existants-1] = nouveau2;}
{copiePanneau.nombreCarres3Existants++;
copiePanneau.lesCarres3[copiePanneau.nombreCarres3Existants-1] = nouveau3;}
{copiePanneau.nombreCarres4Existants++;
copiePanneau.lesCarres4[copiePanneau.nombreCarres4Existants-1] = nouveau4;}
// Vérification des propositions données par l'utilisateur
// choix 1
if (choice1==random1){
nouveau01.remplir(100,100,Color.red);
}
else{
if( choice1==random2&choice2!=random2|choice1==random3&choice3!=random3|
choice1==random4&choice4!=random4){
nouveau01.remplir(100,100,Color.white);
}
else{nouveau01.remplir(100,100,Color.gray);
}
}
//choix 2
if (choice2==random2){
nouveau02.remplir(100,100,Color.red);
}
else{
if( choice2==random1&choice1!=random1|
choice2==random3&choice1!=random3&choice3!=random3|
choice2==random3&choice1==random1&choice1==random3&choice3!=random3|
choice2==random4&choice1!=random4&choice4!=random4|
choice2==random4&choice1==random1&choice1==random4&choice4!=random4|
choice2==random4&choice1==random3&choice1==random4&choice4!=random4&choice3!=random3|//changmt
choice2==random4&choice3==random3&choice3==random4&choice4!=random4&choice1!=random4){
nouveau02.remplir(100,100,Color.white);
}
else{nouveau02.remplir(100,100,Color.gray);
}
}
//choix 3
if (choice3==random3){
nouveau03.remplir(100,100,Color.red);
}
else{
if( choice3==random1&choice2!=random1&choice1!=random1|
choice3==random1&choice2==random1&choice2==random2&choice1!=random1|
choice3==random2&choice1!=random2&choice2!=random2|
choice3==random2&choice1==random1&choice1==random2&choice2!=random2|
choice3==random4&choice1!=random4&choice2!=random4&choice4!=random4|
choice3==random4&choice1==random1&choice1==random4&choice4!=random4&choice2!=random4|
choice3==random4&choice1==random1&choice1==random4&choice4!=random4&choice2==random2|
choice3==random4&choice1==random2&choice1==random4&choice4!=random4&choice2!=random2|
choice3==random4&choice2==random2&choice2==random4&choice4!=random4&choice1!=random4|
choice3==random4&choice2==random1&choice2==random4&choice4!=random4&choice1!=random1){
nouveau03.remplir(100,100,Color.white);
}
else{nouveau03.remplir(100,100,Color.gray);
}
}
//choix 4
if (choice4==random4){
nouveau04.remplir(100,100,Color.red);
}
else{
if( choice4==random1&choice2!=random1&choice3!=random1&choice1!=random1|
choice4==random1&choice2==random1&choice2==random2&choice1!=random1&choice3!=random1|
choice4==random1&choice2==random1&choice2==random2&choice1!=random1&choice3==random1&choice3==random3|
choice4==random1&choice3==random1&choice3==random3&choice1!=random1&choice2!=random1|
choice4==random2&choice1!=random2&choice3!=random2&choice2!=random2|
choice4==random2&choice1==random1&choice1==random2&choice2!=random2&choice3!=random2|
choice4==random2&choice1==random1&choice1==random2&choice2!=random2&choice3==random2&choice3==random3|
choice4==random2&choice3==random2&choice3==random3&choice2!=random2&choice1!=random2|
choice4==random2&choice3==random2&choice3==random1&choice1!=random1&choice2!=random2|//changmt
choice4==random3&choice1!=random3&choice2!=random3&choice3!=random3|
choice4==random3&choice1==random1&choice1==random3&choice3!=random3&choice2!=random3|//changmt
choice4==random3&choice1==random2&choice1==random3&choice3!=random3&choice2!=random2|
choice4==random3&choice2==random2&choice2==random3&choice3!=random3|
choice4==random3&choice2==random1&choice2==random3&choice1!=random1&choice3!=random3){//changmt
nouveau04.remplir(100,100,Color.white);
}
else{nouveau04.remplir(100,100,Color.gray);
}
copiePanneau.nombrePetitCarres1Existants++;
copiePanneau.lesPetitsCarres1[copiePanneau.nombrePetitCarres1Existants-1] = nouveau01;
copiePanneau.nombrePetitCarres2Existants++;
copiePanneau.lesPetitsCarres2[copiePanneau.nombrePetitCarres2Existants-1] = nouveau02;
copiePanneau.nombrePetitCarres3Existants++;
copiePanneau.lesPetitsCarres3[copiePanneau.nombrePetitCarres3Existants-1] = nouveau03;
copiePanneau.nombrePetitCarres4Existants++;
copiePanneau.lesPetitsCarres4[copiePanneau.nombrePetitCarres4Existants-1] = nouveau04;
copiePanneau.repaint();
}
// Si l'utilisateur a trouvé la réponse
if(choice1==random1&choice2==random2&choice3==random3&choice4==random4)
{
JOptionPane jop1;
jop1=new JOptionPane();
ImageIcon img = new ImageIcon("Bravo.gif");
jop1.showMessageDialog(null,null,"Félicitations",JOptionPane.INFORMATION_MESSAGE,img);
// arrêt du programme
System.exit(1);}
else{}
// Si l'utilisateur dépasse 12 essais
if(indice==11){JOptionPane jop2;
jop2=new JOptionPane();
ImageIcon img = new ImageIcon("img_0-557_smile_triste.png");
jop2.showMessageDialog(null,"Nombre maximal de tentatives dépassé"," Perdu...",JOptionPane.INFORMATION_MESSAGE,img);
JOptionPane.showMessageDialog(null,"La réponse était "+random1+ random2+random3+random4);
System.exit(1);
}
else{
// Question à la fin de chaque essai de l'utilisateur
int rep=JOptionPane.showConfirmDialog(null,"Voulez-vous continuer ?","Question importante",
JOptionPane.YES_NO_OPTION);
if (rep==0){}
else{
System.exit(1);}
}
}
}
}} |
Partager