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

2012/10/29

How can I do the alternative switching in the Chess Game

CKL CKL

2012/10/29

#
I want to write a chess program. But I dont know how to do the Two players take turn alternately which means if I clicked one chess and move one step then it cannot move until the player slect his chess and move it. Another techni barrier is that I dont want to set which side begin first. I want the first mouse clicked on which color of chess then the player who choose the color should start and another.
CKL CKL

2012/10/29

#
Forget to mention one more thing is that I want the player can select chess with many times until he move one chess then he should pass to next one. And another problem is that if I add two Object in the same cell with addObject function , how can I select the object in the below and remove the upper object with just using mouseClicked as I have tried to combine both but once I remove the upper object, I hv to click one more time to select the object originally below . All I want is One click and the upper object disappear and the below object is being selected thx
danpost danpost

2012/10/29

#
Normally, one would use a move counter in the world class to both keep track of how many moves have been played and to determine whose move it is. If it was even, it would be white's turn; if odd, then black's turn. You can still use this, but will need another int variable to add an 'offset' for whose turn it is to move. It will be zero or one, so you can just add it to the move count and use the same rule. You can initialize the offset variable to -1 to signify any move on the board from either player can be made; and when made set the variable to zero or one as needed. For selecting lower piece, you can use one of the get...Object(s) methods in the Actor class to find the other piece. If one exists select it and remove the piece clicked on. If one does not exist, then just select the piece clicked on.
CKL CKL

2012/10/30

#
I have typed some code this is extract from my GreenArrow class
    public void act() 
    {
    if(Greenfoot.mouseClicked(this)){
       if (getOneIntersectingObject(Chess.class) != null)
       { ChessWorld.lastChessClicked = (Chess) getOneIntersectingObject(Chess.class);
           
         
         getWorld().removeObjects(getWorld().getObjects(border.class));
         getWorld().removeObjects(getWorld().getObjects(RedArrow.class));
         getWorld().removeObjects(getWorld().getObjects(GreenArrow.class));

        }  
           else if (getOneIntersectingObject(Chess.class) == null){
        getWorld().removeObjects(getWorld().getObjects(border.class));
        getWorld().removeObjects(getWorld().getObjects(RedArrow.class));
        getWorld().removeObjects(getWorld().getObjects(GreenArrow.class));

Chess.act();
                      
        }
    }
    }
In my chess class
    public void act() 
    {
         
      if (Greenfoot.mouseClicked(this))  
      {  
                    
          ChessWorld.lastChessClicked = this;     
           
            if (isFlipped == true)  
            {
                getWorld().removeObjects(getWorld().getObjects(border.class));
                getWorld().removeObjects(getWorld().getObjects(RedArrow.class));
                getWorld().removeObjects(getWorld().getObjects(GreenArrow.class));
                getWorld().addObject(new border(),getX(),getY());
                    if ((left() == false) && (getX() >0))
                    {.......
CKL CKL

2012/10/30

#
I would like to click the green arrow means that I click the chess How can I get this done?? thanks
CKL CKL

2012/10/30

#
Let me clarify my point, I have finished the alternative switch in players. Thank so much. The only thing I still dont know is that how to make the mouse click on upper object as selecting the object below and remove the object above. And I use the getOneIntersectingObject(Chess.class) seems doesn't work , how can I link the object in the below to the act() in Chess class and also skip the process of clicking the chess again
danpost danpost

2012/10/30

#
If you are trying to 'drag' the piece under the green arrow, you cannot. You cannot change the actor of a mouse action. There are ways around it though. (1) put the piece above the green arrow and add code to the act of the pieces to make invisible (or semi-translucent) when intersecting a greenarrow object. This way the greenarrow object shows and you can click the actual piece. When the piece is pressed on to drag, remove the intersecting greenarrow object. (2) put a boolean in the piece class to track if beingdragged or not. When the greenarrow is pressed on, get a reference to the piece, set beingdragged to true and remove the greenarrow object. In the act of the pieces, add code that if beingdragged, follow mouse. When mouseClicked is detected while beingdragged, set beingdragged to false.
CKL CKL

2012/10/31

#
how to get a reference to the piece use getOneIntersectingObject???
CKL CKL

2012/10/31

#
i have just tackled it. Thank you for your help.
CKL CKL

2012/10/31

#
Um..I would like to ask how can I make a game save and reload it by producing a txt file. Is there any sample that I can take reference?
danpost danpost

2012/11/2

#
It is certainly possible; however, be advised that you will not be able to do so on the site. You will have to use only what storage is available with UserInfo. If you are still interested in how, repost. And let us know whether you looking for a way to do so on the site or on your system (or both).
CKL CKL

2012/11/2

#
Thanks I have post the question, and I have finished the "save" part, just handling with the "load" part.
You need to login to post a reply.