Results 1 to 16 of 16
  1. #1
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default Wait() and Notify()

    I have a chess board that I would like to add a function to. When I get a pawn to the opposite side of the board I would like to swap it out for a different piece. I have a two dimensional array board, [0][0] being the first box, and [7][7] for the last. I also have a "captured" section, one for black, one for white. Captured pieces go there.

    What I would like to use is a wait to allow a player to press the piece he/she would like back. Basically, I need a wait command of some type that will wait for one of the captured buttons to be pressed, only when a pawn to get the opposing side.

    I think wait() - notify() will work, but I do not understand how to use them properly. I tried googling them, and looking in forums, to no avail.

    Can anyone lend some insight?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,001
    Rep Power
    10

    Default

    I think you're looking for a KeyListener or a MouseListener of some kind. I don't really see how wait() or notify() will help you here.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    I understand so little as to how they work that I don't know if it is indeed what I need. I just want something that will allow for another JButton to be pressed before continuing. What can make that happen?
    Last edited by SiX; 07-26-2011 at 08:18 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    I just want something that will allow for another button to be pressed before continuing. What can make that happen?
    You can let the normal GUI thread handle this. You want our code to do something when a specific button is pressed.
    You can disable all the other buttons except for the one that must be pressed. The GUI will wait forever until the user presses the enabled button.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,001
    Rep Power
    10

    Default

    Without knowing how your game loop works, I have no idea. Are you using a thread? A Timer? Something else?

    I suppose you could use wait() and notify(), but you're probably better off with a simple boolean that you check in the game loop and set when the JButton is pressed.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    At this point in the cycle I would like to be able to, once the reaches the right spot, for the game to pause and await the next command action to come through. I intend to disable the other JButtons during this wait, that's not really the issue. I would like the game to pause as the pawn reaches the spot, wait for the captured button to be pressed, and for the game to continue.

    The only thing I am missing from this is some kind of mechanism to "pause" the game, with the condition being that it's a pawn piece that has reached either [0][0] through [7][0] or [0][7] through [7][7]. I have everything worked out in my head, I just need the "pause and wait for wake up" portion.

  7. #7
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    Java Code:
    public void actionPerformed(ActionEvent e)
        {
            String cmd= e.getActionCommand();
            System.out.println("User clicked on " + cmd);
            
            
            if(!cmd.substring(0,1).equals("b") && !cmd.substring(0,1).equals("w"))
            {
                int ifCheckX = Integer.parseInt(cmd.substring(0,1));
                int ifCheckY = Integer.parseInt(cmd.substring(1,2));
            
            
                //System.out.println("" + r.square[ifCheckX][ifCheckY]);
                if((this.numberOfClicks == 2) || ((r.square[ifCheckX][ifCheckY].substring(0,1).equals("B") && r.turn == true) || (r.square[ifCheckX][ifCheckY].substring(0,1).equals("W") && r.turn == false)))
                {
                    if(numberOfClicks == 1)
                    {
                        this.startX = Integer.parseInt(cmd.substring(0,1));
                        this.startY = Integer.parseInt(cmd.substring(1,2));
                        //System.out.println("Number of Clicks " + numberOfClicks);
                        this.numberOfClicks++;
                    }
                    else if(numberOfClicks == 2)
                    {
                        this.finishX = Integer.parseInt(cmd.substring(0,1));
                        this.finishY = Integer.parseInt(cmd.substring(1,2));
                        if(r.isMoveValid(startX, startY, finishX, finishY) == true)
                        {
                            if(!r.square[finishX][finishY].equals("Empty") && !r.square[finishX][finishY].substring(6,7).equals("P"))
                            {
                                if(r.square[finishX][finishY].substring(0,1).equals("B"))
                                {
                                    whiteCapturesBlack(finishX, finishY);
                                }
                                if(r.square[finishX][finishY].substring(0,1).equals("W"))
                                {
                                    blackCapturesWhite(finishX, finishY);
                                }
                            }
                            if((r.square[startX][startY].equals("White Pawn") && (finishX == 0)) || (r.square[startX][startY].equals("Black Pawn") && (finishX == 7)))
                            {
                                wait();
                                returnCaptured(cmd, finishX, finishY);                                        <~~~~~~~ where I would like the wait, so i can force numberOfClicks to 2 and pass the next
                            }                                                                                                               JButton string to the finish location.
                            r.move(startX, startY, finishX, finishY);
                            r.turn = !r.turn;
                            turnTF = !turnTF;
                            guiUpdate();
                            this.numberOfClicks = 1;
                        }
                        else
                        {
                            this.numberOfClicks = 1;
                            
                            g.add(this.fail);
                        }
                    }
                }
            }
        }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    I would like the game to pause as the pawn reaches the spot, wait for the captured button to be pressed, and for the game to continue.
    That is EXACTLY what using the GUI would do. When the pawn is moved to the last row, then state of the game changes and nothing happens until the new piece is chosen. What would the "game" do otherwise? Each side must wait for the other to make its move.

  9. #9
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    So, a player clicks once for the piece they'd like to move. The player then picks another spot. If the move is valid and empty, move the piece. If the move is valid and there is a piece of the opposite color, "capture" the piece (which puts it in a JButton on their side). The last one is my issue and is as follows:

    If the piece is a pawn, and has made it to the other side of the board, I want the player to able to click on the JButton with the piece they want returned. Now, the first click selected the pawn, no issue there. The second click selects the destination. Now, I have a function designed to take the command, plus two ints (below). If a pawn is at the first click, and the destination is in the right spot, assuming that player has had at least one captured piece, wait for the next JButton to be pressed (others will be disabled) which will be a captured piece, which sends a cmd with the piece name. The guiUpdate method deals with changing the gui, so I just need to wait for the next jbutton with the condition being that which I have mentioned ^_^

    Java Code:
    public void returnCaptured(String cmd, int xSwap, int ySwap)
        {
            if(cmd.substring(0,1).equals("B"))
            {
                if(cmd.substring(6,7).equals("H"))
                {
                    r.square[xSwap][ySwap] = cmd.substring(0,5) + " Horse";
                    blackCapture[Integer.parseInt(cmd.substring(1,2))].setActionCommand(blackCapture[Integer.parseInt(cmd.substring(1,2))].getActionCommand().substring(0,2));
                    blackCapture[Integer.parseInt(cmd.substring(1,2))].setIcon(null);
                }
                if(cmd.substring(6,7).equals("B"))
                {
                    r.square[xSwap][ySwap] = cmd.substring(0,5) + " Bishop";
                    blackCapture[Integer.parseInt(cmd.substring(1,2))].setActionCommand(blackCapture[Integer.parseInt(cmd.substring(1,2))].getActionCommand().substring(0,2));
                    blackCapture[Integer.parseInt(cmd.substring(1,2))].setIcon(null);
                }
                if(cmd.substring(6,7).equals("R"))
                {
                    r.square[xSwap][ySwap] = cmd.substring(0,5) + " Rook";
                    blackCapture[Integer.parseInt(cmd.substring(1,2))].setActionCommand(blackCapture[Integer.parseInt(cmd.substring(1,2))].getActionCommand().substring(0,2));
                    blackCapture[Integer.parseInt(cmd.substring(1,2))].setIcon(null);
                }
                if(cmd.substring(6,7).equals("Q"))
                {
                    r.square[xSwap][ySwap] = cmd.substring(0,5) + " Queen";
                    blackCapture[Integer.parseInt(cmd.substring(1,2))].setActionCommand(blackCapture[Integer.parseInt(cmd.substring(1,2))].getActionCommand().substring(0,2));
                    blackCapture[Integer.parseInt(cmd.substring(1,2))].setIcon(null);
                }
            }
            if(cmd.substring(0,1).equals("W"))
            {
                if(cmd.substring(6,7).equals("H"))
                {
                    r.square[xSwap][ySwap] = cmd.substring(0,5) + " Horse";
                    whiteCapture[Integer.parseInt(cmd.substring(1,2))].setActionCommand(whiteCapture[Integer.parseInt(cmd.substring(1,2))].getActionCommand().substring(0,2));
                    whiteCapture[Integer.parseInt(cmd.substring(1,2))].setIcon(null);
                }
                if(cmd.substring(6,7).equals("B"))
                {
                    r.square[xSwap][ySwap] = cmd.substring(0,5) + " Bishop";
                    whiteCapture[Integer.parseInt(cmd.substring(1,2))].setActionCommand(whiteCapture[Integer.parseInt(cmd.substring(1,2))].getActionCommand().substring(0,2));
                    whiteCapture[Integer.parseInt(cmd.substring(1,2))].setIcon(null);
                }
                if(cmd.substring(6,7).equals("R"))
                {
                    r.square[xSwap][ySwap] = cmd.substring(0,5) + " Rook";
                    whiteCapture[Integer.parseInt(cmd.substring(1,2))].setActionCommand(whiteCapture[Integer.parseInt(cmd.substring(1,2))].getActionCommand().substring(0,2));
                    whiteCapture[Integer.parseInt(cmd.substring(1,2))].setIcon(null);
                }
                if(cmd.substring(6,7).equals("Q"))
                {
                    r.square[xSwap][ySwap] = cmd.substring(0,5) + " Queen";
                    whiteCapture[Integer.parseInt(cmd.substring(1,2))].setActionCommand(whiteCapture[Integer.parseInt(cmd.substring(1,2))].getActionCommand().substring(0,2));
                    whiteCapture[Integer.parseInt(cmd.substring(1,2))].setIcon(null);
                }
            }
        }
    Basically, it takes two ints and swaps the piece with the one passed to it. I want to use this function to do it. I have everything worked out in my head, just need some "wait".

    If there is a better way, please, do tell.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    If the piece is a pawn, and has made it to the other side of the board, I want the player to able to click on the JButton with the piece they want returned
    Yes, that makes sense to do when a pawn has reached the 8th row. When the pawn has reached the 8th row, the game pauses until the user has selected the piece he wants to change to. The pause is effected by the GUI waiting for the user to make the selection by pressing a button.

    returnCaptured
    This is an interesting name for a method. Why did you chose that?
    Do you know it is possible to have 9 Queens on the board by changing the 8 pawns each for a queen plus the original one?
    You can chose whatever piece you want.

  11. #11
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    Truth be told, I did not know that. Alright. I can adjust that later, but I am in the same issue. How should I go about this?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    How do you currently wait for the user to make a decision? And then react to that decision?


    You have too many magic(hard coded) numbers in your code. WAAAAAAAAAAAAAAAY too many.
    Your code is LOCKED into the displacements in a String.
    Any change to what is in that String will destroy your code.
    Last edited by Norm; 07-26-2011 at 10:55 PM.

  13. #13
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    I thought it was more elegant to have while statements make my strings for me, hence the magic numbers. I don't need to modify anything, just need to potentially add a boolean that checks piece string and location and if true wait for another cmd and reset back to default when the boolean was false. OR I need the GUI to wait after the second click for the next command, and pass the String piece that is given from the command to the returnedCaptured. So is there such a function?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    add a boolean that checks piece string and location and if true wait for another cmd and reset back to default when the boolean was false.
    A guestion about how the game is being played. Are there threads that are moving the pieces around without telling the rest of the program what they are doing? So you need a thread that is checking what this other thread has done.
    That is the only case I can see for how you have designed this.

    I need the GUI to wait after the second click for the next command
    That is how GUI works. It responds to user clicks.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    You must like to type to have entered some many repeated statements. Here's a newer version of your code:
    Java Code:
       public void returnCaptured(String cmd, int xSwap, int ySwap)     {
            <???>[] pieceCapt = null;
            if(cmd.substring(0,1).equals("B"))
               pieceCapt = blackCapture;
            else if(cmd.substring(0,1).equals("W"))
               pieceCapt = whiteCapture;
            else {
               System.out.println("unknown  cmd=" + cmd);
               ???? throw an exception or ???
            }
    
             makeCapture(pieceCapt, cmd);
        }
             
        private   void makeCapture(<??>[] pieceCapt, String cmd) {
                // First pick up the bits and pieces we need
                String pieceId = cmd.substring(6,7);
                String sqrPrefix = cmd.substring(0,5);
                int pieceIdx = Integer.parseInt(cmd.substring(1,2));
                String newCmd = pieceCapture[pieceIdx)].getActionCommand().substring(0,2));
    
                if(pieceId.equals("H"))
                {
                    r.square[xSwap][ySwap] = sqrPrefix + " Horse";
                    pieceCapture[pieceIdx)].setActionCommand(newCmd);
                    pieceCapture[pieceIdx)].setIcon(null);
                }
                else if(pieceId.equals("B"))
                {
                    r.square[xSwap][ySwap] = sqrPrefix + " Bishop";
                    pieceCapture[pieceIdx)].setActionCommand(newCmd);
                    pieceCapture[pieceIdx)].setIcon(null);
                }
                else if(pieceId.equals("R"))
                {
                    r.square[xSwap][ySwap] = sqrPrefix + " Rook";
                    pieceCapture[pieceIdx)].setActionCommand(newCmd);
                    pieceCapture[pieceIdx)].setIcon(null);
                }
                else if(pieceId.equals("Q"))
                {
                    r.square[xSwap][ySwap] = sqrPrefix + " Queen";
                    pieceCapture[pieceIdx)].setActionCommand(newCmd);
                    pieceCapture[pieceIdx)].setIcon(null);
                }
                else {
                  System.out.println("unhandled cmd=" + cmd);
                }
          }

  16. #16
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    Thank you for your reply, and for spending time to clean that up.

Similar Threads

  1. wait() and notify()
    By jomypgeorge in forum New To Java
    Replies: 4
    Last Post: 02-15-2011, 09:58 AM
  2. Need help with wait and notify
    By mityay in forum Threads and Synchronization
    Replies: 3
    Last Post: 01-06-2011, 05:24 PM
  3. Need help with wait() and notify()
    By Mkaveli in forum Threads and Synchronization
    Replies: 2
    Last Post: 03-30-2010, 12:58 PM
  4. Wait() notify() implementation
    By georges in forum Advanced Java
    Replies: 4
    Last Post: 02-05-2010, 02:33 AM
  5. wait() and notify() trouble with UI
    By Atriamax in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-09-2009, 03:51 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •