0

Basically, I have this while loop that retrieves the co-ordinates of a button pressed (which is a chess piece) and goes through the loop to see if a move can be made.

However, the co-ordinates of the same chess piece are constantly being fed through so if the piece has no moves available, the same message keeps being displayed over and over again until a different button is pressed, and the same thing happens again if that piece has no moves. How would I go about only printing the same message once each time a piece is clicked?

I tried using a do while loop where the condition in the while loop was only check if the button has moves available if the value of the co-ordinates are different to the ones being fed in. But this made no difference?

        while(!correctMove){
            do{
                fromXCo = s.getFromXInt();
                fromYCo = s.getFromYInt();
                toXCo = s.getToXInt();
                toYCo = s.getToYInt();

                    p = board.getPiece(fromXCo, fromYCo);

                    //checks if occupied co-ordinate, if not goes back out the loop and asks again.
                    if (!board.occupied(fromXCo, fromYCo)){
                        System.out.println("Please choose occupied co-ordinates \n");
                        correctMove = false;
                    }

                    //checks if the colour is the same as the player's. If not, asks for co-ordinates again.
                    if (p.getColour() != getPieces().getColour()){
                        System.out.println("Please move your own pieces \n");
                        correctMove = false;
                    }

                    //tells player their selected piece
                    System.out.println("Your selected piece is " + p.getChar());

                    //looks through available moves for piece
                    moves = p.availableMoves();

                    //if no moves available, asks for new co-ordinates.
                    if (moves == null){
                        System.out.println("No moves available for this piece \n");
                        correctMove = false;
                    }
                    else {

                        Move found = null;

                        for( Move m : moves){
                            //checks if move can be done
                            if (m.ToX() == toXCo && m.ToY() == toYCo){
                                //if move is allowed- exit loop
                                found = m; 

                                correctMove = true;

                            }
                        }

                        if (found == null) {
                            //if move can't be, ask for new co-ordinates
                            System.out.println("This move is not legal \n");
                            correctMove = false;

                        }   
                    }
                }while(fromXCo != s.getFromXInt() && fromYCo != s.getFromYInt() && toXCo != s.getToXInt() && toYCo != s.getToYInt());
            }
3
  • 1
    A perfect case where you can re-do some logic in your program, or you can use some sentinel variable that exits the "while" loop, or you can use a goto and get criticizes for even suggesting it.... in 3...2...1... Commented May 13, 2015 at 15:34
  • You can exit a while loop immediately with the break statement. Commented May 13, 2015 at 15:35
  • Both comments so far very important. I'd add that instead of adding code, try removing and/or combining code to get to your solution (or at least closer to it...) Commented May 13, 2015 at 15:37

1 Answer 1

1

If I understand correctly the purpose of while is so if a legal piece with legal move is not selected user can repeat the selection. You won't need to have two while loops (the inner while seems to be acting just as an if). This is how I would go about it (pseudocode):

correctMove = false;
While(!correctMove) {
    square = inputNewSquare();
    if(isEmpty(square))
        "click on a piece"
    else if(pieceHasRightColor(square))
        "you can only move your own piece"
    else if(!pieceHasMove(square))
        "this piece has no legal move"
    else {
        //move(s) found, do stuff
        correctMove = true;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.