Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By benji2505

Thread: Need help assigning a timer to time players.

  1. #1
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default Need help assigning a timer to time players.

    I am currently working on a game, and now I want to implement a timer that times the player and checks the amount of time it took him to make a move, say for instance player 1 has 60 seconds to respond back with their move, but doesn't make it in time, and thus an error message will be printed and it will then be players 2 turn and same time is applied to them. Now I was wondering how can I set a timer up? Is there a Java package or something that comes with a included timer and how can I implement it to my problem above. If anyone willing to share an example of using a timer or how to set it up that will be great. Thanks!

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Need help assigning a timer to time players.

    The java.time package that came with Java8 should be helpful. I would take a look at the LocalTime class and its collection of methods to do calculations like this.
    Eat_Whatever likes this.

  3. #3
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default Re: Need help assigning a timer to time players.

    Oh ok, thanks a lot.

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

    Default Re: Need help assigning a timer to time players.

    Check out java.util.Timer. You can configure it to generate an event every second to keep track of how long it takes to make a move. You may want to
    incorporate it into a wrapper class to add additional functionality.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Join Date
    Dec 2016
    Posts
    5
    Rep Power
    0

    Default Re: Need help assigning a timer to time players.

    what is spring
    Last edited by venkateswarareddy; 03-30-2017 at 08:12 AM. Reason: it is not current

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Need help assigning a timer to time players.

    Quote Originally Posted by venkateswarareddy View Post
    what is spring
    Spring is a season that comes after winter. It is usually associated with the new growth of vergetation and the birth of animals. I once saw a field full of young lambs, which was awesome because they taste so good. Piglets are cute too, and they make an awesome bacon.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  7. #7
    gozzy is offline Member
    Join Date
    Jan 2017
    Posts
    46
    Rep Power
    0

    Default Re: Need help assigning a timer to time players.

    Personally i would use javax.swing.Timer (assuming you're using Swing) as it already occupies the same thread group as what handles what the user is doing. Depending on what granularity you need you can count 'ticks' that occur between starting the timer and the user having cancelled it by making their move

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Need help assigning a timer to time players.

    +1 on the Swing Timer.

    Since this does not look like a typical school assignment, I'll show you some code. Here's a rough idea of how it should work. A Swing Timer just fires off actionPerformed() every n milliseconds, in this case 1000 millis. It's your job to create an object that implements ActionListener and work with the actionPerformed() method.

    Count each "tick" and when it reaches the max, do something. This code has lots of room for improvement, but it should show you the basic use of a Timer.

    Java Code:
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class GameTimer {
        public static void main(String[] args) {
            TurnTimer turnTimer = new TurnTimer(60);
    
            Timer timer = new Timer(1000, turnTimer);
            timer.setRepeats(true);
            timer.start();
            
            //when the player moves, do this:
            timer.stop()
            turnTimer.reset();
            timer.restart();
    
        }
    
    
        private static class TurnTimer implements ActionListener {
            int seconds;
            private int maxSeconds;
    
            public TurnTimer(int maxSeconds) {
                this.maxSeconds = maxSeconds;
            }
    
            @Override
            public void actionPerformed(ActionEvent e) {
                seconds++;
                if (seconds == maxSeconds) {
                    System.out.println("done");
                }
            }
    
            public void reset() {
                seconds = 0;
            }
        }
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default Re: Need help assigning a timer to time players.

    Thanks guys for the help, I will take it to consideration. By the way, I got the game running on a command line, and I'm trying to use Ecllipse to produce a GUI for the game, is there a way to some how get a box with buttons that connect with my game? Or do I need to rewrite things. Because for my first idea is to get some sort of box or window created in Ecllipse with buttons that are clickiable to some extend using the java packages. Once I have those done, then should I implement that window class and get the fuctions from the other classess to implement into the window class and be able to play or is there a better approach to all of this? Thanks in advance.

  10. #10
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Need help assigning a timer to time players.

    Sure, there are 2 GUI APIs for desktop applications that are "alive": Swing and JavaFX.
    The logic in your code can probably be adapted easily, I would not expect fundamental changes.

  11. #11
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default Re: Need help assigning a timer to time players.

    @benji2505

    Ah, glad to know. Thanks a lot.

  12. #12
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default Re: Need help assigning a timer to time players.

    @benji2505

    Update, thanks to your suggestion, I got GUI going lol. But now what I want to do is to be able to click my button and then the action of the button is going to make a new Panel, but not change the current Frame. So far I tried the action listener on the main class where the actionperfromed was a static class, insted of new panel I just made a new JFrame pop up when the button was clicked. Now I am trying to make a new class which extends main class and also implements ActionListener and so then when the button called "Play Game" is clicked, what I want it to do is just remove everything the first Panel had created and add new panel with necessary components that I will add once I get to that stage. What I have so far in my new class is actionPerformed() {...}; and inside that curly brackets I made a new panel, set the size, set the visibility, and whenever I compile, both classes, nothing is shown, which cofuses me. And I think its cause I might have not created the panel correctly and or removed the previous panel, which I have no idea how to yet. Heres the code from my new class:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class PlayGameGUI extends MainWindow implements ActionListener {
    	
           //Not sure if this is needed...
    	public static void main(String[] args) {
    		new PlayGameGUI();
    	}
    	//Here is what I have done first to test the pop frame, but I don't want the JFrame, just the JPanel. 
    	public void actionPerformed(ActionEvent e) {
    		JFrame frame2 = new JFrame("Clicked");
    		frame2.setVisible(true);
    		frame2.setSize(500,500);
    		JPanel p2 = new JPanel();
    		JLabel l = new JLabel("It worked!");
    		p2.add(l);
    		frame2.add(p2);
    
    	}
    	
    }

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Need help assigning a timer to time players.

    One problem I see is setVisible is called before all the GUI has been built. Components added after the call to setVisible will need some event to tell the GUI to rebuild and show what was added after the first call.

    Look at the CardLayout layout manager to allow showing different panels in one JFrame.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default Re: Need help assigning a timer to time players.

    Oh ok, that makes sense.
    And Card Layout is what I probably need from what I have read so far. Thanks, I will give it a try.

  15. #15
    Eat_Whatever is offline Member
    Join Date
    Nov 2016
    Posts
    24
    Rep Power
    0

    Default Re: Need help assigning a timer to time players.

    I am back once more lol, but this time with a different question, so i got everything set up and working. My game is a mancala(khala) game so it has six holes and two bigger holes at end of the board here a image: Need help assigning a timer to time players.-mancala-game-board-6x2.gif So each small holes contain max of 4 seeds or pebbles in them a total of 48 pebbles, so I have that image set in my GUI, and now I want to dispaly like a number 4 on each one and clicking on one hole will distribute those seeds into other holes in a clockwise cycle. The good thing is I already have a class or GameState class that implemetes such things except it was on a command line, and I want to some how connect them to the holes. Any suggestions?

Similar Threads

  1. how to stop a timer at particular time
    By Mr.cool in forum New To Java
    Replies: 2
    Last Post: 08-03-2012, 10:42 AM
  2. timer clock(buzzer) How get time
    By derata in forum Sun Java Wireless Toolkit
    Replies: 4
    Last Post: 05-06-2012, 07:36 PM
  3. Stopping a Timer from Inside the timer
    By krishnan in forum Java Applets
    Replies: 2
    Last Post: 10-04-2010, 11:15 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:46 PM

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
  •