5 * Created by Daniel Axtens on 19/04/10.
6 * Licensed under an MIT-style license: see the LICENSE file for details.
16 /* You don't need to read this file.
17 All you have to do is implement the bot functions defined in <c_link.h>
18 This file sets up the I/O for you, as well as some utility functions and tables.
21 char ITEMNAMES[3][MAXITEMLEN] = {"Rock", "Paper", "Scissors"};
23 /* rock-rock rock-paper rock-scissors
24 paper-rock paper-paper paper-scissors
25 scissors-rock scissors-paper scissors-scissors */
27 RESULTTYPE RESULTOF[3][3] = { { tie, defender, attacker },
28 { attacker, tie, defender },
29 { defender, attacker, tie } };
32 ITEMTYPE RandomAttack() {
33 return (ITEMTYPE)rand()%3;
36 ITEMTYPE stringToItem( char * str ) {
37 if (strcasecmp( str, "Rock" ) == 0) return rock;
38 if (strcasecmp( str, "Paper" ) == 0) return paper;
39 if (strcasecmp( str, "Scissors" ) == 0) return scissors;
40 /* If we reach this point, we've got real problems. */
41 fprintf( stderr, "Attempt to convert invalid string \"%s\" into an ITEMTYPE! Aborting.\n", str );
47 RESULTTYPE stringToResult( char * str ) {
48 if (strcasecmp( str, "Attacker" ) == 0) return attacker;
49 if (strcasecmp( str, "Defender" ) == 0) return defender;
50 if (strcasecmp( str, "Tie" ) == 0) return tie;
51 /* If we reach this point, we've got real problems. */
52 fprintf( stderr, "Attempt to convert invalid string \"%s\" into an ITEMTYPE! Aborting.\n", str );
57 int main( int argc, char * argv[] ) {
58 srand( time( NULL ) );
60 char command[MAXCOMMANDLEN];
61 char foeName[MAXFOENAMELEN];
62 char attItem[MAXITEMLEN], defItem[MAXITEMLEN], bluffItem[MAXITEMLEN];
63 char didYouInstigate[MAXBOOLLEN];
64 char winner[MAXRESULTLEN];
70 /* generate a random id for this bot. Hopefully it's unique
71 I can't use the UUID, because python doesn't pass it to me! */
75 scanf( "%s", command );
77 while (strcasecmp("BYE",command) != 0) {
79 if (strcasecmp("ATTACK", command) == 0) {
80 scanf( "%s", foeName );
81 attack = Attack( foeName );
82 printf("ATTACKING %s %s\n", ITEMNAMES[attack.realAttack], ITEMNAMES[attack.promisedAttack]);
84 } else if (strcasecmp("DEFEND", command) == 0) {
85 scanf( "%s %s", foeName, bluffItem );
86 defence = Defend(foeName, stringToItem(bluffItem));
87 printf("DEFENDING %s\n", ITEMNAMES[defence]);
89 } else if (strcasecmp("RESULTS", command) == 0) {
90 /* (foeName, isInstigatedByYou, winner, attItem, defItem, bluffItem, pointDelta) */
91 scanf( "%s %s %s %s %s %s %d", foeName, didYouInstigate, winner, attItem, defItem, bluffItem, &pointChange );
92 Results(foeName, (strcasecmp("True",didYouInstigate)==0), stringToResult(winner),
93 stringToItem(attItem), stringToItem(defItem), stringToItem(bluffItem), pointChange);
100 // read the next command!
101 scanf( "%s", command );