Bonsoir, je créé dans le cadre de mes cours de développement un Puissance 4.
J'ai choisi d'utiliser Java pour ce faire.
Voilà le code jusqu'à présent :
int w = 7, h = 6, bs = 100, joueur = 1;
int [] [] board = new int [h] [w] ;
void setup() {
size (700, 600); ellipseMode (CORNER);
}
int p(int y, int x) {
return (y<0||x<0||y>=h||x>=w)?0:board[y][x];
}
int getWinner() {//rows, columns, diagonals
for(int y=0;y<h;y++)for(int x=0;x<w;x++)
if(p(y,x)!=0&&p(y,x)==p(y,x+1)&&p(y,x)==p(y,x+2)&&p(y,x)==p(y,x+3)) return p(y,x) ;
for(int y=0;y<h;y++)for(int x=0;x<w;x++)
if(p(y,x)!=0&&p(y,x)==p(y+1,x)&&p(y,x)==p(y+2,x)&&p(y,x)==p(y+3,x)) return p(y,x) ;
for(int y=0;y<h;y++)for(int x=0;x<w;x++)for(int d=-1;d<=1;d+=2)
if(p(y,x)!=0&&p(y,x)==p (y+1*d,x+1)&&p(y,x)==p (y+2*d,x+2)&&p(y,x)==p(y+3*d,x+3)) return p(y,x);
for(int y=0;y<h;y++)for(int x=0;x<w;x++)if(p(y,x)==0) return 0;
return -1; //tie
}
int nextSpace (int x) {
for(int y=h-1;y>=0;y--) if (board[y][x]==0) return y;
return -1;
}
void mousePressed() {
int x = mouseX / bs, y = nextSpace(x) ;
if(y>=0) {
board [y][x] = joueur;
joueur = joueur==1?2:1;
}
}
void draw() {
if (getWinner()==0) {
for(int j=0;j<h;j++) for(int i=0;i<w;i++){
fill (255);
rect(i*bs, j*bs,bs,bs);
if(board[j][i]>0){
fill(board[j][i]==1?255:255, board[j][i]==2?255:0,0);
ellipse (i*bs,j*bs,bs,bs);
}
}
}else {
background(0); fill(255);text("Le gagnant est : "+getWinner()+". Appuyer sur espace pour relancer", width/2,height/2);
if(keyPressed&&key==' ') {
joueur=1; for (int y=0;y<h;y++) for (int x=0;x<w;x++) board [y][x]=0;
}
}
}
Sauriez vous comment faire pour que quand le j1 joue le curseur de la souris soit rouge (ou remplacé par un rond rouge) et de même pour le j2 mais en jaune ?
Merci d'avance !