Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I developed a web based connect4 game, but I have a problem.

In single player mode it works just fine and for multiplayer mode I managed to update the board saving the state (0-clear, 1- yellow, 2-red for each slot) into a XML file (via AJAX) and then updating the board image within 0.5s also via AJAX so the other player sees the move of his opponent.

The problem is that all I do is update the image, but I need to update the array as well. When a player made his move this javascript code is executed:

JavaScript
arrayPecas[coluna] = arrayPecas[coluna] + 1;

I know JS is client side and it's processed locally, but I need to update that array on the browser of other player.

How should I do the trick?

Thanks in advance!
Posted
Updated 9-Mar-11 2:49am
v3

Use ajax. Your clients are observers and a server side page which is observable. The observable do any serverside process and update the observers. The observers listen continuously to the observable by the javascript's setInterval method.

An example of this patterns is explained here.

Observer Pattern in web applications (JavaScript): A walkthrough with an AJAX example[^]

This article has both observer and observable at client side. But you can move observable to a serverside. Try!
 
Share this answer
 
v2
Comments
Maxdd 7 9-Mar-11 9:42am    
Thank you very much, I'll take a look.
Maxdd 7 9-Mar-11 12:26pm    
But the way, how would you move observable to server-side ?
Albin Abel 9-Mar-11 14:22pm    
When you move observable to server side, each client has to identify itself with a unique key. This unique key has to pass with ajax request. Then the observalble can serve accordingly. Alternatively you can create a webservice which can communicate through ajax
You can also use "Live Data binding using ASP.NET AJAX 4.0"
each time a player update the "arrayPecas", using Ajax , you call the server to update the others players

you can find exemples here :

http://msdn.microsoft.com/en-us/magazine/ee309508.aspx[^]

and

http://dotnetslackers.com/articles/ajax/Live-Data-Binding-using-ASP-NET-AJAX-4-0-Preview-4.aspx[^]
 
Share this answer
 
Comments
Maxdd 7 11-Mar-11 14:55pm    
Thanks for your help.

Unfortunately my little project it's in PHP.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900