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

2019/2/27

How can I check for a valid move in checkers?

Sedas Sedas

2019/2/27

#
I am making a checkers game and currently the player can move the piece to any part of the board, how can I stop this?
Sedas Sedas

2019/2/27

#
 if(Greenfoot.mouseDragged(this)){
            MouseInfo mouse=Greenfoot.getMouseInfo();
            
            if(!drag){
                drag=true;
                rx=getX()-mouse.getX();
                ry=getY()-mouse.getY();
                
                
                hitTheEdge();
                
               
            } else {
                setLocation(mouse.getX()+rx,mouse.getY()+ry);
                    hitTheEdge();
                
                }
               
            
                
                
            }
               
           
            }
            
           
        if(Greenfoot.mouseDragEnded(this)){
             drag=false;
         } 
danpost danpost

2019/2/27

#
Sedas wrote...
I am making a checkers game and currently the player can move the piece to any part of the board, how can I stop this? << Code Omitted >>
The first thing you would need to do is save the original location of the checker so that you can move it back when moved to an illegal square. With that rx and ry would be unnecessary. Next, thinking logically about how a checker should move, you have blue men moving down and red men moving up. A blue man will always move along the y-axis in a positive direction and a red man in a negative one. The change horizontally will be either plus or minus one when not capturing and plus or minus two when capturing. Kings, of course, do not have the uni-directional restriction along the y-axis and all pieces can multi-jump. So, a single piece must "know" its color (which is not apparent with the little amount of code given); it must also "know" whether it is a single man or a king (again, not apparent). With that information, a piece will "know" what checks to perform to determine whether a move is valid or not. I am not sure if hitTheEdge is needed or not. It would depend on your world dimensions and whether it is bound or not (not apparent).
You need to login to post a reply.