Make a simple robot for IWOR
(Python edition)

Author: Aidin Gharibnavaz

Table Of Contents


1. Introduction

Using simple messages for communicating between robots and the simulation server makes it easy to constructing robots. But if you want to have a real intelligence robot, you need to use artificial intelligence techniques.

This document explain how to make a simple robot for Imperfect world of Robots. This robot moves around the world randomly, and try to detect crystals around her. This not an intelligence robot and can't survive longer than a minute in this pitiless world! But you will gain some idea about how IWOR works, and how to make your own robots.

You can find the complete robot in `sample/robots/Tutorial(Python)' directory.

NOTE: Codes in the `sample/robot/Tutorial(Python)' directory, and in this tutorial, are publish under the terms of GNU General Public License. According to this license, If you use these codes in your robot, you have to publish your robot under GPL, too. Means your program will be free (as in freedom), and everyone can modify and redistribute it again. If you don't like it, please don't use the codes in this tutorial.


2. Connecting to the server

First of all, our robot need to connect to the simulating server. By default, server listen to the 7200 port and wait for the robot's connection. We need to open a socket and use it to connect to the server.

Using sockets in Python is very easy. The first lines of our robot are:

import sys
from socket import *

server = socket(AF_INET, SOCK_STREAM) #Creating a socket.
server.connect(('localhost', 7200))   #Connecting to the server.

This code create a socket and connect it to the simulation server, which running on the same machine (localhost). We can use `server' variable for sending and receiving messages to/from server.


3. Initialization

Our robot need to initialize her self in order to join the game. This can be done by sending `INIT' command to the server.

import sys
from socket import *

server = socket(AF_INET, SOCK_STREAM) #Creating a socket.
server.connect(('localhost', 7200))   #Connecting to the server.

#Initilizing ...
server.send('INIT&1&tutorial&0&')
serverMessage = server.recv(256)
while (serverMessage != '255'):
   server.send('INIT&1&Tutorial&0&')
   serverMessage = server.recv(256)

INIT command have three argument. `1' is the tribe number we want to join to, `tutorial' is the name of our robot, and `0' is the key code to join the game. Zero keys are available only at the start of the game, and number of them is limited. After all of the robots consumed these zero keys, robot must gain new ones from her parents, so its a good idea to add a ability for your robots to gain these keys by command line arguments, so their parents can give keys to them easily. We don't want to go through with this here, take a look at the hand book to gain more information.

`server' provide two important methods for us: send() and recv(). `send' accept one argument, and send it to the simulation server through socket. `recv' read messages from socket. Its only argument is the size of the message. 256 is the maximum length a server message can be.

The reason of using `while' is that sometimes server can't create this robot in the simulated world, because there's no empty space. In this case, we will receive 190 error number which tell us to send INIT later. 255 mean success. Robot send INIT command until she receive a success message from the server.

Now we are in the game! Next step is to teach our robot how to move around.


4. Moving around randomly

in this section, we will add a method named `randomMove' which generate a random number, and produce a moving command according to the generated number.

import random

def randomMove():
   rndint = (int)(random.random() * 4)
   if rndint == 0:
      return 'MOVE&NORTH&'
   elif rndint == 1:
      return 'MOVE&EAST&'
   elif rndint == 2:
      return 'MOVE&SOUTH&'
   else:
      return 'MOVE&WEST&'

This code is explain itself. `randomMove' return a random moving command each time it called. Our robot start moving in a direction, and when she hit a blocking object, she will change her direction. By adding the following code to the robot, it can move around randomly.

movingCommand = 'MOVE&EAST&'

#Moving around ...
while (True):
   server.send(movingCommand)
   serverMessage = server.recv(256)
   if (serverMessage != '255'):
      #I hit a wall or something, changing the direction.
      movingCommand = randomMove()

After each successful move, server will send us 255. If robot dosen't receive this message, it will try to change the direcion by invoking `randomMove()'.


5. Finding crystals around the robot

Now our robot can move around the world, and we're going to add an ability of detecting crystals around herself. Robots can use `SENSE' command to see what objects are around them. The result of SENSE is something like 3,5,#&4,6,C& which means there's a wall at the (3,5) location, and there's a crystal at (4,6). As you can see, objects are separated with '&' and their location is separated with ','. So all we need to do is to extract objects from the result, and if it's a crystal, find its position. The following code will handle this for us:

def seekCrystal(senseResult):
   index = 0
   lastAmpPosition = 0
   result = ['','']

   while index < len(senseResult):
      if senseResult[index]=='&':
         if (senseResult[index-1]=='C'):
            #I found a crystal!
            i = lastAmpPosition
            j = lastAmpPosition
            k = 0
            while (i < index-1):
               if (senseResult[i]==','):
                  result[k] = senseResult[j+1:i]
                  k=k+1
                  j = i
               
               i = i+1
            
         lastAmpPosition = index

      index = index+1

   return result

It's not as complicated as it seems. First, it find '&' charachters in the `senseResult', and check its previous charachter to see whether its a crystal or not:

   while index < len(senseResult):
      if senseResult[index]=='&':
         if (senseResult[index-1]=='C'):
            #I found a crystal!
            .
            .
            .
         lastAmpPosition = index

      index = index+1

If there was a crystal in the result, we want it's location in the world. All we need to do is to find ',' charachter before the 'C':

   while (i < index-1):
      if (senseResult[i]==','):
         result[k] = senseResult[j+1:i]
         k=k+1
         j = i
                  
      i = i+1

Then location will be put on the `result' and return.


6. Last words

This was a very very simple robot, which only move around the world and report if there's a crystal around her. she won't pick up any crystal, don't care about her battery (which cause she became inactive very soon) and in fact do nothing useful! You should make more complicated robots if you want to enjoy the game! Take a look at our sample robots to find out some ideas.


Copyright (c) 2008 Aidin Gharibnavaz
You are free to redistribute and/or modify this document under the terms of the GNU Free Documentation License. A copy of this license can be found on the FDL file along with this document.

Syntax highlighting generated by Web C Plus Plus v0.8.4
Webcpp Copyrighted by Jeffrey Bakker (2001-2004) and publish under the terms of GNU GPL.