/* data for each instance of my agent */
typedef struct {
/* The name of the n-th foe who has beaten us */
- char (*defeatingFoes)[MAXFOENAMELEN];
+ char (*defeatingFoes)[MAXAGENTNAMELEN];
/* The length of the array, and how far we are along it */
size_t foesLen;
/* check every foe we know to have defeated us */
for (foe=0; foe<me->foesCount; foe++) {
- if (strncmp( me->defeatingFoes[foe], foeName, MAXFOENAMELEN) == 0) {
+ if (strncmp( me->defeatingFoes[foe], foeName, MAXAGENTNAMELEN) == 0) {
//debugmsg( "%d\thaveLostTo( %s ) -> Yes\n", me, foeName );
return 1;
}
void * Initialise( char * myName ) {
wash_data * me = malloc( sizeof( wash_data ) );
- me->defeatingFoes = calloc( NUMBEROFAGENTSGUESS, sizeof( MAXFOENAMELEN*sizeof(char) ) );
+ me->defeatingFoes = calloc( NUMBEROFAGENTSGUESS, sizeof( MAXAGENTNAMELEN*sizeof(char) ) );
me->foesLen = NUMBEROFAGENTSGUESS;
me->foesCount = 0;
/* if we've already lost the foe, don't store again */
for (foe=0; foe<me->foesCount; foe++) {
- if (strncmp( me->defeatingFoes[foe], foeName, MAXFOENAMELEN ) == 0) {
+ if (strncmp( me->defeatingFoes[foe], foeName, MAXAGENTNAMELEN ) == 0) {
/* we've found it! */
return;
}
if (me->foesCount==me->foesLen) {
/* double the array size. This should error check, but doesn't */
me->defeatingFoes = realloc( me->defeatingFoes,
- me->foesLen*2*sizeof( MAXFOENAMELEN*sizeof(char) ) );
+ me->foesLen*2*sizeof( MAXAGENTNAMELEN*sizeof(char) ) );
me->foesLen *= 2;
}
- strncpy( me->defeatingFoes[me->foesCount], foeName, MAXFOENAMELEN );
+ strncpy( me->defeatingFoes[me->foesCount], foeName, MAXAGENTNAMELEN );
me->foesCount++;
return;
srand( time( NULL ) );\r
\r
char command[MAXCOMMANDLEN];\r
- char foeName[MAXFOENAMELEN];\r
+ char foeName[MAXAGENTNAMELEN];\r
char attItem[MAXITEMLEN], defItem[MAXITEMLEN], bluffItem[MAXITEMLEN];\r
char didYouInstigate[MAXBOOLLEN];\r
char winner[MAXRESULTLEN];\r
- char uuid[UUIDLEN]; \r
+ char uuid[MAXAGENTNAMELEN]; \r
int pointChange;\r
void *thisInstance = NULL;\r
\r
ATTACKTYPE attack;\r
ITEMTYPE defence;\r
\r
- /* generate a random id for this bot. Hopefully it's unique\r
- I can't use the UUID, because python doesn't pass it to me! */\r
- me = rand();\r
- \r
scanf( "%s", command );\r
\r
while (strcasecmp("BYE",command) != 0) {\r
if (strcasecmp("HI", command) == 0) {\r
scanf( "%s", uuid );\r
thisInstance = Initialise( uuid );\r
- }\r
- else if (strcasecmp("ATTACK", command) == 0) {\r
+ \r
+ } else if (strcasecmp("ATTACK", command) == 0) {\r
scanf( "%s", foeName );\r
- if( !thisInstance ) break;\r
attack = Attack( thisInstance, foeName );\r
printf("ATTACKING %s %s\n", ITEMNAMES[attack.realAttack], ITEMNAMES[attack.promisedAttack]);\r
\r
} else if (strcasecmp("DEFEND", command) == 0) {\r
scanf( "%s %s", foeName, bluffItem );\r
- if( !thisInstance ) break;\r
defence = Defend(thisInstance, foeName, stringToItem(bluffItem));\r
printf("DEFENDING %s\n", ITEMNAMES[defence]);\r
\r
} else if (strcasecmp("RESULTS", command) == 0) {\r
/* (foeName, isInstigatedByYou, winner, attItem, defItem, bluffItem, pointDelta) */\r
scanf( "%s %s %s %s %s %s %d", foeName, didYouInstigate, winner, attItem, defItem, bluffItem, &pointChange );\r
- if( !thisInstance ) break;\r
Results(thisInstance, foeName, (strcasecmp("True",didYouInstigate)==0), stringToResult(winner),\r
stringToItem(attItem), stringToItem(defItem), stringToItem(bluffItem), pointChange);\r
printf("OK\n");\r
scanf( "%s", command );\r
}\r
\r
- if( !thisInstance )\r
+ if( thisInstance )\r
Cleanup(thisInstance);\r
\r
return 0;\r
#include <stdio.h>\r
\r
#define MAXCOMMANDLEN 15\r
-#define MAXFOENAMELEN 50\r
+#define MAXAGENTNAMELEN 42 /* 40 digits, 'L', and NULL */\r
#define MAXITEMLEN 10\r
#define MAXRESULTLEN 10\r
#define MAXBOOLLEN 6\r
-#define UUIDLEN 42 // 40 digits, 'L' and NULL\r
\r
/********** Type definitions **********/\r
\r
\r
/* prints a debug message. Same arguments as printf().\r
(you can't use printf because it is used to talk between\r
- the agent and supervisor) \r
+ the agent and supervisor)\r
+ \r
+ Hint: store the name passed to you in Initalise and use it to uniquely\r
+ identify the agent sending the message.\r
*/\r
\r
#define debugmsg(x...) fprintf(stderr, x)\r
\r
-/* A (hopefully) unique identifier for this particular instance of your agent,\r
- to help with debugging */\r
-int me;\r
-\r
\r
/* Returns a random item */\r
\r
BaseAgent.__init__(self)
try:
self.process = subprocess.Popen(externName, stdin=subprocess.PIPE,
- stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+ stdout=subprocess.PIPE,
universal_newlines=True)
except Exception, e:
print ("Error spawning \"%s\": " % externName), e
-
+
+ self.process.stdin.write ( ' '.join( ["HI", repr(self.GetID()), "\r\n"] ) )
+
def stringToItem( self, str ):
if str == "Rock":
return Rock
return attack, bluff
except:
#agent is insane
- print "Agent is insane:", self
+ print "Agent is insane:", self, self.GetID()
pass
def Defend (self, foe, bluff ):
return defence
except:
#agent is insane
- print "Agent is insane:", self
+ print "Agent is insane:", self, self.GetID()
pass
def Results (self, foe, isInstigatedByYou, winner, attItem, defItem, bluffItem, pointDelta):