wood burning stoves 2.0*
The moose likes Servlets and the fly likes Two player game. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Two player game." Watch "Two player game." New topic
Author

Two player game.

R Sriram
Greenhorn

Joined: Jun 11, 2008
Posts: 18
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Jun 06, 2007
Posts: 2709
    
    5

Hi sriram R welcome to Javaranch ,
Looks like your display name does not follow the Javaranch Naming policy.
Please change it here.


SCJP, SCWCD.
|Asking Good Questions|
R Sriram
Greenhorn

Joined: Jun 11, 2008
Posts: 18
@ 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

Joined: Jun 06, 2007
Posts: 2709
    
    5

@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

Joined: Jan 28, 2003
Posts: 3934
    
  17

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.


Steve
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
R Sriram
Greenhorn

Joined: Jun 11, 2008
Posts: 18
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two player game.
 
Similar Threads
memento design pattern
Optimizing Code for Chess
Topic for a project in JAVA
java chess
How to store possible movements? and which is the best way?