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

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