Hi guys!
I'm a beginner in coding and I've began a chess game, but I'm facing a problem. Whenever I click on a chessman, a green square appears on the cases he can move over. And then when the player clicks on a green square, the chessman moves over it. For the pawns, king or horses I got no problem, I just create an object for every case. But the queen for example, there would be too many objects to create, that wouldn't be optimised. Here's my king's code, so you can see how I did it so far:
I juste created an Object for every possible move, add them to a list, and put an if condition for every objects. It works but it's not optimised and for the queen it would be too long. So how can I automatically create an Object from a given class and whenever I click on an Object from this class, it removes all the objects from this class? Because another problem is that the command mouseClicked canno't have a class as an argument, only an object.
Thanks for your help!
import greenfoot.*;
import java.util.List;
import java.util.ArrayList;
public class roiBlanc extends Actor
{
int etat;
carreVert carre = new carreVert();
carreVert carre2 = new carreVert();
carreVert carre3 = new carreVert();
carreVert carre4 = new carreVert();
carreVert carre5 = new carreVert();
carreVert carre6 = new carreVert();
carreVert carre7 = new carreVert();
carreVert carre8 = new carreVert();
List liste = new ArrayList();
public void act()
{
GreenfootImage image = new GreenfootImage("roiBlanc.png");
image.scale(45, 60);
setImage(image);
liste.add(carre);
liste.add(carre2);
liste.add(carre3);
liste.add(carre4);
liste.add(carre5);
liste.add(carre6);
liste.add(carre7);
liste.add(carre8);
//Apparition possibiltés de mouvement
if(etat==0 && Greenfoot.mouseClicked(this)){
getWorld().addObject(carre, getX(), getY()-75);
getWorld().addObject(new carreVert(), getX()+75, getY()-75);
getWorld().addObject(carre3, getX()+75, getY());
getWorld().addObject(carre4, getX()+75, getY()+75);
getWorld().addObject(carre5, getX(), getY()+75);
getWorld().addObject(carre6, getX()-75, getY()+75);
getWorld().addObject(carre7, getX()-75, getY());
getWorld().addObject(carre8, getX()-75, getY()-75);
etat=1;
}
//Mouvements
if(etat==1 && Greenfoot.mouseClicked(carre)){
getWorld().removeObjects(liste);
setLocation(getX(), getY()-75);
etat=0;
}
if(etat==1 && Greenfoot.mouseClicked(carre2)){
getWorld().removeObjects(liste);
setLocation(getX()+75, getY()-75);
etat=0;
}
if(etat==1 && Greenfoot.mouseClicked(carre3)){
getWorld().removeObjects(liste);
setLocation(getX()+75, getY());
etat=0;
}
if(etat==1 && Greenfoot.mouseClicked(carre4)){
getWorld().removeObjects(liste);
setLocation(getX()+75, getY()+75);
etat=0;
}
if(etat==1 && Greenfoot.mouseClicked(carre5)){
getWorld().removeObjects(liste);
setLocation(getX(), getY()+75);
etat=0;
}
if(etat==1 && Greenfoot.mouseClicked(carre6)){
getWorld().removeObjects(liste);
setLocation(getX()-75, getY()+75);
etat=0;
}
if(etat==1 && Greenfoot.mouseClicked(carre7)){
getWorld().removeObjects(liste);
setLocation(getX()-75, getY());
etat=0;
}
if(etat==1 && Greenfoot.mouseClicked(carre8)){
getWorld().removeObjects(liste);
setLocation(getX()-75, getY()-75);
etat=0;
}
}
}
