Networked Worlds:  Chess 
 
The images above show the white and black players views of the Alice 3D Chess World.
A typical networked application contains hundreds of lines of code to simply enable the involved computers to communicate. This code usually has little relevance to the overall purpose of the application. In Alice, we have incorporated a library called Connect15 with the goal of reducing the amount of networking code an Alice author has to write to make a networked Alice world. The 3D chess world that is shown above is an example of a networked world created with Connect15. The 3D chess world allows two geographically isolated players to play a game of chess in a shared Alice world. Connect15 facilitates the automatic synchronization of the objects within the world such that both users of the Alice worlds see the same board state.  

Below are snippets of code that are necessary to use Connect15 to synchronize networked worlds.  

To include the Connect15 library in your Alice world,  put the following line at the beginning of your script:  
    import connect15  

This line gives you access to the Connect15 library. The next step is to create an array of the objects that will need to be synchronized in the networked world and tell Connect15 to keep these objects at the same position in all networked copies of the world. For the 3D Chess World, the chess pieces need to appear at the same position in the world for both players so that each player can observe the board position (much as they would if they were playing chess across a table rather than across a network).  A shortened list of objects for the chess world appears below:  

    transformables = [blackBishop._composite,  
       blackBishop2._composite,  
       blackCastle._composite,  
       blackCastle2._composite,  
       blackHorse._composite,  
       blackHorse2._composite,  
       blackKing._composite,  
       whiteQueen._composite,  
       whitePawn._composite,  
       whitePawn2._composite,  
       whitePawn3._composite]  

    connect15.SetTransformables(transformables)  

Obviously, this is an incomplete list; it is necessary to include all the pieces that need to be synchronized across Alice worlds.  The line the follows the declaration of the transformables array tells Connect15 that the objects in the transformables array must be synchronized across the Alice worlds. The next step is to declare which player is allowed to move a given piece. In the 3D Chess world, there is an obvious way to partition the pieces; one player has permission to move the white pieces, the other player can move only the black pieces. This can be accomplished in the following way:  

    if Black:  
        connect15.SetTag (0, 1)  
        connect15.SetTag (1, 1)  
        connect15.SetTag (2, 1)  
        connect15.SetTag (3, 1)  
        connect15.SetTag (4, 1)  
        connect15.SetTag (5, 1)  
        connect15.SetTag (6, 1)  

    else:  
        connect15.SetTag(7,1)  
        connect15.SetTag(8,1)  
        connect15.SetTag(9,1)  
        connect15.SetTag(10,1)  

In the above example, the ownership of each piece is set using a variable Black. This variable has a different value depending on the player; in other words, in one player's world Black is set to 1, in the other, Black is set to 0. Based on black, a series of connect15.SetTag commands are executed. The SetTag command basically just tell connect15 to assign control of a particular piece to the computer issuing the command. So, for connect15.SetTag(0,1), we have assigned control of the 0th piece in the transformables array (blackBishop_composite) to the player for whom Black is 1.  

The final code snippet defines the messages that will be passed between the Alice worlds and initiates communication between them.  

    def ExecAndBroadcastString (message):  
         connect15.BroadcastString (message, Yes)  
         c = compile (message, "partyline", "exec")  
         exec c in __main__.__dict__  

    def ReceiveMessages ():  
         while 1:  
         message = connect15.receive ()  
         if message:  
             try:  
                code = compile (message, "<PartyLine>", "exec")  
                exec code in __main__.__dict__  
           except:  
                print message  
      else:  
           break  

    Listen = Do(ReceiveMessages,eachframe)  
    Blab = Do(connect15.BroadcastTaggedTransformables, eachframe)  

    listen.start()  
  
 

  

  
  
  
  
 
  

Questions about this site?  Contact the Webmaster