GameGrid: Game programming with Java

Research project PHBern  
HomePrintJava-Online

Exercise 8: Pearl-Game, TCP/IP-Kommunikation


Pearl Game is an implementation of the well known game Nim. In the beginning 21 beads are arranged in six rows. A player may remove any number of beads from one row, then end his turn by rightclicking. Then it's the other player's turn. The player removing the last bead loses.

Program a tcp pearl game, with which two players can play over the internet. For two players to play together, they need to sign in with the same SessionID. Communication is managed by two TcpNodes.

Tutorials and examples of TCPGame programming can be found at www.aplu.ch/jgamegrid.

As template you can use the file TcpPearl_0.java

Edit TcpPearl_0.java in the Online-Editor

Download TcpPearl_0.zip

 

Step 1: The game board is completely filled with beads. When the active player clicks on a pearl, it will be removed on both player's screens. Then it's the second player's turn.

Start with TcpPearl_0.java. Create in each location of the 6 x 6 grid an actor Pearl. You need to extend the methods MouseEvent(GGMouse mouse) and MessageReceived(String sender, String text) so that the coordinates of the removed bead are transmitted and the flag isMyMove is changed when the turn is over. This flag also disables or enables the mouse

(A similar example: Tcp Battleships).

 
Player 1   Player 2

 

Step 2: Take any number of pearls

Change the view so that only 21 beads are positioned in the grid. Every player should be able to remove any number of pearls by clicking left. To end the turn, use right click.

For the event "right mouse click" you can use the number "88", since these coordinates can not occur. An example of using the left and right mouse button can be found at
Mouses-Events I/Example 3: Use left and right mouse click

 

 

Step 3: Feedback in the status bar

Next, let the status bar show the amount of remaining pearls.

 

Step 4: Take pearls from the same row only

Check to see if the beads are chosen from the same row. If this is not the case, a warning will appear in the status bar.
Also verify that at least one bead was removed, before the turn is passed.

 

Step 5: End of the game

Check if a player removed the last bead. Show in the title or status bar who won and who lost. Disable the mouse events for both players.