Replaced slow pexpect with subprocess.popen which is fast. Implemented Results()...
[progcomp10.git] / src / link / C / c_link.c
1 /*
2  *  c_link.c
3  *  c-link-lib
4  *
5  *  Created by Daniel Axtens on 19/04/10.
6  *  Licensed under an MIT-style license: see the LICENSE file for details.
7  *
8  */
9
10 #include "c_link.h"
11 #include <stdlib.h>
12 #include <string.h>
13 #include <stdio.h>
14 #include <time.h>
15
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. 
19  */
20
21 char ITEMNAMES[3][MAXITEMLEN] = {"Rock", "Paper", "Scissors"};
22
23 /* rock-rock     rock-paper     rock-scissors 
24    paper-rock    paper-paper    paper-scissors
25    scissors-rock scissors-paper scissors-scissors */  
26    
27 RESULTTYPE RESULTOF[3][3] = { { tie, defender, attacker },
28                               { attacker, tie, defender },
29                               { defender, attacker, tie } };
30
31
32 ITEMTYPE RandomAttack() {
33         return (ITEMTYPE)rand()%3;
34 }
35
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 );
42         exit(EXIT_FAILURE);
43         return -1;
44 }
45         
46
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 );
53         exit(EXIT_FAILURE);
54         return -1;
55 }
56
57 int main( int argc, char * argv[] ) {
58         srand( time( NULL ) );
59         
60         char command[MAXCOMMANDLEN];
61         char foeName[MAXFOENAMELEN];
62         char attItem[MAXITEMLEN], defItem[MAXITEMLEN], bluffItem[MAXITEMLEN];
63         char didYouInstigate[MAXBOOLLEN];
64     char winner[MAXRESULTLEN];
65         int pointChange;
66         
67         ATTACKTYPE attack;
68         ITEMTYPE defence;
69         
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! */
72     me = rand();
73     
74     
75         scanf( "%s", command );
76         
77         while (strcasecmp("BYE",command) != 0) {
78                 
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]);
83                 
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]);
88                 
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);
94                         printf("OK\n");
95                 }
96         
97         fflush(stdout);
98         fflush(stderr);
99         
100                 // read the next command!
101                 scanf( "%s", command );
102         }
103         
104         Cleanup();
105         
106         return 0;
107 }

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