The source code of all examples is included in the JDroidLib distribution.
Porting TCP Games from Java SE to Android and vice-versa
The Tic-Tac-Toe Game
Our aim is to develop Internet-based games using a game server that handles player teams in multiple game rooms. The concept is presented in details on our TcpJLib site. Please study the principle of the relay server and the specifications of our Internet-based Tic-Tac-Toe game in Tutorial 1 to 3.
Porting programs from JGameGrid to JDroidLib and vice-versa is straight-forward due to the similitude of the library design and class/method naming conventions. (Consult the comparison chart here.)
Fortunately Android uses exactly the same API for TCP-related classes as Java SE. This makes our TCP library for Android (TcpALib) that sits on top of the Java API and handles TCP tunneling through firewalls almost 100 percent compatible with the Java SE version (TcpJLib).
The close affinity between the code for desktop and smartphone games has another big advantage: In a multi-player game the participants may use either a desktop/notebook computer or a smartphone/tablet in the same game. Game servers as well may run on a desktop under Linux/Mac/Windows or on any Android smartphone/tablet.
Because the code for the Android Tic-Tac-Toe is almost the same as for the Java SE version published on the TcpJLib website, we don't show the program source here. (It can be downloaded using the link below.) We just mention that the GGStatusBar class is of great help to display the game state easily.
Under Android you can only run one instance of the player/server on the same device. On the other hand, under Java SE many TCP sessions can run in parallel. This limitation is not due to the Android TCP API, but a consequence that only one JDroidLib app can run on a smartphone at the same time.
Before the first player connects to the relay server, the TicTacToe game server must be started. You must select a string that identifies the game room. The same room name needs to be selected when the two players start the game. Many game servers with different room names may run at the same time. For your convenience we permanently run two Tic-Tac-Toe game servers with room names "mars" and "moon".
Download TicTacToeServer app for installation on a smartphone
Create QR code to download Android app to your smartphone.
Download sources (TicTacToeServer.zip).
Here you can run the Java SE version for Linux/Mac/Windows desktop computers (using WebStart).
|
|
|
Tic-Tac-Toe on a smartphone |
You may even play it in on the Android emulator! |
Download TicTacToe app for installation on a smartphone
Create QR code to download Android app to your smartphone.
Download sources (TicTacToe.zip).
Here you can run the Java SE version for Linux/Mac/Windows desktop computers (using WebStart).
|