This site requires JavaScript, please enable it in your browser!
Greenfoot back
Fargesia
Fargesia wrote ...

2019/2/24

Chess Games

Fargesia Fargesia

2019/2/24

#
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:
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;
        }
    }    
}
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!
danpost danpost

2019/2/24

#
A move is a behavior of the game itself. Sure, any piece (from the side, WHITE or BLACK, whose turn it is) may be moved; but, a move is part of the game itself. As such, it would be best for your world to detect and act upon clicks. In pseudo-code:
if (Greenfoot.mouseClicked(null))
{
    Object obj = Greenfoot.getMouseInfo().getActor();
    if (clickedOn == null) // using :   private Actor clickedOn;  // for piece to move
    {  // here if clicking a piece to move
        if ((obj != null) && (obj instanceof Piece) && ((turn == WHITE) == (obj instanceof WhitePiece))) setPieceToMove((Actor)obj);
    }
    else
    {  //possible destination click
        if ((obj != null) && (obj instanceof DestinationSquare)) movePiece(clickedOn, (Actor)obj);
    }
}
The dirty work would be done in the setPieceToMove and movePiece method (adding and removing destination objects, setting appropriate field values and controlling the progress of the game).
Fargesia Fargesia

2019/2/24

#
Ok thank for the tip, I'll do it this way but I still got my problem, which is what you call the dirty work, how do I create multiple time the same object and delete it later without creating an object for every possible move like I did?
danpost danpost

2019/2/25

#
Fargesia wrote...
how do I create multiple time the same object and delete it later without creating an object for every possible move like I did?
If you are to "highlight" all possible moves for the piece that is to move, it is by necessity that every possible square has its own object that can be clicked on. There are things you can do to help reduce the amount of code needed for that (loops and clever use of Maths, for examples).
You need to login to post a reply.