1

I have made a game named Two Dice Pig in java. The game has a simple GUI interface showing two dice faces as well as both players scores and names. The game starts with the simple dialog message asking for both players' name and the game starts. Both players interact with only one GUI interface and play as their turn come. The game is completely coded. Now, what I want is to make a server (For now I want to try it on my local server) and both players should get their own GUI. And when a player A is playing, player B should not be able to do anything with the interface. I hope it clears. It's my networking project, but I don't have any idea how to do that. Any help would be greatly appreciated.

Note: I want to apply the same idea as this man applied. Just watch only for 15 seconds. Thanks!,

j08691
  • 204,283
  • 31
  • 260
  • 272
SHEIKH
  • 25
  • 6
  • I hope you have used MVC ... because you have to update the controller almost completely to allow the server communication. Of course, I can't really help if you don't have tried anything. See [ask]. – AxelH Dec 04 '17 at 13:10
  • @AxelH I have read about it a lot. And watched tons of tutorials. So, I'm not completely blank. The code is completely coded. But I want is just two players play on a local server. If you can refer any link that would also be appreciable. Thanks – SHEIKH Dec 04 '17 at 13:16
  • Try to start to implement a simple server to allow client to log and talk together. You will be able to get the idea and update your logic. From what you have, I can't help because SO is not made to provide link, tutorial, guide, ... but help on an existing problem. See how to use a Socket for a start – AxelH Dec 04 '17 at 13:19

2 Answers2

0

You should start googling "Rest Services with Java" or something like this.

You need some central component, that manages the game state of your two android game sessions. For example you have a url like localhost://myService/Dice where you would POST a new dice throw. If somebody else makes a GET to that ressource, he would be able to retrieve the thrown dice you have posted during the game. If you do not want the player to be able to interact, you could forbid a player to POST a new Dice when he is not able to do that and the GUI should reflect that somehow. Maybe disable buttons after you posted a Dice or something like that.

There is much more to it, but this should get you started somehow.

https://docs.oracle.com/javaee/6/tutorial/doc/gilik.html

the important code part from the url above would be the hello world like this:

package com.sun.jersey.samples.helloworld.resources;

import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;

// The Java class will be hosted at the URI path "/helloworld"
@Path("/helloworld")
public class HelloWorldResource {

    // The Java method will process HTTP GET requests
    @GET
    // The Java method will produce content identified by the MIME Media
    // type "text/plain"
    @Produces("text/plain")
    public String getClichedMessage() {
        // Return some cliched textual content
        return "Hello World";
    }
}
modmoto
  • 2,901
  • 7
  • 29
  • 54
  • Thanks for your response. Actually, I want to apply a simple idea to my game which I get from [this](https://youtu.be/aIaFFPatJjY?t=1s) youtube tutorial. Just look for only 15 seconds and you will get some idea of what I am looking for. I would be thankful if you could give me how can I do this. – SHEIKH Dec 04 '17 at 13:25
  • Looks like the guy used the Socket class https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html The Basic idea is to send messages to that socket and handle the messages accordingly. But if you want to play it through the internet, this is no option, I would say. – modmoto Dec 04 '17 at 13:30
  • Yes, you got my point. For now, I just want to play it like this. Can you refer something to get it done? – SHEIKH Dec 04 '17 at 13:33
  • Maybe start here http://cs.lmu.edu/~ray/notes/javanetexamples/#tictactoe This looks like some examples where you could catch on. At least read about the posted Socket Class and how you could use it. It will take some time to get to know the stuff but you will get there. – modmoto Dec 04 '17 at 13:38
  • you saved my life. You reference link was good enough for my better understanding of networking programming. It's just awesome. Thanks! – SHEIKH Dec 04 '17 at 16:32
0

Making a multiplayer game is not a trivial task. Be prepared to rewrite most of you code as it seems you didn't think of writing multiplayer code from the beginning. Here are two different strategies:

Method 1: Dumb client

Most of game logic should be done in server. Clients just display what server told them. They also send commands to server to change game state.

Example:

  • Player X clicks on roll the dice button.
  • Client X sends to server roll command
  • Server checks if it's player X turn or not and returns an error if not.
  • Server rolls the dice. Assume its 3.
  • Server changes the turn to Y.
  • Server sends a message to all clients saying the dice is 3 and it's Y's turn.

Basically server waits for a command from players and do them in sequence. Update the world and broadcast the new world to all clients. Clients just wait to get updates from the server and display them to user.

Method 2: Lockstep simulation

Most of game logic is done in client and server just mediate between them to convey commands. Only commands are transferred and state is simulated in each client. You should be pretty careful to not get synchronization bugs (command may deliver late or other things could happen). You should make sure that applying a command in each client in the same state. For example random seeds should be same so random functions will return same value in both clients.

Example:

  • Player X clicks on roll the dice.
  • Client X will roll the dice for itself and let say it results in 4
  • Client X will send roll command to server.
  • Server broadcast roll command to client Y.
  • Client Y receive roll command.
  • Client Y will roll the dice and should result in 4 (Your code should provide same result in all clients in consistent manner)
Community
  • 1
  • 1
Arman Ordookhani
  • 6,031
  • 28
  • 41
  • Thanks. You cleared my mind of what I can do. For now, can you refer something to better understand it and apply it to my game for any method? – SHEIKH Dec 04 '17 at 13:53
  • @SHEIKH welcome. There are things like https://gafferongames.com/categories/networked-physics/ but I think that's more than you need. Maybe you could start with creating a simple chat java application. – Arman Ordookhani Dec 04 '17 at 14:04