Title: Networking
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Networking Part1

gameNet Package

Steps to using gameNet

There are 5 pieces that you need to define:

1. Create a class which implements Serializable that defines inputs from your GUI to be sent to your Game. Lets call this class MyGameInput.
2. Create a class which implements Serializable that defines outputs from your Game that need to be sent to your GUI to be drawn. Lets call this class MyGameOutput.
3. Create a class which extends an abstract class called GameNet_CoreGame and implements Serializable. Lets call this class MyGame. The one abstract method that you must implement is called process. Process takes one input of type Object. Note that this input will actually be your game's MyGameInput class. The method process returns a class of type Object which will actually be your MyGameOutput class. The actual definition of process is as follows:
Object process(Object);

Your game class will look something like:

public class MyGame extends GameNet_CoreGame
              implements Serializable
{
    ......
    public MyGameOutput process(Object inputs)
    {
        MyGameInput myIn = (MyGameInput)inputs;
       ......
       MyGameOutput myOut = new MyGameOutput(...);
       ......
       return myOut;
    }
    .....

}
	

4. You will need to create a class for your User Interface which implements the GameNet_UserInterface interface. Lets call this class MyUserInterface. This interface has 2 required methods. The first method is called receivedMessage which receives a class of type Object. This class will actually be of type MyGameOutput. This method will be called whenever the process method in GameNet_CoreGame executes and it is sending a MyGameOutput class to all connected User Interface classes. The other method required in the Interface_UserInterface interface is the method startUserInterface. This method is called when the game is ready to roll, and a GamePlayer object is passed into startUserInterface. The GamePlayer object is required for sending user inputs to the Game class. The GameNet_UserInterface interface looks like the following:
public interface GameNet_UserInterface
{
public void receivedMessage(Object ob);
public void startUserInterface (GamePlayer player);
}
When the startUserInterface method is called, you will want to save the player parameter. When your user interface wants to send MyGameInput objects to the MyGame class, the call will look something like:
player.sendMessage(MyGameInput );

If you want to know the name of the Player that is entering the game in startUserInterface it can be found as follows:
String playerName = player.getPlayerName();
5. A small class to tie it altogether. This small class will extend GameCreator and will require a method called createGame which will return an instance of CoreGame. This class will also contain main. The code for this small class will almost always look something like:


  import gameNet.*;

  public class MyMain extends GameCreator{

  public CoreGame createGame()
  {
      return new MyGame();
  }


  public static void main(String[] args)
  {
      MyMain myMain = new MyMain();
      GameNet_UserInterface myUserInterface = new MyUserInterface();

      myMain.enterGame(myUserInterface);
  }// end of main
}// end of class



If the game requires Threads (like Pong), then the MyGame class needs something more than the process routine.

We will now describe how to make your game run threads like Pong where the screen needs to be updated even when the user is not doing anything. Your Game class will look something like:


public class MyGame extends GameNet_CoreGame
              implements Serializable, Runnable
{
    transient GameControl gameControl ;
    // The Java reserved word "transient" tells the system
    //      to not send gameControl across Serialized connections

    ......


    public MyGameOutput process(Object inputs)
    {
        MyGameInput myIn = (MyGameInput)inputs;
       ......
       MyGameOutput myOut = new MyGameOutput(...);
       ......
       return myOut;
    }

    public void startGame(GameControl g)
    {
        this.gameControl = g;
        Thread t = new Thread(this);
        t.start();
    }
    public void run()
    {
       ......
       MyGameOutput myOut = new MyGameOutput( ...);
       gameControl.putMsgs(myOut);
    }
}


- Add one more method to your MyGame class that has the following prototype:
public void startGame(GameControl gameControl);
You will want to save the gameControl object for the duration of your game. Note that this is the one variable that you don't want to have passed as part of your synchronized game object. This is because GameControl contains a lot of things which really can't be properly serialized and also there is not reason to pass this object across your sockets. To make a variable not part of the serialization, you will save it with a statement something like:

transient GameControl gameControl;
- When your startGame method is called, you will want to save the passed in variable gameControl, and you probably want to start your thread at this point.
- Whenever you want to update your User Interface screens from your Game without User Inputs, you can make a call like:
gameControl.putMsgs(MyGameOutput);
- You will most likely declare an instance of gameControl in your MyGame class. One very important thing is that gameControl should not be part of the Serializable objects in your MyGame class. This is because gameControl has some things which DO NOT WANT TO BE SENT ACROSS THE NETWORK. So the trick is declare your instance as follows:
transient GameControl gameControl; // This tells the system to not send gameControl across Serialized connections

Sample Programs and packages found in my Zip file:

gameNet package - contains the guts to making this whole thing work. DO NOT MESS WITH THIS CODE.

GuiChat - World's simplest program that uses gameNet. Any number of users can enter this program, and there are almost no rules. Each user sends messages containing their name, a message and the number of messages sent. This is broadcast to all users of gameNet.

Rps - This is a 2 person game of Rock Paper Scissors. This code is not too bad to figure out. Some complexity in making sure there are only 2 users, and the both users have their picks before a winner is picked.

TicTacToe - This implements networked Tic Tac Toe. There are 2 versions for the GUI. One of them uses JButton for the 9 Tic Tac Toe squares. The other GUI draws everything with a paint method. This code is a little more difficult than you might expect, but it is not a bad place to start.

Pong.jar - Implementation of a 2 person Pong game. I didn't provide the code because this might be one of your projects. We will attempt to demo 1 person Pong networked. I obviously have the code, but I can't provide it to anyone who might be implementing this for their project. Pong uses a Thread.

QuickClick - A simple program that I invented that has a Thread and many bouncing balls that any number of players can play. This is a good example if you want to do something that requires a Thread. The code is not too bad for this kind of thing.

Chess - I have a chess game that is basically brain dead. It lets you drag pieces around and set them in any other square you want even if that piece can't make the particular move. I have 2 projects. The first is "chess_not_networked". In this project there is no gameNet. However, the second project called "chess_networked" takes the non-networked version and adds gameNet to create a networked game.

October 30, 2012 11:57 PM