Attempt at fixing memory mess.
[progcomp10.git] / src / link / C / agents / c_wash.c
1 /*
2  *  c_wash.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 #include <string.h>
13
14 /* Implement the wash agent, that is by default nice but will 
15    permanently turn against any agent that betrays it.
16    This is trickier in C than in any other language, for a number of reasons:
17      - there's no classes in C, so we can't just write a generic learning agent
18        and subclass it.
19      - your agent has no idea how many agents it's going to battle, or how many
20        battles it is going to fight, so you've got to do dynamic memory allocation.
21         (anyone who tries to read the source of the supervisor to find out is liable
22          to have their program break unexpectedly)
23  */
24
25 /* To simplify things, we just look at whether we have lost to a particular agent.
26    Unlike in the Python version, we don't keep a generic list
27    This is also done in a inefficient (O(bot-cout)) way.
28    Implementing a faster version is left as an exercise to the DSA student. */
29
30 /* Our guess at the number of agents I'm going to fight in my lifetime
31    (note that this is only a guess, not an upper limit. Do *not* take it as 
32    gospel on the number of agents you're going to see. */
33 #define NUMBEROFAGENTSGUESS 100
34
35 typedef char agentName[MAXAGENTNAMELEN];
36
37 /* data for each instance of my agent */
38 typedef struct {
39         /* The name of the n-th foe who has beaten us */
40         agentName * defeatingFoes;
41
42         /* The length of the array, and how far we are along it */
43         size_t foesLen;
44         unsigned int foesCount;
45 } wash_data;
46
47
48 /* an internal function - have I lost to a given foe? */
49 int haveLostTo( wash_data * me, char * foeName ) {
50     
51         int foe;
52     
53     /* check every foe we know to have defeated us */
54     for (foe=0; foe<me->foesCount; foe++) {
55         if (strncmp( me->defeatingFoes[foe], foeName, MAXAGENTNAMELEN) == 0) {
56             //debugmsg( "%d\thaveLostTo( %s ) -> Yes\n", me, foeName );
57             return 1;
58         }
59     }
60     
61     /* this foe not found */
62     return 0;
63 }
64
65 /* set up myself */
66 void * Initialise( char * myName ) {
67         wash_data * me = malloc( sizeof( wash_data ) );
68         
69         me->defeatingFoes = calloc( NUMBEROFAGENTSGUESS, sizeof( agentName ) );
70         me->foesLen = NUMBEROFAGENTSGUESS;
71     me->foesCount = 0;
72         
73         return (void *) me;
74 }
75
76 /* Attack */
77 ATTACKTYPE Attack( void * this, char * foeName ) {
78         wash_data * me = (wash_data *)this;
79     
80         ATTACKTYPE attack;
81         
82         attack.realAttack =  RandomAttack();
83         
84     /* have I lost to this foe? */
85     if ( haveLostTo(me, foeName) ) {
86         /* Assume they are lying  */
87         switch (attack.realAttack) {
88             case rock:
89                 attack.promisedAttack = scissors;
90                 break;
91             case paper:
92                 attack.promisedAttack = rock;
93                 break;
94             default: /* attack = scissors */
95                 attack.promisedAttack = paper;
96                 break;
97         }
98     } else {
99         /* be nice! */
100         attack.promisedAttack = attack.realAttack;
101     }
102
103         
104         return attack;
105 }
106
107 /* defend */
108 ITEMTYPE Defend( void * this, char * foeName, ITEMTYPE foePromisedAttack ) {
109         wash_data * me = (wash_data *)this;
110         
111         ITEMTYPE defence;
112         
113     if (haveLostTo(me, foeName)) {
114         /* They've screwed us in the past, assume they're lying and go for the
115            kill. */
116         switch (foePromisedAttack) {
117             case rock:
118                 defence = scissors;
119                 break;
120             case paper:
121                 defence = rock;
122                 break;
123             default:
124                 defence = paper;
125                 break;
126         }
127     } else {
128         /* be nice! */
129         defence = foePromisedAttack;
130     }
131
132     return defence;
133 }
134
135 /* This is so much less fun in C */
136 void Results( void * this, char * foeName, int isInstigatedByYou, 
137                          RESULTTYPE winner, ITEMTYPE attItem, ITEMTYPE defItem, 
138                          ITEMTYPE bluffItem, int pointDelta ) {
139         
140     wash_data * me = (wash_data *)this;
141         
142         int foe;
143     
144     /* figure out if we lost, which is the only thing we care about
145        if we didn't, move on. */
146     if ((winner == tie) || 
147         (winner==attacker && isInstigatedByYou) ||
148         (winner==defender && !isInstigatedByYou) ) return;
149     
150     //fprintf( stderr, "%d\tsaving loss from %s\n", me, foeName );
151     
152     /* if we've already lost the foe, don't store again */
153     for (foe=0; foe<me->foesCount; foe++) {
154         if (strncmp( me->defeatingFoes[foe], foeName, MAXAGENTNAMELEN ) == 0) {
155             /* we've found it! */
156             return;
157         }
158     }
159     
160     /* we haven't found the foe. add it, expanding the array if needed */
161     if (me->foesCount==me->foesLen) {
162         /* double the array size. This should error check, but doesn't */
163         me->defeatingFoes = realloc( me->defeatingFoes, 
164                                              me->foesLen*2*sizeof( agentName ) );
165         me->foesLen *= 2;
166     }
167     
168     strncpy( me->defeatingFoes[me->foesCount], foeName, MAXAGENTNAMELEN );
169     me->foesCount++;
170     
171     return;
172 }
173
174 /* Cleanup */
175 void Cleanup( void * this ) {
176         wash_data * me = (wash_data *) this;
177         free(me->defeatingFoes);
178         free(me);
179 }

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