-1

I got an assignment for university where I have to implement a hangman game with Threads in Java. My problem is that I can't understand how to handle the threads. In my code there is a GameLeader who prompts the GuessingPlayer to enter a char which he guesses in the startWord. After he did that (run()-method) he takes the message further. The connection between the two players should be arranged with 'Messages' (own implemented class). It's working if I use run() instead of wait(). Can u help me to understand why the while loop is not working after the first entered message?

Thanks!

Class GameLeader:

public class GameLeader {

    public static void main(String[] args) throws IOException {

            GuessingPlayer guessingPlayer = new GuessingPlayer(userInterface);
            String guess;

            System.out.println("Please enter a startWord to begin!");
            String startWord = userInterface.enterWord();

            guessingPlayer.start();

            while (attempts < 11) {

                synchronized (guessingPlayer) {

                    System.out.println("It's your turn, Guessing Player!");

                    guessingPlayer.wait();

                    guess = guessingPlayer.message.toString();
                    if (startWord.contains(guess)) {
                        ...
                        }
                    } else {
                        ...
                    }
                    userInterface.mainMenu(guess);
                }
            }
    }
}

Class GuessingPlayer:

public class GuessingPlayer extends Thread {

    Message guessMessage;
    private UserInterface userInterface;

    GuessingPlayer(UserInterface userInterface) {
        this.userInterface = userInterface;
    }

    @Override
    public void run() {
        synchronized (this) {
            guessMessage = new Message(userInterface.enterWord());
            notify();
        }
    }
}
xXfreshXx
  • 9
  • 3
  • _"...help me to understand why the while loop is not working..."_ We can't help you unless you tell us what you expect and why is the current output **wrong** for you. – progyammer Jan 06 '17 at 17:40
  • https://www.tutorialspoint.com/java/java_multithreading.htm this was very helpful in getting me to understand how threads work in java, and includes a very simple working example. – mstorkson Jan 06 '17 at 17:41
  • 1
    First of all I recommend checking out a basic tutorial on threads, eg. [Concurrency](http://docs.oracle.com/javase/tutorial/essential/concurrency/). Secondly I don't think this is a problem that needs threads, they will just complicate things further. Finally you're demonstrating some bad practices, for example [synchronizing on this instead of on a separate lock object](http://stackoverflow.com/q/442564/5517612) and [extending `Thread` instead of implementing `Runnable`](http://stackoverflow.com/a/541506/5517612). – Todd Sewell Jan 06 '17 at 17:42
  • Thanks for the answers. I understand now, that wait() is similar to sleeping(). I know it's a lot easier without threads and maybe thats why I'm so confused. But I forgot to say that the guessingPlayer has only 30sec to write a char/word and I think for this I need synchronized? – xXfreshXx Jan 09 '17 at 11:35

2 Answers2

0

I think you would be well served to review the course material on threads, and/or talk to your instructor. But, a few comments and suggestions:

  1. I imagine the game leader is supposed to be a thread, and the player(s) are also supposed to be threads (as your current GuessingPlayer class is). These are all instantiated and started when your program starts by calling the start() method on the thread.

  2. You don't have to call run, that gets called internally by the thread once it's started. But you probably want a loop in the run method that waits for the thread to be notified, and then repeats.

  3. By "message passing" they mean something general like having a shared object or Queue that all the threads can read/write and have a reference to. One thread writes something in that object, and calls Thread.notify() to notify the other threads that something interesting has happened in that object. When that happens, the other thread will wake up right where it called the Thread.wait() method. Then it can check that shared object to see what's up.

http://web.mit.edu/6.005/www/fa14/classes/20-queues-locks/message-passing/

http://www.programcreek.com/2009/02/notify-and-wait-example/

Hope this helps.

Jake
  • 8,167
  • 1
  • 19
  • 19
-1

You are using wait() method in a wrong way. You do not need an object reference to call wait() method. It is a method defined in Java's Object class. Therefore, just write wait() instead of using guessingPlayer object reference. Hope this helps. :)