5 * Created by Daniel Axtens on 22/04/10.
6 * Licensed under an MIT-style license: see the LICENSE file for details.
14 /* Implement the frenchie bot, 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
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)
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. */
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
35 /* The name of the n-th foe who has beaten us */
36 char (*defeatingFoes)[MAXFOENAMELEN] = NULL;
38 /* The length of the array, and how far we are along it */
40 unsigned int foesCount = 0;
42 /* an internal function - have I lost to a given foe? */
43 int haveLostTo( char * foeName ) {
46 /* check every foe we know to have defeated us */
47 for (foe=0; foe<foesCount; foe++) {
48 if (strncmp( defeatingFoes[foe], foeName, MAXFOENAMELEN) == 0) {
49 //debugmsg( "%d\thaveLostTo( %s ) -> Yes\n", me, foeName );
54 /* this foe not found */
59 ATTACKTYPE Attack( char * foeName ) {
62 attack.realAttack = RandomAttack();
64 /* have I lost to this foe? */
65 if ( haveLostTo(foeName) ) {
66 /* Assume they are lying (FIXME: I'm not entirely sure why the python
67 does what it does: I'm just duplicating.) */
68 switch (attack.realAttack) {
70 attack.promisedAttack = scissors;
73 attack.promisedAttack = rock;
75 default: /* attack = scissors */
76 attack.promisedAttack = paper;
81 attack.promisedAttack = attack.realAttack;
89 ITEMTYPE Defend( char * foeName, ITEMTYPE foePromisedAttack ) {
92 if (haveLostTo(foeName)) {
93 /* They've screwed us in the past, assume they're lying and go for the
95 switch (foePromisedAttack) {
108 defence = foePromisedAttack;
114 /* This is so much less fun in C */
115 void Results( char * foeName, int isInstigatedByYou, RESULTTYPE winner,
116 ITEMTYPE attItem, ITEMTYPE defItem, ITEMTYPE bluffItem,
121 /* check to see if we've initialised our arrays */
122 if (defeatingFoes == NULL) {
123 defeatingFoes = calloc( NUMBEROFAGENTSGUESS, sizeof( MAXFOENAMELEN*sizeof(char) ) );
124 foesLen = NUMBEROFAGENTSGUESS;
127 /* figure out if we lost, which is the only thing we care about
128 if we didn't, move on. */
129 if ((winner == tie) ||
130 (winner==attacker && isInstigatedByYou) ||
131 (winner==defender && !isInstigatedByYou) ) return;
133 //fprintf( stderr, "%d\tsaving loss from %s\n", me, foeName );
135 /* if we've already lost the foe, don't store again */
136 for (foe=0; foe<foesCount; foe++) {
137 if (strncmp( defeatingFoes[foe], foeName, MAXFOENAMELEN ) == 0) {
138 /* we've found it! */
143 /* we haven't found the foe. add it, expanding the array if needed */
144 if (foesCount==foesLen) {
145 /* double the array size */
146 defeatingFoes = realloc( defeatingFoes, foesLen*2*sizeof( MAXFOENAMELEN*sizeof(char) ) );
150 strncpy( defeatingFoes[foesCount], foeName, MAXFOENAMELEN );