From 0656e65b86862a314f4f7c909f1719a6823fb53a Mon Sep 17 00:00:00 2001 From: Daniel Axtens Date: Fri, 23 Apr 2010 00:54:45 +0800 Subject: [PATCH] More work on C sample agents. Learning agents are a pain. --- link/C/c-link-lib/Makefile | 4 +-- link/C/c-link-lib/agents/c-angel.c | 6 +++++ link/C/c-link-lib/agents/c-lucifer.c | 6 +++++ link/C/c-link-lib/agents/c-streetfighter.c | 22 +++++++++------- .../c-link-lib.xcodeproj/project.pbxproj | 2 ++ link/C/c-link-lib/c_link.c | 14 ++++++++-- link/C/c-link-lib/c_link.h | 26 ++++++++++++++++++- 7 files changed, 65 insertions(+), 15 deletions(-) diff --git a/link/C/c-link-lib/Makefile b/link/C/c-link-lib/Makefile index c10ef62..10e306b 100644 --- a/link/C/c-link-lib/Makefile +++ b/link/C/c-link-lib/Makefile @@ -11,8 +11,6 @@ LINKLIB=libc_link.a AGENTSRCS=$(wildcard agents/*.c) AGENTS=$(AGENTSRCS:.c=) -EXECUTABLE=rps-c - all: $(LINKSRCS) $(LINKLIB) $(AGENTS) $(LINKLIB): $(LINKOBJS) @@ -27,4 +25,4 @@ $(AGENTS): $(AGENTSRCS) .PHONY : clean clean: - rm $(LINKOBJS) $(LINKLIB) $(AGENTS) \ No newline at end of file + rm $(LINKOBJS) $(LINKLIB) $(AGENTS) diff --git a/link/C/c-link-lib/agents/c-angel.c b/link/C/c-link-lib/agents/c-angel.c index c628ade..c6c8687 100644 --- a/link/C/c-link-lib/agents/c-angel.c +++ b/link/C/c-link-lib/agents/c-angel.c @@ -31,4 +31,10 @@ void Results( char * foeName, int isInstigatedByYou, ITEMTYPE yourItem, ITEMTYPE theirItem, ITEMTYPE promisedItem) { return; /* Ignore whatever just happened. */ +} + +/* same for Cleanup() */ + +void Cleanup() { + return; } \ No newline at end of file diff --git a/link/C/c-link-lib/agents/c-lucifer.c b/link/C/c-link-lib/agents/c-lucifer.c index adfb2ba..1dabc34 100644 --- a/link/C/c-link-lib/agents/c-lucifer.c +++ b/link/C/c-link-lib/agents/c-lucifer.c @@ -57,4 +57,10 @@ void Results( char * foeName, int isInstigatedByYou, ITEMTYPE yourItem, ITEMTYPE theirItem, ITEMTYPE promisedItem) { return; /* Ignore whatever just happened. */ +} + +/* same for Cleanup() */ + +void Cleanup() { + return; } \ No newline at end of file diff --git a/link/C/c-link-lib/agents/c-streetfighter.c b/link/C/c-link-lib/agents/c-streetfighter.c index 9a1d6ce..c63939d 100644 --- a/link/C/c-link-lib/agents/c-streetfighter.c +++ b/link/C/c-link-lib/agents/c-streetfighter.c @@ -17,35 +17,33 @@ ATTACKTYPE Attack( char * foe_name ) { 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) { case rock: result.promisedAttack = paper; break; case paper: - result.promisedAttack = rock; + result.promisedAttack = scissors; break; default: /* attack = scissors */ - result.promisedAttack = paper; + result.promisedAttack = rock; break; } - attack.promisedAttack = result.realAttack; /* Tells the truth for its bluff */ - 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 defence; switch (foePromisedAttack) { case rock: - defence = paper; + defence = scissors; break; case paper: - defence = scissors; + defence = rock; break; default: - defence = rock; + defence = paper; break; } } @@ -57,3 +55,9 @@ void Results( char * foeName, int isInstigatedByYou, ITEMTYPE yourItem, return; /* Ignore whatever just happened. */ } + +/* same for Cleanup() */ + +void Cleanup() { + return; +} diff --git a/link/C/c-link-lib/c-link-lib.xcodeproj/project.pbxproj b/link/C/c-link-lib/c-link-lib.xcodeproj/project.pbxproj index 467b386..80d9803 100644 --- a/link/C/c-link-lib/c-link-lib.xcodeproj/project.pbxproj +++ b/link/C/c-link-lib/c-link-lib.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ 2291A1BD117EE3FD00854CBE /* c-angel.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "c-angel.c"; sourceTree = ""; }; 2291A1BE117EE3FD00854CBE /* c-lucifer.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "c-lucifer.c"; sourceTree = ""; }; 2291A1BF117EE3FD00854CBE /* c-streetfighter.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "c-streetfighter.c"; sourceTree = ""; }; + 2291A1EC117FF85D00854CBE /* c-frechie.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "c-frechie.c"; sourceTree = ""; }; 22F652F5117C679300A3793D /* Makefile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.make; path = Makefile; sourceTree = ""; }; 22F652F6117C6C9500A3793D /* c_link.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = c_link.h; sourceTree = ""; }; /* End PBXFileReference section */ @@ -33,6 +34,7 @@ 2291A1BD117EE3FD00854CBE /* c-angel.c */, 2291A1BE117EE3FD00854CBE /* c-lucifer.c */, 2291A1BF117EE3FD00854CBE /* c-streetfighter.c */, + 2291A1EC117FF85D00854CBE /* c-frechie.c */, ); path = agents; sourceTree = ""; diff --git a/link/C/c-link-lib/c_link.c b/link/C/c-link-lib/c_link.c index e75997f..680450e 100644 --- a/link/C/c-link-lib/c_link.c +++ b/link/C/c-link-lib/c_link.c @@ -14,12 +14,20 @@ #include /* 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 - functions defined in + All you have to do is implement the bot functions defined in + This file sets up the I/O for you, as well as some utility functions and tables. */ 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() { return (ITEMTYPE)rand()%3; @@ -73,5 +81,7 @@ int main( int argc, char * argv[] ) { scanf( "%s", command ); } + Cleanup(); + return 0; } \ No newline at end of file diff --git a/link/C/c-link-lib/c_link.h b/link/C/c-link-lib/c_link.h index 08cdb63..84e9bce 100644 --- a/link/C/c-link-lib/c_link.h +++ b/link/C/c-link-lib/c_link.h @@ -7,6 +7,8 @@ * */ +#include + #define MAXCOMMANDLEN 15 #define MAXFOENAMELEN 50 #define MAXITEMLEN 10 @@ -17,6 +19,9 @@ /* The type of item used in an attack or defence */ 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 */ typedef struct { @@ -28,6 +33,13 @@ typedef struct { /********** Utility Function definitions **********/ /* 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 */ ITEMTYPE RandomAttack(); @@ -37,6 +49,10 @@ ITEMTYPE RandomAttack(); 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 **********/ /* You need to provide implementations for these to create a bot */ @@ -70,4 +86,12 @@ ATTACKTYPE Attack( char * foeName ); */ void Results( char * foeName, int isInstigatedByYou, ITEMTYPE yourItem, - ITEMTYPE theirItem, ITEMTYPE promisedItem); \ No newline at end of file + ITEMTYPE theirItem, ITEMTYPE promisedItem); + +/* 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 -- 2.20.1