Added support for HI command (C can now see UUID)
[progcomp10.git] / src / link / C / c_link.c
1 /*\r
2  *  c_link.c\r
3  *  c-link-lib\r
4  *\r
5  *  Created by Daniel Axtens on 19/04/10.\r
6  *  Licensed under an MIT-style license: see the LICENSE file for details.\r
7  *\r
8  */\r
9 \r
10 #include "c_link.h"\r
11 #include <stdlib.h>\r
12 #include <string.h>\r
13 #include <stdio.h>\r
14 #include <time.h>\r
15 \r
16 /* You don't need to read this file.\r
17    All you have to do is implement the bot functions defined in <c_link.h>\r
18    This file sets up the I/O for you, as well as some utility functions and tables. \r
19  */\r
20 \r
21 char ITEMNAMES[3][MAXITEMLEN] = {"Rock", "Paper", "Scissors"};\r
22 \r
23 /* rock-rock     rock-paper     rock-scissors \r
24    paper-rock    paper-paper    paper-scissors\r
25    scissors-rock scissors-paper scissors-scissors */  \r
26    \r
27 RESULTTYPE RESULTOF[3][3] = { { tie, defender, attacker },\r
28                               { attacker, tie, defender },\r
29                               { defender, attacker, tie } };\r
30 \r
31 ITEMTYPE RandomAttack() {\r
32         return (ITEMTYPE)rand()%3;\r
33 }\r
34 \r
35 ITEMTYPE stringToItem( char * str ) {\r
36         if (strcasecmp( str, "Rock" ) == 0) return rock;\r
37         if (strcasecmp( str, "Paper" ) == 0) return paper;\r
38         if (strcasecmp( str, "Scissors" ) == 0) return scissors;\r
39         /* If we reach this point, we've got real problems. */\r
40         fprintf( stderr, "Attempt to convert invalid string \"%s\" into an ITEMTYPE! Aborting.\n", str );\r
41         exit(EXIT_FAILURE);\r
42         return -1;\r
43 }\r
44         \r
45 \r
46 RESULTTYPE stringToResult( char * str ) {\r
47         if (strcasecmp( str, "Attacker" ) == 0) return attacker;\r
48         if (strcasecmp( str, "Defender" ) == 0) return defender;\r
49         if (strcasecmp( str, "Tie" ) == 0) return tie;\r
50         /* If we reach this point, we've got real problems. */\r
51         fprintf( stderr, "Attempt to convert invalid string \"%s\" into an ITEMTYPE! Aborting.\n", str );\r
52         exit(EXIT_FAILURE);\r
53         return -1;\r
54 }\r
55 \r
56 int main( int argc, char * argv[] ) {\r
57         srand( time( NULL ) );\r
58         \r
59         char command[MAXCOMMANDLEN];\r
60         char foeName[MAXFOENAMELEN];\r
61         char attItem[MAXITEMLEN], defItem[MAXITEMLEN], bluffItem[MAXITEMLEN];\r
62         char didYouInstigate[MAXBOOLLEN];\r
63         char winner[MAXRESULTLEN];\r
64         char uuid[UUIDLEN];  \r
65         int pointChange;\r
66         void *thisInstance = NULL;\r
67 \r
68         ATTACKTYPE attack;\r
69         ITEMTYPE defence;\r
70         \r
71         /* generate a random id for this bot. Hopefully it's unique\r
72            I can't use the UUID, because python doesn't pass it to me! */\r
73         me = rand();\r
74         \r
75         scanf( "%s", command );\r
76         \r
77         while (strcasecmp("BYE",command) != 0) {\r
78                 \r
79                 if (strcasecmp("HI", command) == 0) {\r
80                         scanf( "%s", uuid );\r
81                         thisInstance = Initialise( uuid );\r
82                 }\r
83                 else if (strcasecmp("ATTACK", command) == 0) {\r
84                         scanf( "%s", foeName );\r
85                         if( !thisInstance )     break;\r
86                         attack = Attack( thisInstance, foeName );\r
87                         printf("ATTACKING %s %s\n", ITEMNAMES[attack.realAttack], ITEMNAMES[attack.promisedAttack]);\r
88                 \r
89                 } else if (strcasecmp("DEFEND", command) == 0) {\r
90                         scanf( "%s %s", foeName, bluffItem );\r
91                         if( !thisInstance )     break;\r
92                         defence = Defend(thisInstance, foeName, stringToItem(bluffItem));\r
93                         printf("DEFENDING %s\n", ITEMNAMES[defence]);\r
94                 \r
95                 } else if (strcasecmp("RESULTS", command) == 0) {\r
96                         /* (foeName, isInstigatedByYou, winner, attItem, defItem, bluffItem, pointDelta) */\r
97                         scanf( "%s %s %s %s %s %s %d", foeName, didYouInstigate, winner, attItem, defItem, bluffItem, &pointChange );\r
98                         if( !thisInstance )     break;\r
99                         Results(thisInstance, foeName, (strcasecmp("True",didYouInstigate)==0), stringToResult(winner),\r
100                                         stringToItem(attItem), stringToItem(defItem), stringToItem(bluffItem), pointChange);\r
101                         printf("OK\n");\r
102                 }\r
103         \r
104                 fflush(stdout);\r
105                 fflush(stderr);\r
106         \r
107                 // read the next command!\r
108                 scanf( "%s", command );\r
109         }\r
110         \r
111         if( !thisInstance )\r
112                 Cleanup(thisInstance);\r
113         \r
114         return 0;\r
115 }\r

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