Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two player game.

 
R Sriram
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,

I have been working on a small two player web based game. I am not sure how to sync up player's moves. for ex., if player1 & player2 are playing the game, how do I communicate the moves of each player between them. I am of the opinion that I have to use some event to update the server after a move occurs at one end, but I am not sure how the other player(browser client) will get the data as and when an update occurs, (again an event there?) ?

As a more practical ex., if two play a chess game, how do I reflect the move made by one on the opponent's screen, so that the opponent will make move according to the new chess board configuration..

Hope I am clear.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not straight forward in Servlets, this type of behaviour is typically done using AJAX or alternate technologies like Flash.

In a pure Servlet environment, you need the person that it waiting to keep refreshing in order to pick up the change once ready.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sriram R welcome to Javaranch ,
Looks like your display name does not follow the Javaranch Naming policy.
Please change it here.
 
R Sriram
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ David, thanks first up. Now, even If I am doing it in AJAX, I need to send back some info to the server so that the client @ the other end can pick it up. So I just want to know how do I get it going.

@Amit, Name Changed!
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Amit, Name Changed!

Thanks

I dont know Ajax but as Dave said above Flash is another option. And I also think it is better as it will also help you with GUI for the game.
Also I think that you will need periodic page refresh or a similar mechanism to get updated positions.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by R Sriram:
@ David, thanks first up. Now, even If I am doing it in AJAX, I need to send back some info to the server so that the client @ the other end can pick it up. So I just want to know how do I get it going.


The AJAX request from a player sends info to the server letting it know about the player's move.

Now it is the server's turn to inform the second player of information. This can be done a few ways:
1) There is a regular page refresh either with a meta refresh tag, or a javascript timeout.
2) Regularly scheduled AJAX calls poll the the server for information
3) You use a second framework, called Pushlets, to allow the Server to Push content to the Player. See The Pushlets Home Page

With a combination of AJAX and Pushlets you can get a pretty nice event driven web game.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by R Sriram:
@ David, thanks first up. Now, even If I am doing it in AJAX, I need to send back some info to the server so that the client @ the other end can pick it up. So I just want to know how do I get it going.

@Amit, Name Changed!


First, please try to use real words when posting to the ranch. All those 'at' symbols (@) don't help people who are using language translation software to read your posts.

A two player game is an advanced topic for servlets.
If you're not already really strong with both servlets and web development in general, you might want to step back from this goal and work through some simpler projects to strengthen your knowledge of all the necessary technologies before trying something that pushes the boundaries like this one.
 
R Sriram
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve, Thanks for your reply. I was just trying to figure if there was something like this(Pushlets)!
Ben, I thought the symbol(at-the-rate) meant the same in all languages. Anyways will try to cut down on those.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic