From: Daniel Axtens Date: Tue, 20 Apr 2010 16:36:51 +0000 (+0800) Subject: Implemented C I/O X-Git-Tag: v01~32 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=3667187723faeadb1af1e52f412b31397afafc01;p=progcomp10.git Implemented C I/O --- diff --git a/link/C/Link/c-link-lib/c-link.c b/link/C/Link/c-link-lib/c-link.c index b7d6285..0b8af58 100644 --- a/link/C/Link/c-link-lib/c-link.c +++ b/link/C/Link/c-link-lib/c-link.c @@ -9,13 +9,71 @@ #include "c-link.h" #include +#include +#include + +/* If you just want to write a bot, you don't need to read this file. + This merely sets up the I/O for you, so all you have to do is define the bot + functions defined in + + The only reason you would care about this info is if you want to use pointChange + and/or childSpawned parameters from the RESULTS call, which are otherwise discarded. + */ + +char ITEMNAMES[3][MAXITEMLEN] = {"Rock", "Paper", "Scissors"}; + ITEMTYPE RandomAttack() { return (ITEMTYPE)rand()%3; } +ITEMTYPE stringToItem( char * str ) { + if (strcasecmp( str, "Rock" ) == 0) return rock; + if (strcasecmp( str, "Paper" ) == 0) return paper; + if (strcasecmp( str, "Scissors" ) == 0) return scissors; + /* If we reach this point, we've got real problems. */ + fprintf( stderr, "Attempt to convert invalid string \"%s\" into an ITEMTYPE! Aborting.\n", str ); + exit(EXIT_FAILURE); + return -1; +} + int main( int argc, char * argv[] ) { srand( time( NULL ) ); + + char command[MAXCOMMANDLEN]; + char foeName[MAXFOENAMELEN]; + char yourItem[MAXITEMLEN], theirItem[MAXITEMLEN], promisedItem[MAXITEMLEN]; + char didYouInstigate[MAXBOOLLEN], childSpawned[MAXBOOLLEN]; + int pointChange; + + ATTACKTYPE attack; + ITEMTYPE defence; + + scanf( "%s", command ); + + while (strcasecmp("BYE",command) != 0) { + + if (strcasecmp("ATTACK", command) == 0) { + scanf( "%s", foeName ); + attack = Attack( foeName ); + printf("ATTACKING %s %s\n", ITEMNAMES[attack.realAttack], ITEMNAMES[attack.promisedAttack]); + + } else if (strcasecmp("DEFEND", command) == 0) { + scanf( "%s %s", foeName, promisedItem ); + defence = Defend(foeName, stringToItem(promisedItem)); + printf("DEFENDING %s\n", ITEMNAMES[defence]); + + } else if (strcasecmp("RESULTS", command) == 0) { + scanf( "%s %s %s %s %s %d %s", foeName, didYouInstigate, yourItem, theirItem, promisedItem, &pointChange, childSpawned ); + Results(foeName, (strcasecmp("False",didYouInstigate)==0), + stringToItem(yourItem), stringToItem(theirItem), stringToItem(promisedItem)); + printf("OK\n"); + } + + // read the next command! + scanf( "%s", command ); + } + return 0; } \ No newline at end of file diff --git a/link/C/Link/c-link-lib/c-link.h b/link/C/Link/c-link-lib/c-link.h index b53b90e..a369ea8 100644 --- a/link/C/Link/c-link-lib/c-link.h +++ b/link/C/Link/c-link-lib/c-link.h @@ -7,6 +7,11 @@ * */ +#define MAXCOMMANDLEN 15 +#define MAXFOENAMELEN 50 +#define MAXITEMLEN 10 +#define MAXBOOLLEN 6 + /********** Type definitions **********/ /* The type of item used in an attack or defence */ @@ -19,13 +24,21 @@ typedef struct { ITEMTYPE promisedAttack; } ATTACKTYPE; + /********** Utility Function definitions **********/ +/* These are implemented in c-link.c, and automagically linked in */ /* Returns a random item */ ITEMTYPE RandomAttack(); +/* A useful translation table + eg printf( "I use %s.\n", ITEMNAMES[rock] ); */ + +extern char ITEMNAMES[3][MAXITEMLEN]; + /********** Bot Function definitions **********/ +/* You need to provide implementations for these to create a bot */ /* Defend( foeName : string - the name of your foe; foePromisedAttack : ITEMTYPE - the item your foe promised to use diff --git a/technicalities.txt b/technicalities.txt index 2f75cd8..1a0bf03 100644 --- a/technicalities.txt +++ b/technicalities.txt @@ -48,6 +48,8 @@ Syntax for collecting results: RESULTS agent00001 False Rock Paper Scissors -2 False -BYE +{program exits without responding} \ No newline at end of file