Merge branch 'c-agents'
[progcomp10.git] / src / link / C / c_link.c
old mode 100644 (file)
new mode 100755 (executable)
index fdb1ffc..524697d
-/*
- *  c_link.c
- *  c-link-lib
- *
- *  Created by Daniel Axtens on 19/04/10.
- *  Licensed under an MIT-style license: see the LICENSE file for details.
- *
- */
-
-#include "c_link.h"
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
-#include <time.h>
-
-/* You don't need to read this file.
-   All you have to do is implement the bot functions defined in <c_link.h>
-   This file sets up the I/O for you, as well as some utility functions and tables. 
- */
-
-char ITEMNAMES[3][MAXITEMLEN] = {"Rock", "Paper", "Scissors"};
-
-/* rock-rock     rock-paper     rock-scissors 
-   paper-rock    paper-paper    paper-scissors
-   scissors-rock scissors-paper scissors-scissors */  
-   
-RESULTTYPE RESULTOF[3][3] = { { tie, defender, attacker },
-                              { attacker, tie, defender },
-                              { defender, attacker, tie } };
-
-
-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;
-}
-       
-
-RESULTTYPE stringToResult( char * str ) {
-    if (strcasecmp( str, "Attacker" ) == 0) return attacker;
-    if (strcasecmp( str, "Defender" ) == 0) return defender;
-    if (strcasecmp( str, "Tie" ) == 0) return tie;
-    /* 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 attItem[MAXITEMLEN], defItem[MAXITEMLEN], bluffItem[MAXITEMLEN];
-       char didYouInstigate[MAXBOOLLEN];
-    char winner[MAXRESULTLEN];
-       int pointChange;
-       
-       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) {
-               
-               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, bluffItem );
-                       defence = Defend(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 );
-                       Results(foeName, (strcasecmp("True",didYouInstigate)==0), stringToResult(winner),
-                                       stringToItem(attItem), stringToItem(defItem), stringToItem(bluffItem), pointChange);
-                       printf("OK\n");
-               }
-       
-        fflush(stdout);
-        fflush(stderr);
-        
-               // read the next command!
-               scanf( "%s", command );
-       }
-       
-       Cleanup();
-       
-       return 0;
-}
\ No newline at end of file
+/*\r
+ *  c_link.c\r
+ *  c-link-lib\r
+ *\r
+ *  Created by Daniel Axtens on 19/04/10.\r
+ *  Licensed under an MIT-style license: see the LICENSE file for details.\r
+ *\r
+ */\r
+\r
+#include "c_link.h"\r
+#include <stdlib.h>\r
+#include <string.h>\r
+#include <stdio.h>\r
+#include <time.h>\r
+\r
+/* You don't need to read this file.\r
+   All you have to do is implement the bot functions defined in <c_link.h>\r
+   This file sets up the I/O for you, as well as some utility functions and tables. \r
+ */\r
+\r
+char ITEMNAMES[3][MAXITEMLEN] = {"Rock", "Paper", "Scissors"};\r
+\r
+/* rock-rock     rock-paper     rock-scissors \r
+   paper-rock    paper-paper    paper-scissors\r
+   scissors-rock scissors-paper scissors-scissors */  \r
+   \r
+RESULTTYPE RESULTOF[3][3] = { { tie, defender, attacker },\r
+                              { attacker, tie, defender },\r
+                              { defender, attacker, tie } };\r
+\r
+ITEMTYPE RandomAttack() {\r
+       return (ITEMTYPE)rand()%3;\r
+}\r
+\r
+ITEMTYPE stringToItem( char * str ) {\r
+       if (strcasecmp( str, "Rock" ) == 0) return rock;\r
+       if (strcasecmp( str, "Paper" ) == 0) return paper;\r
+       if (strcasecmp( str, "Scissors" ) == 0) return scissors;\r
+       /* If we reach this point, we've got real problems. */\r
+       fprintf( stderr, "Attempt to convert invalid string \"%s\" into an ITEMTYPE! Aborting.\n", str );\r
+       exit(EXIT_FAILURE);\r
+       return -1;\r
+}\r
+       \r
+\r
+RESULTTYPE stringToResult( char * str ) {\r
+       if (strcasecmp( str, "Attacker" ) == 0) return attacker;\r
+       if (strcasecmp( str, "Defender" ) == 0) return defender;\r
+       if (strcasecmp( str, "Tie" ) == 0) return tie;\r
+       /* If we reach this point, we've got real problems. */\r
+       fprintf( stderr, "Attempt to convert invalid string \"%s\" into an ITEMTYPE! Aborting.\n", str );\r
+       exit(EXIT_FAILURE);\r
+       return -1;\r
+}\r
+\r
+int main( int argc, char * argv[] ) {\r
+       srand( time( NULL ) );\r
+       \r
+       char command[MAXCOMMANDLEN];\r
+       char foeName[MAXAGENTNAMELEN];\r
+       char attItem[MAXITEMLEN], defItem[MAXITEMLEN], bluffItem[MAXITEMLEN];\r
+       char didYouInstigate[MAXBOOLLEN];\r
+       char winner[MAXRESULTLEN];\r
+       char uuid[MAXAGENTNAMELEN];  \r
+       int pointChange;\r
+       void *thisInstance = NULL;\r
+\r
+       ATTACKTYPE attack;\r
+       ITEMTYPE defence;\r
+       \r
+       scanf( "%s", command );\r
+       \r
+       while (strcasecmp("BYE",command) != 0) {\r
+               \r
+               if (strcasecmp("HI", command) == 0) {\r
+                       scanf( "%s", uuid );\r
+                       thisInstance = Initialise( uuid );\r
+                       \r
+               } else if (strcasecmp("ATTACK", command) == 0) {\r
+                       scanf( "%s", foeName );\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
+                       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
+                       Results(thisInstance, foeName, (strcasecmp("True",didYouInstigate)==0), stringToResult(winner),\r
+                                       stringToItem(attItem), stringToItem(defItem), stringToItem(bluffItem), pointChange);\r
+                       printf("OK\n");\r
+               }\r
+       \r
+               fflush(stdout);\r
+               fflush(stderr);\r
+        \r
+               // read the next command!\r
+               scanf( "%s", command );\r
+       }\r
+       \r
+       if( thisInstance )\r
+               Cleanup(thisInstance);\r
+       \r
+       return 0;\r
+}\r

UCC git Repository :: git.ucc.asn.au