1

I have an array of JButtons which are displayed on a window when it is loaded up. For some reason, when the window loads, the first button seems to be already selected and so puts my programme into an infinite loop.

I know that to stop this infinite loop I should change the condition in my while(true) loop, but am unsure what conditions to put in. So I thought another way round would be to figure out why one of the buttons is already selected.

actionPerformed method - this gets the name of the button selected which I made to be a string to represent co-ordinates. It then turns the co-ordinates into integers which can be obtained by the HumanPlayer class with the getXInt and getYInt methods. The board and pieces are already set up in other classes, I know it all works because i've already created a text version, this is now the GUI version I am working on. It's just a case of enabling the user to select a button without the while(true) loop getting stuck.

The Screen class:

public void actionPerformed(ActionEvent e) {
    JButton piece = (JButton) e.getSource();        
    String xy = piece.getName();
    String x = xy.substring(0,1);
    String y = xy.substring(2,3);

    xInt = Integer.parseInt(x);
    yInt = Integer.parseInt(y);
}

public int getXInt() {
    return xInt;
}

public int getYInt() {
    return yInt;
}

HumanPlayer class which gets the information of the piece selected and checks whether it can be moved.

//Sets up new instance of Screen class which is where the window displaying the buttons is set up (this contains the getXInt and getYInt methods are- so the HumanPlayer class can retrieve the piece which has been selected.
Screen s = new Screen(board);

public boolean makeMove() {

    int fromXCo , fromYCo, toXCo, toYCo;
    Piece p;

    ArrayList<Move> moves = null;

while (true){

    //reads x and y co-ordinate
    System.out.println("Click on a piece to move");
    //takes information from which button is clicked from other class
    fromXCo = s.getXInt();
    fromYCo = s.getYInt();

    p = board.getPiece(fromXCo, fromYCo);
    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");
        continue;           
    }
    break;
}

So the output is a continuous:

  • Click on a piece to move
  • Your selected piece is r
  • There are no available moves for this piece...
ChatNoir
  • 415
  • 8
  • 18
  • What is the stop condition for `while(true)`? – user1803551 May 06 '15 at 17:52
  • while (true) never ends, where's your break statement? – Shar1er80 May 06 '15 at 17:52
  • I didn't think it was necessary to add the whole thing but hold on a sec- will add in – ChatNoir May 06 '15 at 17:54
  • 1
    No, NO! Don't add the "whole thing", just the minimum amount of code that reproduces the problem and that we can run ourselves (it must compile for us). – user1803551 May 06 '15 at 17:55
  • 1
    No i didn't put the whole thing haha I just meant the rest of the while loop to make it make sense – ChatNoir May 06 '15 at 17:58
  • I don't see a connection between the `actionPerformed` and the `while(true)` loop. Why do you blame a button for the infinite loop? – user1803551 May 06 '15 at 18:00
  • Because the while true loop takes in the "co-ordinates" I have created to represent the button, which is set up in the other class. These co-ordinates are then processed in the while true loop against conditions which tell the user whether the button can be used or not. Since the button which is being automatically selected has no moves available for it (it's a chess game) It then asks the user to select a new button... But some how the selected button is stuck – ChatNoir May 06 '15 at 18:02
  • Between what seems like a wrong direction in the design concept and the lack of context, it's hard to understand where the problem lies. Add yet a bit more code which includes the relation between `actionPerformed` and the `while(true)` loop. I assume it has to do with `getPiece` since `actionPerformed` doesn't block for user input. – user1803551 May 06 '15 at 18:17
  • I know getPiece works because it is returning the correct piece and I had no problems when I was making the text version of the game - this is now the GUI version. I've tried to add in more context, I hope this makes it easier to understand! – ChatNoir May 06 '15 at 18:51
  • 1
    Alright, I *think* I see the problem. I assume that `xInt` and `yInt` are fields of the `Screen` class. Pressing the button only updates these values, which are accessed in the loop. However, the loop doesn't prompt the user to press a new button (at least not in the code I see) and update those coordinates values. `s.getXInt()` will therefor always return the same `xInt`. – user1803551 May 06 '15 at 19:02
  • (Tip: to reply to a user in the comments, use @, like @hheather.) – user1803551 May 06 '15 at 19:03
  • Ah! I see what you mean - thanks for pointing me in the right direction! :) – ChatNoir May 06 '15 at 19:08
  • I'm curious now, was that it? Is it getting fixed? (I also wrote a chess program once, so I'm sympathetic to your cause). – user1803551 May 06 '15 at 19:25
  • @user1803551 It hasn't been fixed... I'm now trying to figure out how to update the values if the user were to select a new button... i believe it's something to do with the action performed method. But I'm new to java so it will take a while! :( – ChatNoir May 06 '15 at 19:36
  • I think that you should use an "event driven" approach. The `actionPerformed` should call the operations you wrote inside the `while` loop instead of having the while loop altogether. – user1803551 May 06 '15 at 19:42

0 Answers0