Results 1 to 5 of 5
  1. #1
    ShoeLace1291 is offline Member
    Join Date
    Dec 2015
    Posts
    6
    Rep Power
    0

    Default Whats the best way to add a 3 minute delay to my for loop that controls players' turn

    I am developing a board game. Currently, the game draws a board of hexagon shaped tiles. Once the game starts, I cycle through each player twice. For each player one player at a time, I draw circles at each of the tiles' intersections. I want to delay the turn loop until the user clicks on one of these circles. What would be the best way to delay the iteration of the turn for loop(for int p) until the user clicks on a circle?

    Here's my code:
    Java Code:
    startGame.addEventHandler(MouseEvent.MOUSE_CLICKED,
                    new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent e) {
    
                    gameStatus = "Started";
                    players.add(new Player(0, "Amrit"));
                    players.add(new Player(1, "Tyler"));
                    players.add(new Player(2, "Scott"));
                    players.add(new Player(3, "Ryker"));
    
                    //while (gameStatus == "Started") {
    
                    //Select first two settlements with one road extending from each       
                            
                    for (int s = 0; s < 2; s++) {
    
                        for (int p = 0; p < players.size(); p++) {
    
                            phase = "Pick Settlements";
                            playerTurn = p;
    
                            //Loop through all tiles
                            for (int t = 0; t < tiles.length; t++) {
    
                                //Loop through the vertices of the current tile
                                for (int v = 0; v < tiles[t].vertices.length; v++) {
    
                                    drawCircle(gc, new Point(tiles[t].vertices[v].x, tiles[t].vertices[v].y), (int) (windowHeight * .05), true, true, "rgba(255, 238, 144, .3)", 0);
    
                                }
    
                            }
    
                            gc.setFill(Color.web("0xFFEB8A"));
                            gc.strokeText(players.get(p).name + ", please place your first settlement!", windowHeight * .95, windowWidth * .85);
                            selected = false;
                            canvas.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
                                // @Override
                                @Override
                                public void handle(final MouseEvent event) {
                                    if (!pointClicked().equals(0)) {
                                        players.get(playerTurn).settlementsAvail--;
                                        selected = true;
                                    } else {
                                        selected = false;
                                    }
                                }
    
                            });
                            while(selected){
                                try{
                                    Thread.sleep(1000*60*3);
                                }catch(InterruptedException ex){}
                            }
                            
                       } 
    
                    }
    
                    gameStatus = "Ended";
    
                }
    
            });
    The thread sleep that I use in this code does not do anything. The circles are drawn 8 times very quickly. Anyone have any ideas?
    Last edited by ShoeLace1291; 02-02-2017 at 10:46 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Whats the best way to add a 3 minute delay to my for loop that controls players'

    Having all this in an event handler seems wrong.
    I would expect this (it's turn based by the look of it) to be entirely based around taking turns to deploy things, and the computer reacting to the events that match a placement, or something like that.

    I think that is the root of your problem, and even if it isn't it's still going to bite you at some point.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    ShoeLace1291 is offline Member
    Join Date
    Dec 2015
    Posts
    6
    Rep Power
    0

    Default Re: Whats the best way to add a 3 minute delay to my for loop that controls players'

    Quote Originally Posted by Tolls View Post
    Having all this in an event handler seems wrong.
    I would expect this (it's turn based by the look of it) to be entirely based around taking turns to deploy things, and the computer reacting to the events that match a placement, or something like that.

    I think that is the root of your problem, and even if it isn't it's still going to bite you at some point.
    So if I want to start the game when the Start Game button is clicked, then how would I execute the code currently inside the event handler?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Whats the best way to add a 3 minute delay to my for loop that controls players'

    Probably start a new thread. Even if you call another method, you could tie up processing. The code in event handlers should be executed relatively quickly and then leave. Of course, using threads invites its own set of potential headaches but they are essential for doing a lot of interactive processing. There are numerous concurrency classes that could help with this.

    Edit: And it looks like you are adding a new anonymous handler inside a mouse handler. So you are handling events inside other event handlers. This will probably cause all kinds of lockups in your code.

    Regards,
    Jim
    Last edited by jim829; 02-02-2017 at 03:42 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Whats the best way to add a 3 minute delay to my for loop that controls players'

    You have several events then.
    The StartButton is clicked (which is an ActionEvent in Swing, so add an ActionListener on the JButton), which calls the handler.
    The handler passes that to a controller, which then gets the GUI to draw whatever is need for the first player.
    The first player does whatever, with suitable listeners to detect when they've finished and move onto the next step, and finally the next user.

    That's essentially it. The GUI hands off to a controller that does the game logic.
    The Controller then tells the GUI what bits it should display.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. why my car can not turn left and turn 5 round,Help!!THx
    By seongchog in forum Java Applets
    Replies: 7
    Last Post: 03-01-2012, 10:53 AM
  2. Whats wrong with my while loop?
    By lowziqian in forum New To Java
    Replies: 1
    Last Post: 11-16-2011, 09:25 AM
  3. Please Help Me!!! Last minute test questions...
    By justinm231 in forum New To Java
    Replies: 1
    Last Post: 11-10-2011, 07:42 PM
  4. Replies: 10
    Last Post: 03-29-2011, 12:09 PM

Posting Permissions

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