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
| import java.awt.Color;
import java.awt.Graphics;
public class Puissance4 {
/**
* @param args the command line arguments
*/
private int[][] grille;
private int joueur;
public Puissance4() {
grille = new int[7][6];
for (int ligne=0; ligne<6; ligne++) {
for (int colonne=0; colonne<7; colonne++) {
grille[colonne][ligne] = 0;
}
}
joueur = 1;
}
public int deposer(int colonne) {
for ( int ligne=0; ligne<6; ligne++)
{
if(grille[colonne-1][ligne]==0)
{
grille[colonne-1][ligne]=joueur;
joueur=(joueur%2)+1;
return ligne;
}
}
return -1;
}
public int positiony(int ligne){
return ligne*105+15;
}
public void paint(Graphics g, int x, int y){
if(afficher()==1){
g.fillOval(x, y, 20, 20);
g.setColor(Color.yellow);
g.drawOval(x, y, 20, 20);
}
else
if(afficher()==2){
g.fillOval(x, y, 20, 20);
g.setColor(Color.green);
g.drawOval(x, y, 20, 20);
} }
public int afficher(){
return joueur;
}
public int positionx(int colonne){
return 12+105*(colonne-1);
}
public void dessiner(Graphics c){
if(afficher()==1){
c.setColor(Color.yellow);
c.fillOval(positionx(1), positiony(deposer(1)), 80, 80);
}
else
if(afficher()==2){
c.setColor(Color.red);
c.fillOval(positionx(1), positiony(deposer(1)), 80, 80);
}
}
public String getCouleur(){
String couleur= "yellow";
if(afficher()==1)
couleur= "yellow";
else
if(afficher()==2)
couleur= "green";
return couleur;
}
public int Compter(int x,int y,int joueur,int dx,int dy){
// dx=0,dy=-1 vers le bas
// dx=-1,dy=0 vers la gauche
// dx=+1,dy=0 vers la droite
// dx=-1,dy=+1 vers la gauche en bas
// dx=+1,dy=+1 vers la droite en bas
// dx=-1,dy=-1 vers la gauche en haut
// dx=+1,dy=-1 vers la droite en haut
if((x>=0)&&(x<7)&&(y>=0)&&(y<6)){
if(grille[y][x]==joueur)
return 1+Compter(x+dx,y+dy,joueur,dx,dy);
else
return 0;
} else
return 0;
}
public boolean gagner(int x, int y) {
int bas=Compter(x,y,joueur,0,1) ;
int droite=Compter(x,y,joueur,1,0) ;
int gauche=Compter(x,y,joueur,-1,0) ;
int basGauche=Compter(x,y,joueur,-1,1) ;
int basDroite=Compter(x,y,joueur,1,1) ;
int hautGauche=Compter(x,y,joueur,-1,-1) ;
int hautDroite=Compter(x,y,joueur,1,-1) ;
if ( bas>=4)
return true ;
else
if ( droite + gauche-1 >=4)
return true ;
else
if ( basGauche+hautDroite-1>=4)
return true ;
else
if(basDroite+hautGauche>=4)
return true ;
else
return false ;
}
} |
Partager