More work on C sample agents. Learning agents are a pain.
[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    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, lose, win },
28                               { win, tie, lose },
29                               { lose, win, 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 int main( int argc, char * argv[] ) {
48         srand( time( NULL ) );
49         
50         char command[MAXCOMMANDLEN];
51         char foeName[MAXFOENAMELEN];
52         char yourItem[MAXITEMLEN], theirItem[MAXITEMLEN], promisedItem[MAXITEMLEN];
53         char didYouInstigate[MAXBOOLLEN], childSpawned[MAXBOOLLEN];
54         int pointChange;
55         
56         ATTACKTYPE attack;
57         ITEMTYPE defence;
58         
59         scanf( "%s", command );
60         
61         while (strcasecmp("BYE",command) != 0) {
62                 
63                 if (strcasecmp("ATTACK", command) == 0) {
64                         scanf( "%s", foeName );
65                         attack = Attack( foeName );
66                         printf("ATTACKING %s %s\n", ITEMNAMES[attack.realAttack], ITEMNAMES[attack.promisedAttack]);
67                 
68                 } else if (strcasecmp("DEFEND", command) == 0) {
69                         scanf( "%s %s", foeName, promisedItem );
70                         defence = Defend(foeName, stringToItem(promisedItem));
71                         printf("DEFENDING %s\n", ITEMNAMES[defence]);
72                 
73                 } else if (strcasecmp("RESULTS", command) == 0) {
74                         scanf( "%s %s %s %s %s %d %s", foeName, didYouInstigate, yourItem, theirItem, promisedItem, &pointChange, childSpawned );
75                         Results(foeName, (strcasecmp("False",didYouInstigate)==0),
76                                         stringToItem(yourItem), stringToItem(theirItem), stringToItem(promisedItem));
77                         printf("OK\n");
78                 }
79         
80                 // read the next command!
81                 scanf( "%s", command );
82         }
83         
84         Cleanup();
85         
86         return 0;
87 }

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