Results 1 to 5 of 5
  1. #1
    3.14.TR is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Arrow wait until method is finished

    Hello everybody,
    I am trying to code a simple game and I have a little problem. I hope you can help me - thanks a lot

    There is an IPlayer interface, with void play() method. Of course I have some classes implementing IPlayer such as Human or AI.
    Moreover, there is class Judge, which I would like to do something like this:
    Java Code:
    while (!gameOver) {
      this.selectPlayer().play();
    }
    Of course, I need other players to wait until currently selected IPlayer finishes his turn (click some buttons). That means two possible ways:
    1) Human is selected - Judge must wait until player clicks on two differents JButtons (cards)
    2) AI is selected - Judge must run play() method (which does sth similar - flips come cards and wait for a few seconds - solved) and wait until the turn is over
    After that Judge is supposed run another player.

    The problem is, that I can't run this in while loop, as I described previously (because of threads).

    Can anybody tell me, how to reach what I want to do? I guess I am going to need something like SwingWorker or something like that, don't I? But I don't know how to use it properly

    Please help, I thank you in advance
    Peter

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: wait until method is finished

    I would avoid using while loops like that even on a background thread as there is no need since this is an event driven program. Instead I'd have your game master change the program's state depending on who's turn it is, and have this only allow buttons to be enabled if it makes sense for them to be so.

  3. #3
    3.14.TR is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default Re: wait until method is finished

    so... how to write it? :D I've never worked with own events or even listeners...

    I imagine it sth like this:
    1) Judge have registred listener to GameStatus
    2) when someone finishes turn, he changes GameStatus. That invokes some Event which will be sent to Judge and he will select another player a and run his method play()

    Right? Or do I understand it insufficiently?
    (btw it's not only about disabling the buttons, it's also about running virtual player's move)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: wait until method is finished

    Perhaps you need a Person whoseTurn variable that is set on whoever's turn it is, and then base your program's behavior on the value held by this variable.

  5. #5
    3.14.TR is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default Re: wait until method is finished

    Quote Originally Posted by Fubarable View Post
    Perhaps you need a Person whoseTurn variable that is set on whoever's turn it is, and then base your program's behavior on the value held by this variable.
    I hoped that could determine the selectPlayer() method of Judge... but anyway, how to write the listener? I have really no experience with such a thing :-(

Similar Threads

  1. Just finished Ch. 2 (need help)
    By Kurtis in forum New To Java
    Replies: 1
    Last Post: 10-08-2012, 04:37 AM
  2. wait() and notify() + synchronized method
    By pEkvo in forum Threads and Synchronization
    Replies: 9
    Last Post: 12-29-2011, 10:29 PM
  3. WAIT method question
    By alexalex1234 in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-14-2011, 12:37 PM
  4. Wait method and sleep method
    By chathura87 in forum New To Java
    Replies: 3
    Last Post: 03-01-2011, 06:04 PM
  5. Why would a String class need a wait() method?
    By javanb in forum New To Java
    Replies: 8
    Last Post: 12-06-2010, 02:36 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
  •