This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes 2 player internet game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "2 player internet game" Watch "2 player internet game" New topic

2 player internet game

Kieth Nogins

Joined: Mar 10, 2006
Posts: 18

I need to create a 2 player internet game.
My question is : If one player goes to the site and lets say enters his name and then waits for another player on another computer to go to the site and request to play, how do I handle this?
I have basic knowledge in servlets and JSP and pretty good JAVA.
I know I have to use sessions, but how whould the second player know that there is a user waiting for another player to sign in?

I'm not asking for code, but if someone can please explain the idea or the concept to me?

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60817

I've not done this yet, but I have given some thought to someday modifying my BlackBox game (shameless plug: to be 2-player capable.

While the session is necessary to maintain per-user state, it won't help with total game state. That sort of info would need to be stored in the application context. I envisioned each "game" being assigned a unique key, and a Map in the app context being used to store the state of each running game. The game key would be stored in each player's session context in order for the app to know which game instance each player was participating in.

That enough to get you started?

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kieth Nogins

Joined: Mar 10, 2006
Posts: 18
thank you Bear Bibeault,
the game I have to make is similiar to checkers or chess where each player whould see the same board, so your advice didnt really apply, but aside from that your blackbox game was very impressive.
Do you know where I could get sourcecode for games of that sort(maybe even 2 player games).
Thanks anyway
Ben Zhang

Joined: Sep 24, 2008
Posts: 12
Hi Bear, et al.,

Sorry for reviving an old thread. The reason is I have some questions regarding Bear's suggestions.

1) I would see the game state stored in the application scope has to be synchronized?
2) If 1) is true, supposed I use Ajax for pulling the data in 1-5 seconds, how do you see the synchronization overhead?
3) Any other ideas for developing multi-user java-based games?

Thanks, Ben
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60817

1) Yes
2) Only trying it will tell
3) Not at this time -- I still haven't had any time to devote to this
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Nice game Bear. I liked it.

My Blog SCJP 5 SCWCD 5
wood burning stoves
subject: 2 player internet game
Similar Threads
help with tcp causing lag in ping pong game
Awkward question (sorry)
Colourful arrays!!
2 player chess
Creating A Multi Player Game With Java