• Post Reply Bookmark Topic Watch Topic
  • New Topic

While getting Input from User check a second condition  RSS feed

 
Kevin Olome
Ranch Hand
Posts: 44
1
Android Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, is it possible to wait for an input for a user and check in the meantime if something else is true?

Now if the server in the meantime says: there is no input needed and tells it the client.
Could the client now somehow change from
input.readLine() to another Method say: quit() ?

I am stuck there because I know the server sends: quit to the client1 but the client2 cannot react because it still waits for an input for the user.

Maybe something with Thread.sleep ?


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Olome wrote:Hello, is it possible to wait for an input for a user and check in the meantime if something else is true?

Yes, but you'd need more than one Thread.

Now if the server in the meantime says: there is no input needed and tells it the client.
Could the client now somehow change from
input.readLine() to another Method say: quit() ?

Again, yes; but it doesn't sound like a wonderful idea to me.

Could I suggest that you describe to us WHAT is happening - ie, describe the scenario to us. Right now, you're describing HOW you want to do it, and it plainly isn't working.

Winston
 
Kevin Olome
Ranch Hand
Posts: 44
1
Android Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating a rock scissors paper game via network connection.

I have a Client a Server and a ThreadHelperClass.

Server waits for 2 Clients then starts a Thread.
When thread is startet the ThreadHelper class come and is there for the game logic.

Currently it checks if an input for a user is k.
If the user enters k it sends the client a true boolean otherwise a false boolean

The Problem now is: Client1 who entered k knows that the Server sent a boolean and correspond to it.
Client2 however still can enter something because of String input = v.nextLine(); and can not react to the boolean which the server send. Well it can react after the player send a letter but I want that the connection is closed right after someone entered the letter.

Pastebin Client:
http://pastebin.com/cLSxssRu

Pastebin ThreadHelper:
http://pastebin.com/As1zLav4
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Olome wrote:I am creating a rock scissors paper game via network connection.

OK. Now we know what we're talking about.

I have a Client a Server and a ThreadHelperClass. [lots of stuff] ...The Problem now is: Client1 who entered k knows that the Server sent a boolean and correspond to it.
Client2 however still can enter something because of String input = v.nextLine(); and can not react to the boolean which the server send. Well it can react after the player send a letter but I want that the connection is closed right after someone entered the letter.

And this is where I think your problem is - you're much too concerned with the the mechanics of the problem (the 'HOW'), rather than trying to work out scenarios for the game, and then decide WHAT should happen.

For example, you say that your server waits until two clients log on, so:
What happens between the time that Client1 logs on and Client2 logs on?
What happens if Client1 logs off after a "no-result"?

Also: the game requires two inputs in order to make a decision, yet you seem to want to respond to each Client immediately. In such a setup, the only response I can think of to the first Client who inputs is: "waiting for other Player".

Not sure if it helps, and I'm certainly no expert on interactive games; but my advice would be: Back up and work out WHAT you want to happen in every case before you start implementing a solution.

Winston
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, to describe this from the design point of view, if I understand it right...

Your server will wait for input from both players. They will normally enter one of rock, paper, and scissors, and when the server has the input from both of them it will tell them which player won that round, update the scores, etc, etc, and then repeat.

Except that if one player enters the special code "k", that means the game is over? and then the server is still left listening for a response from the other player. Is that right?

 
Kevin Olome
Ranch Hand
Posts: 44
1
Android Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help.
I think i got it wrong with threads.

currently reading:
http://docs.oracle.com/javase/tutorial/essential/concurrency/simple.html

I will complelty reanalyze the situation and have a concrete idea before I start coding.

Thanks for your patience

Edit:
So I am currently rethinking my Thread Story.

Is it possible to throw the user out of the console input if I write this:



If isOver gets true will the InterruptedException be catched?
Or is it only possible in the 1millisecond time which I gave him?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Olome wrote:I am creating a rock scissors paper game via network connection.

Hi Kevin,

As Winston already suggested, you should figure out the whole idea first.

From my view, if I understand you correct, this is what has to be done:
Server: (1)waits for the connection from two clients on port xxxx, (2)receives by one character from each of 2 client instances, (3) According to the game logic checks the conditions, (4)sends the messages back to both clients accordingly to the satisfied game condition, (5)waits for another two connections
Client: (1)creates a connection to the server at port xxxx, (2)waits from the user to input the character, either 'R'(rocket), 'P'(paper) or 'S'(scissors), (3)sends the character to the server, (4)waits for a reply from server, (5)prints received from the server message, (6)closes connection

Are you thinking about something like this?
 
Kevin Olome
Ranch Hand
Posts: 44
1
Android Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes and it already works if every client makes an input like Rock Paper or Scissors.

My only problem is the quit condition.
Because I want that if somebody sends (x)
That the other player who still can enter something can now enter nothing and receive also the quit command from player one.

Because now its like this.
Client 1 sends x.
Server recognize x and send quit to Client1 and Client2
Client 2 still wait for user input.
Client 2 can only react after user have entered something OR before user have entered something. BUT not in between. And I want to react for the client2 that it can react while user can enter something.
 
Kevin Olome
Ranch Hand
Posts: 44
1
Android Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,
today I read twice about Threads and thought to apply this knowledge for the game.

I changed quite a lot but now I have different problems.

How should I say it i have now a thread which waits for a condition from the server to jump to the end of the game.
The "Waiting for the end" condition is checked very often (as much as the thread Scheduler) thinks he must run the thread.

But now my inputs and output don't get along well with what the server is expecting.

Sometimes the client can "receive" the things from the server. Sometimes the client can't receive it because it is maybe in the other thread. (Which expects other stuff from the server)

I heard about synchronised but I don't know how i can apply it here.
I think i need to synchronize this part:


Here is my Client code:


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Olome wrote:...Sometimes the client can "receive" the things from the server. Sometimes the client can't receive it because it is maybe in the other thread. (Which expects other stuff from the server)

I heard about synchronised but I don't know how i can apply it here.

And that's because I think you're still too concerned with the mechanics.

For example, you say:
Because I want that if somebody sends (x)
That the other player who still can enter something can now enter nothing and receive also the quit command from player one.

Now that's just ONE way of approaching this scenario, and I suspect there might be better ones.
The main problem I see is that when one client quits, you want to disconnect (or "log off") the other one automatically. Why not let them decide if they want to stay connected and wait for a third client to log on to play another game?

And the easiest way to do that (as I see it) would be to send a message like:
Your opponent has quit the game. Would you like to wait for a new one?
and then accept whatever they input as a reply to THAT question.

There are still possible problems, even with that solution - for instance: one client quitting, and the 2nd one typing some input before he receives the message - but it seems like a tidier approach to me. And note that I haven't written one line of code to come up with it.

Yet another possibility might be to force clients to make their choices in sequence - ie, don't even prompt Client2 for input until Client1 has entered something.

WhatNotHow (←click) - it's a very good lesson to learn; especially when stuff starts getting involved.

HIH

Winston
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:The main problem I see is that when one client quits, you want to disconnect (or "log off") the other one automatically.


If that's actually what you want, then let me point out that you won't be able to do it unless your communication protocol allows the server to send "log-off" messages to the client. And right now it doesn't; it only allows the client to send messages to the server. So the best you can do is to disconnect the client and allow it to have exceptions when (and if) it decides to send a message to the server. Naturally it should catch those exceptions and explain to the user that the server has gone away.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!