From e9f0debda3ab1209ece73fd4edaaefe755b6ab2a Mon Sep 17 00:00:00 2001 From: Daniel Axtens Date: Tue, 14 Sep 2010 21:24:54 +0800 Subject: [PATCH] Implemented UUID passing in externAgent and C. Enabled externAgents to print debug messages to stderr --- src/link/C/agents/c_wash.c | 12 ++++++------ src/link/C/c_link.c | 17 +++++------------ src/link/C/c_link.h | 12 +++++------- src/link/externAgent.py | 10 ++++++---- 4 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/link/C/agents/c_wash.c b/src/link/C/agents/c_wash.c index 9ca4a41..a5ab716 100644 --- a/src/link/C/agents/c_wash.c +++ b/src/link/C/agents/c_wash.c @@ -35,7 +35,7 @@ /* 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; @@ -50,7 +50,7 @@ int haveLostTo( wash_data * me, char * foeName ) { /* check every foe we know to have defeated us */ for (foe=0; foefoesCount; 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; } @@ -64,7 +64,7 @@ int haveLostTo( wash_data * me, char * foeName ) { 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; @@ -149,7 +149,7 @@ void Results( void * this, char * foeName, int isInstigatedByYou, /* if we've already lost the foe, don't store again */ for (foe=0; foefoesCount; foe++) { - if (strncmp( me->defeatingFoes[foe], foeName, MAXFOENAMELEN ) == 0) { + if (strncmp( me->defeatingFoes[foe], foeName, MAXAGENTNAMELEN ) == 0) { /* we've found it! */ return; } @@ -159,11 +159,11 @@ void Results( void * this, char * foeName, int isInstigatedByYou, 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; diff --git a/src/link/C/c_link.c b/src/link/C/c_link.c index 6aa5709..524697d 100755 --- a/src/link/C/c_link.c +++ b/src/link/C/c_link.c @@ -57,21 +57,17 @@ int main( int argc, char * argv[] ) { srand( time( NULL ) ); char command[MAXCOMMANDLEN]; - char foeName[MAXFOENAMELEN]; + char foeName[MAXAGENTNAMELEN]; char attItem[MAXITEMLEN], defItem[MAXITEMLEN], bluffItem[MAXITEMLEN]; char didYouInstigate[MAXBOOLLEN]; char winner[MAXRESULTLEN]; - char uuid[UUIDLEN]; + char uuid[MAXAGENTNAMELEN]; int pointChange; void *thisInstance = NULL; ATTACKTYPE attack; ITEMTYPE defence; - /* generate a random id for this bot. Hopefully it's unique - I can't use the UUID, because python doesn't pass it to me! */ - me = rand(); - scanf( "%s", command ); while (strcasecmp("BYE",command) != 0) { @@ -79,23 +75,20 @@ int main( int argc, char * argv[] ) { if (strcasecmp("HI", command) == 0) { scanf( "%s", uuid ); thisInstance = Initialise( uuid ); - } - else if (strcasecmp("ATTACK", command) == 0) { + + } else if (strcasecmp("ATTACK", command) == 0) { scanf( "%s", foeName ); - if( !thisInstance ) break; attack = Attack( thisInstance, foeName ); printf("ATTACKING %s %s\n", ITEMNAMES[attack.realAttack], ITEMNAMES[attack.promisedAttack]); } else if (strcasecmp("DEFEND", command) == 0) { scanf( "%s %s", foeName, bluffItem ); - if( !thisInstance ) break; defence = Defend(thisInstance, foeName, stringToItem(bluffItem)); printf("DEFENDING %s\n", ITEMNAMES[defence]); } else if (strcasecmp("RESULTS", command) == 0) { /* (foeName, isInstigatedByYou, winner, attItem, defItem, bluffItem, pointDelta) */ scanf( "%s %s %s %s %s %s %d", foeName, didYouInstigate, winner, attItem, defItem, bluffItem, &pointChange ); - if( !thisInstance ) break; Results(thisInstance, foeName, (strcasecmp("True",didYouInstigate)==0), stringToResult(winner), stringToItem(attItem), stringToItem(defItem), stringToItem(bluffItem), pointChange); printf("OK\n"); @@ -108,7 +101,7 @@ int main( int argc, char * argv[] ) { scanf( "%s", command ); } - if( !thisInstance ) + if( thisInstance ) Cleanup(thisInstance); return 0; diff --git a/src/link/C/c_link.h b/src/link/C/c_link.h index abf8f1e..0e34644 100755 --- a/src/link/C/c_link.h +++ b/src/link/C/c_link.h @@ -10,11 +10,10 @@ #include #define MAXCOMMANDLEN 15 -#define MAXFOENAMELEN 50 +#define MAXAGENTNAMELEN 42 /* 40 digits, 'L', and NULL */ #define MAXITEMLEN 10 #define MAXRESULTLEN 10 #define MAXBOOLLEN 6 -#define UUIDLEN 42 // 40 digits, 'L' and NULL /********** Type definitions **********/ @@ -37,15 +36,14 @@ typedef struct { /* prints a debug message. Same arguments as printf(). (you can't use printf because it is used to talk between - the agent and supervisor) + the agent and supervisor) + + Hint: store the name passed to you in Initalise and use it to uniquely + identify the agent sending the message. */ #define debugmsg(x...) fprintf(stderr, x) -/* A (hopefully) unique identifier for this particular instance of your agent, - to help with debugging */ -int me; - /* Returns a random item */ diff --git a/src/link/externAgent.py b/src/link/externAgent.py index d3e3af1..0680f33 100644 --- a/src/link/externAgent.py +++ b/src/link/externAgent.py @@ -16,11 +16,13 @@ class externAgent (BaseAgent): 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 @@ -73,7 +75,7 @@ class externAgent (BaseAgent): 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 ): @@ -85,7 +87,7 @@ class externAgent (BaseAgent): 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): -- 2.20.1