Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Client/Server card game  RSS feed

 
Besh Hart
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I'm making a multithread client/server card game, but I don't know how to make the clients take turn.

First how do I know which client started first to stop the client in the first place?

Thank you.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the challenges in writing software is that there are literally hundreds of ways you could possibly do something. Without more specifics as to the particular solution that you have in mind, a question like yours is open to hundreds of different interpretations and you could very well end up getting as many different answers. To get specific answers, be more specific about exactly what it is you're trying to do.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to the Ranch!
 
Besh Hart
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:One of the challenges in writing software is that there are literally hundreds of ways you could possibly do something. Without more specifics as to the particular solution that you have in mind, a question like yours is open to hundreds of different interpretations and you could very well end up getting as many different answers. To get specific answers, be more specific about exactly what it is you're trying to do.

Yes you're right, I apologize for not making myself clear. I'll try to explain it with more details.

Lets say I have two clients that are connected to the server, there is a play button that with be made available once the two clients are connected, any of the two clients can press the button and start playing. The

problem here is, I don't want them both to start playing at the same time, they can both enter the panel where the game is, but I want the client who clicked on the button first, to play, if they opened non matching

cards, the client cannot open any cards because their thread is suspended and the turn goes to the second client. It'll go like this till all the cards are open.

I hope I made myself clear, and thank you for the welcome!
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming your clients are going to browser based, I would do searches using terms like HTML 5, WebSockets, and multi-player game programming. You'll probably find plenty of ideas with those terms alone.
 
Besh Hart
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Assuming your clients are going to browser based, I would do searches using terms like HTML 5, WebSockets, and multi-player game programming. You'll probably find plenty of ideas with those terms alone.


Actually they're not, I'm using TCP sockets. I did search for the term multi-player game programming but it wasn't what I was looking for.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Game Programming forum for a better fit.
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is extremely difficult to find out which of two events on different computers happened first. In fact that could be said to be meaningless. So if you give "Play" buttons to both players, you will not be able to prevent the situation where both of them press the button.

However the server can tell which of two events on the server happened first. So perhaps you could give the "Play" button to the client who connected first?
 
Besh Hart
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:It is extremely difficult to find out which of two events on different computers happened first. In fact that could be said to be meaningless. So if you give "Play" buttons to both players, you will not be able to prevent the situation where both of them press the button.

However the server can tell which of two events on the server happened first. So perhaps you could give the "Play" button to the client who connected first?


If I managed to do that, I still don't know how to make the client thread stop mid game in order for the second client to continue.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your responses make me think that you don't actually have any code right now and you're just conducting a thought experiment based on nebulous notions of how you would implement this.

Thought experiments are fine in physics and philosophy and they may even work to some extent in software development but only to a point. I think we've reached that point now where I would say you need to share some actual code so we can continue having a fruitful conversation based on something concrete rather than conjecture.
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what a "client thread" might be. I would just send a message to the second client saying "Your turn to play now."
 
Besh Hart
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Your responses make me think that you don't actually have any code right now and you're just conducting a thought experiment based on nebulous notions of how you would implement this.

Thought experiments are fine in physics and philosophy and they may even work to some extent in software development but only to a point. I think we've reached that point now where I would say you need to share some actual code so we can continue having a fruitful conversation based on something concrete rather than conjecture.


Class Client:



Class Server:


I didn't post my code because:

1\ Its messy because I'm not done with it.

2\ I didn't want you thinking that I'm just here to dump my code on you so you can fix it.

I apologize for any inconvenience caused.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besh Hart wrote:
I didn't post my code because:

1\ Its messy because I'm not done with it.

2\ I didn't want you thinking that I'm just here to dump my code on you so you can fix it.

I apologize for any inconvenience caused.

I should be more careful what I wish for...

1. Fair enough
2. We wouldn't do that anyway


No apology necessary.

Regarding #1, I will say that it would serve you better to make your code clean and well-organized and keep it organized as you go. Otherwise, as you have probably noticed already, it will just get messier and messier. The messier the code gets, the more unwieldy it becomes and the more difficult it will be to work with and change. It will also get progressively more difficult to understand and this will lead you to making an even bigger mess. This creates a vicious cycle that at some point, probably long before you actually get to work properly, will make your code so unwieldy and inscrutable that you'll just give up. If you do get it to work despite it being a mess, it will be so full of bugs that are hidden in the mess you've made that it will be difficult if not next to impossible to fix all the issues.

Perhaps you need to rethink your "requirement" to "stop the client thread". That's actually an implementation detail and thinking like that can drag you down into a deep, dark hole. It seems to me that your real requirement is you need to "prevent more than one player from starting the game." Thinking from there, that might not even be the real requirement. Maybe your real requirement is to figure out which player should be designated as the "active" player whose turn it is to make a move.

As Paul said to earlier, it's difficult to determine in real-time which client player pressed a button first. What you can determine on the server side is which client's request to play came in first. Based on that, I would set a "game started" state on the server side, then inform all clients that the game has started. You would then inform the client whose request came in first and triggered the start of the game on the server side that it's his turn. The other clients get notified that it's not their turn. The clients then adjust their state accordingly.
 
Besh Hart
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Besh Hart wrote:
I didn't post my code because:

1\ Its messy because I'm not done with it.

2\ I didn't want you thinking that I'm just here to dump my code on you so you can fix it.

I apologize for any inconvenience caused.

I should be more careful what I wish for...

1. Fair enough
2. We wouldn't do that anyway


No apology necessary.

Regarding #1, I will say that it would serve you better to make your code clean and well-organized and keep it organized as you go. Otherwise, as you have probably noticed already, it will just get messier and messier. The messier the code gets, the more unwieldy it becomes and the more difficult it will be to work with and change. It will also get progressively more difficult to understand and this will lead you to making an even bigger mess. This creates a vicious cycle that at some point, probably long before you actually get to work properly, will make your code so unwieldy and inscrutable that you'll just give up. If you do get it to work despite it being a mess, it will be so full of bugs that are hidden in the mess you've made that it will be difficult if not next to impossible to fix all the issues.

Perhaps you need to rethink your "requirement" to "stop the client thread". That's actually an implementation detail and thinking like that can drag you down into a deep, dark hole. It seems to me that your real requirement is you need to "prevent more than one player from starting the game." Thinking from there, that might not even be the real requirement. Maybe your real requirement is to figure out which player should be designated as the "active" player whose turn it is to make a move.

As Paul said to earlier, it's difficult to determine in real-time which client player pressed a button first. What you can determine on the server side is which client's request to play came in first. Based on that, I would set a "game started" state on the server side, then inform all clients that the game has started. You would then inform the client whose request came in first and triggered the start of the game on the server side that it's his turn. The other clients get notified that it's not their turn. The clients then adjust their state accordingly.


Bad habits die hard, but I do plan on working on it since its very confusing for team members to follow on my code.

I changed my mind about detecting the pressed button real time, and I do like your idea, but just notifying them is not going to be enough, because even if they were notified and just decided to ignore the notification

and play anyway, it'll be useless. I'll try to work something out, although if it was up to me, I wouldn't mind them playing concurrently.

Thank you.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant notifying the client program. The notification would trigger the execution of code that would prevent the user from doing anything that would require it to be their turn first. Of course, to make your program more user-friendly, you'd also want to put a message in the UI so the user knows what's going on, otherwise they might think the program stopped working.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!