Implemented C I/O
[progcomp10.git] / link / C / Link / c-link-lib / 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
15 /* If you just want to write a bot, you don't need to read this file.
16    This merely sets up the I/O for you, so all you have to do is define the bot
17    functions defined in <c-link.h>
18  
19    The only reason you would care about this info is if you want to use pointChange
20    and/or childSpawned parameters from the RESULTS call, which are otherwise discarded.
21  */
22
23 char ITEMNAMES[3][MAXITEMLEN] = {"Rock", "Paper", "Scissors"};
24
25
26 ITEMTYPE RandomAttack() {
27         return (ITEMTYPE)rand()%3;
28 }
29
30 ITEMTYPE stringToItem( char * str ) {
31         if (strcasecmp( str, "Rock" ) == 0) return rock;
32         if (strcasecmp( str, "Paper" ) == 0) return paper;
33         if (strcasecmp( str, "Scissors" ) == 0) return scissors;
34         /* If we reach this point, we've got real problems. */
35         fprintf( stderr, "Attempt to convert invalid string \"%s\" into an ITEMTYPE! Aborting.\n", str );
36         exit(EXIT_FAILURE);
37         return -1;
38 }
39         
40
41 int main( int argc, char * argv[] ) {
42         srand( time( NULL ) );
43         
44         char command[MAXCOMMANDLEN];
45         char foeName[MAXFOENAMELEN];
46         char yourItem[MAXITEMLEN], theirItem[MAXITEMLEN], promisedItem[MAXITEMLEN];
47         char didYouInstigate[MAXBOOLLEN], childSpawned[MAXBOOLLEN];
48         int pointChange;
49         
50         ATTACKTYPE attack;
51         ITEMTYPE defence;
52         
53         scanf( "%s", command );
54         
55         while (strcasecmp("BYE",command) != 0) {
56                 
57                 if (strcasecmp("ATTACK", command) == 0) {
58                         scanf( "%s", foeName );
59                         attack = Attack( foeName );
60                         printf("ATTACKING %s %s\n", ITEMNAMES[attack.realAttack], ITEMNAMES[attack.promisedAttack]);
61                 
62                 } else if (strcasecmp("DEFEND", command) == 0) {
63                         scanf( "%s %s", foeName, promisedItem );
64                         defence = Defend(foeName, stringToItem(promisedItem));
65                         printf("DEFENDING %s\n", ITEMNAMES[defence]);
66                 
67                 } else if (strcasecmp("RESULTS", command) == 0) {
68                         scanf( "%s %s %s %s %s %d %s", foeName, didYouInstigate, yourItem, theirItem, promisedItem, &pointChange, childSpawned );
69                         Results(foeName, (strcasecmp("False",didYouInstigate)==0),
70                                         stringToItem(yourItem), stringToItem(theirItem), stringToItem(promisedItem));
71                         printf("OK\n");
72                 }
73         
74                 // read the next command!
75                 scanf( "%s", command );
76         }
77         
78         return 0;
79 }

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