cleanup
[progcomp10.git] / src / link / C / c-link-lib / agents / c-frechie.c
1 /*
2  *  c-frechie.c
3  *  c-link-lib
4  *
5  *  Created by Daniel Axtens on 22/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
13 /* Implement the frenchie bot, that is by default nice but will 
14    permanently turn against any agent that betrays it.
15    This is trickier in C than in any other language, for a number of reasons:
16      - there's no classes in C, so we can't just write a generic learning agent
17        and subclass it.
18      - your agent has no idea how many agents it's going to battle, or how many
19        battles it is going to fight, so you've got to do dynamic memory allocation.
20         (anyone who tries to read the source of the supervisor to find out is liable
21          to have their program break unexpectedly)
22  */
23
24 /* To simplify things, we just look at whether we have lost to a particular agent.
25    Unlike in the Python version, we don't keep a generic list
26    This is also done in a inefficient (O(bot-cout)) way.
27    Implementing a faster version is left as an exercise to the DSA student. */
28
29 /* Our guess at the number of agents I'm going to fight in my lifetime */
30 #define NUMBEROFAGENTSGUESS 100
31
32 /* The name of the n-th foe we've seen, as well as a 0/1 have we lost to them */
33 char foesNames[][MAXFOENAMELEN] = NULL;
34 int haveLostToFoe[] = NULL;
35
36 /* The length of the array, and how far we are along it */
37 size_t foesLen = 0;
38 unsigned int foesCount = 0;
39
40
41 ATTACKTYPE Attack( char * foe_name ) {
42         ATTACKTYPE attack;
43         
44         attack.realAttack =  RandomAttack();
45         
46         /* Here we choose the thing that will hurt them if they go for the kill */
47         switch (attack.realAttack) {
48                 case rock:
49                         result.promisedAttack = paper;
50                         break;
51                 case paper:
52                         result.promisedAttack = scissors;
53                         break;
54                 default: /* attack = scissors */
55                         result.promisedAttack = rock;
56                         break;
57         }
58         return attack;
59 }
60
61 /* Here we assume they are lying, trying to kill us. And we try to kill them. */
62 ITEMTYPE Defend( char * foeName, ITEMTYPE foePromisedAttack ) {
63         ITEMTYPE defence;
64         switch (foePromisedAttack) {
65                 case rock:
66                         defence = scissors;
67                         break;
68                 case paper:
69                         defence = rock;
70                         break;
71                 default:
72                         defence = paper;
73                         break;
74         }
75 }
76
77 /* This is so much less fun in C */
78 void Results( char * foeName, int isInstigatedByYou, ITEMTYPE yourItem, 
79                          ITEMTYPE theirItem, ITEMTYPE promisedItem) {
80         
81     int foe;
82     
83     /* check to see if we've initialised our arrays */
84         if (foesNames == NULL) {
85         foesNames = calloc( NUMBEROFAGENTSGUESS, sizeof( foesNames[0] ) );
86         haveLostToFoe = calloc( NUMBEROFAGENTSGUESS, sizeof( haveLostToFoe[0] ) );
87         foesLen = NUMBEROFAGENTSGUESS;
88     }
89     
90     /* figure out if we lost, which is the only thing we care about
91        if we didn't, move on. */
92     if (RESULTOF[yourItem][theirItem] != lose) return;
93     
94     /* try and find existing foe */
95     
96     return;
97 }
98
99 /* same for Cleanup() */
100
101 void Cleanup() {
102         free(foesNames);
103     free(haveLostToFoe);
104 }

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