Skip to content
Snippets Groups Projects
Commit 0656e65b authored by Daniel Axtens's avatar Daniel Axtens
Browse files

More work on C sample agents. Learning agents are a pain.

parent 1740df93
No related merge requests found
...@@ -11,8 +11,6 @@ LINKLIB=libc_link.a ...@@ -11,8 +11,6 @@ LINKLIB=libc_link.a
AGENTSRCS=$(wildcard agents/*.c) AGENTSRCS=$(wildcard agents/*.c)
AGENTS=$(AGENTSRCS:.c=) AGENTS=$(AGENTSRCS:.c=)
EXECUTABLE=rps-c
all: $(LINKSRCS) $(LINKLIB) $(AGENTS) all: $(LINKSRCS) $(LINKLIB) $(AGENTS)
$(LINKLIB): $(LINKOBJS) $(LINKLIB): $(LINKOBJS)
...@@ -27,4 +25,4 @@ $(AGENTS): $(AGENTSRCS) ...@@ -27,4 +25,4 @@ $(AGENTS): $(AGENTSRCS)
.PHONY : clean .PHONY : clean
clean: clean:
rm $(LINKOBJS) $(LINKLIB) $(AGENTS) rm $(LINKOBJS) $(LINKLIB) $(AGENTS)
\ No newline at end of file
...@@ -31,4 +31,10 @@ void Results( char * foeName, int isInstigatedByYou, ITEMTYPE yourItem, ...@@ -31,4 +31,10 @@ void Results( char * foeName, int isInstigatedByYou, ITEMTYPE yourItem,
ITEMTYPE theirItem, ITEMTYPE promisedItem) { ITEMTYPE theirItem, ITEMTYPE promisedItem) {
return; /* Ignore whatever just happened. */ return; /* Ignore whatever just happened. */
}
/* same for Cleanup() */
void Cleanup() {
return;
} }
\ No newline at end of file
...@@ -57,4 +57,10 @@ void Results( char * foeName, int isInstigatedByYou, ITEMTYPE yourItem, ...@@ -57,4 +57,10 @@ void Results( char * foeName, int isInstigatedByYou, ITEMTYPE yourItem,
ITEMTYPE theirItem, ITEMTYPE promisedItem) { ITEMTYPE theirItem, ITEMTYPE promisedItem) {
return; /* Ignore whatever just happened. */ return; /* Ignore whatever just happened. */
}
/* same for Cleanup() */
void Cleanup() {
return;
} }
\ No newline at end of file
...@@ -17,35 +17,33 @@ ATTACKTYPE Attack( char * foe_name ) { ...@@ -17,35 +17,33 @@ ATTACKTYPE Attack( char * foe_name ) {
attack.realAttack = RandomAttack(); attack.realAttack = RandomAttack();
/* Here we choose the thing that will hurt them if they go for a tie */ /* Here we choose the thing that will hurt them if they go for the kill */
switch (attack.realAttack) { switch (attack.realAttack) {
case rock: case rock:
result.promisedAttack = paper; result.promisedAttack = paper;
break; break;
case paper: case paper:
result.promisedAttack = rock; result.promisedAttack = scissors;
break; break;
default: /* attack = scissors */ default: /* attack = scissors */
result.promisedAttack = paper; result.promisedAttack = rock;
break; break;
} }
attack.promisedAttack = result.realAttack; /* Tells the truth for its bluff */
return attack; return attack;
} }
/* Here we trust that they are telling the truth. And we try to kill them. */ /* Here we assume they are lying, trying to kill us. And we try to kill them. */
ITEMTYPE Defend( char * foeName, ITEMTYPE foePromisedAttack ) { ITEMTYPE Defend( char * foeName, ITEMTYPE foePromisedAttack ) {
ITEMTYPE defence; ITEMTYPE defence;
switch (foePromisedAttack) { switch (foePromisedAttack) {
case rock: case rock:
defence = paper; defence = scissors;
break; break;
case paper: case paper:
defence = scissors; defence = rock;
break; break;
default: default:
defence = rock; defence = paper;
break; break;
} }
} }
...@@ -57,3 +55,9 @@ void Results( char * foeName, int isInstigatedByYou, ITEMTYPE yourItem, ...@@ -57,3 +55,9 @@ void Results( char * foeName, int isInstigatedByYou, ITEMTYPE yourItem,
return; /* Ignore whatever just happened. */ return; /* Ignore whatever just happened. */
} }
/* same for Cleanup() */
void Cleanup() {
return;
}
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
...@@ -14,12 +14,20 @@ ...@@ -14,12 +14,20 @@
#include <time.h> #include <time.h>
/* You don't need to read this file. /* You don't need to read this file.
This merely sets up the I/O for you, so all you have to do is define the bot All you have to do is implement the bot functions defined in <c_link.h>
functions defined in <c_link.h> This file sets up the I/O for you, as well as some utility functions and tables.
*/ */
char ITEMNAMES[3][MAXITEMLEN] = {"Rock", "Paper", "Scissors"}; char ITEMNAMES[3][MAXITEMLEN] = {"Rock", "Paper", "Scissors"};
/* rock-rock rock-paper rock-scissors
paper-rock paper-paper paper-scissors
scissors-rock scissors-paper scissors-scissors */
RESULTTYPE RESULTOF[3][3] = { { tie, lose, win },
{ win, tie, lose },
{ lose, win, tie } };
ITEMTYPE RandomAttack() { ITEMTYPE RandomAttack() {
return (ITEMTYPE)rand()%3; return (ITEMTYPE)rand()%3;
...@@ -73,5 +81,7 @@ int main( int argc, char * argv[] ) { ...@@ -73,5 +81,7 @@ int main( int argc, char * argv[] ) {
scanf( "%s", command ); scanf( "%s", command );
} }
Cleanup();
return 0; return 0;
} }
\ No newline at end of file
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
* *
*/ */
#include <stdio.h>
#define MAXCOMMANDLEN 15 #define MAXCOMMANDLEN 15
#define MAXFOENAMELEN 50 #define MAXFOENAMELEN 50
#define MAXITEMLEN 10 #define MAXITEMLEN 10
...@@ -17,6 +19,9 @@ ...@@ -17,6 +19,9 @@
/* The type of item used in an attack or defence */ /* The type of item used in an attack or defence */
typedef enum {rock, paper, scissors} ITEMTYPE; typedef enum {rock, paper, scissors} ITEMTYPE;
/* A result of a battle */
typedef enum {win, lose, tie} RESULTTYPE;
/* An attack, consisting of the real attack and the attack promised */ /* An attack, consisting of the real attack and the attack promised */
typedef struct { typedef struct {
...@@ -28,6 +33,13 @@ typedef struct { ...@@ -28,6 +33,13 @@ typedef struct {
/********** Utility Function definitions **********/ /********** Utility Function definitions **********/
/* These are implemented in c-link.c, and automagically linked in */ /* These are implemented in c-link.c, and automagically linked in */
/* prints a debug message. Same arguments as printf().
(you can't use printf because it is used to talk between
the agent and supervisor)
*/
#define debugmsg(x...) sprintf(stderr, x)
/* Returns a random item */ /* Returns a random item */
ITEMTYPE RandomAttack(); ITEMTYPE RandomAttack();
...@@ -37,6 +49,10 @@ ITEMTYPE RandomAttack(); ...@@ -37,6 +49,10 @@ ITEMTYPE RandomAttack();
extern char ITEMNAMES[3][MAXITEMLEN]; extern char ITEMNAMES[3][MAXITEMLEN];
/* Another useful table - what's the result of the
first item vs the second item? */
extern RESULTTYPE RESULTOF[3][3];
/********** Bot Function definitions **********/ /********** Bot Function definitions **********/
/* You need to provide implementations for these to create a bot */ /* You need to provide implementations for these to create a bot */
...@@ -70,4 +86,12 @@ ATTACKTYPE Attack( char * foeName ); ...@@ -70,4 +86,12 @@ ATTACKTYPE Attack( char * foeName );
*/ */
void Results( char * foeName, int isInstigatedByYou, ITEMTYPE yourItem, void Results( char * foeName, int isInstigatedByYou, ITEMTYPE yourItem,
ITEMTYPE theirItem, ITEMTYPE promisedItem); ITEMTYPE theirItem, ITEMTYPE promisedItem);
\ No newline at end of file
/* Cleanup();
Called when your agent is no longer needed, either due to the round ending
or due to your agent being eliminated.
*/
void Cleanup();
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment