From: Daniel Axtens Date: Tue, 4 May 2010 09:07:45 +0000 (+0800) Subject: unbroke large chunks X-Git-Tag: v01~24 X-Git-Url: https://git.ucc.asn.au/?p=progcomp10.git;a=commitdiff_plain;h=e4184557ebc33201e217e167d577128f710e4890 unbroke large chunks --- diff --git a/src/link/C/Makefile b/src/link/C/Makefile index 10e306b..ccefb00 100644 --- a/src/link/C/Makefile +++ b/src/link/C/Makefile @@ -16,9 +16,9 @@ all: $(LINKSRCS) $(LINKLIB) $(AGENTS) $(LINKLIB): $(LINKOBJS) $(AR) rcs $(LINKLIB) $(LINKOBJS) -$(AGENTS): $(AGENTSRCS) - @echo Building $< - $(CC) $(CFLAGS) $(LDFLAGS) $< -o $@ +$(AGENTS): $(AGENTSRCS) $(LINKLIB) + @echo Building $@ + $(CC) $(CFLAGS) $(LDFLAGS) $@.c -o $@ .c.o: c_link.h $(CC) -c $(CFLAGS) $< -o $@ diff --git a/src/link/C/__init__.py b/src/link/C/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/link/C/agents-broken/c_frenchie.c b/src/link/C/agents-broken/c_frenchie.c new file mode 100644 index 0000000..78866a6 --- /dev/null +++ b/src/link/C/agents-broken/c_frenchie.c @@ -0,0 +1,105 @@ +/* + * c_frechie.c + * c-link-lib + * + * Created by Daniel Axtens on 22/04/10. + * Licensed under an MIT-style license: see the LICENSE file for details. + * + */ + +#include +#include + +/* Implement the frenchie bot, that is by default nice but will + permanently turn against any agent that betrays it. + This is trickier in C than in any other language, for a number of reasons: + - there's no classes in C, so we can't just write a generic learning agent + and subclass it. + - your agent has no idea how many agents it's going to battle, or how many + battles it is going to fight, so you've got to do dynamic memory allocation. + (anyone who tries to read the source of the supervisor to find out is liable + to have their program break unexpectedly) + */ + +/* To simplify things, we just look at whether we have lost to a particular agent. + Unlike in the Python version, we don't keep a generic list + This is also done in a inefficient (O(bot-cout)) way. + Implementing a faster version is left as an exercise to the DSA student. */ + +/* Our guess at the number of agents I'm going to fight in my lifetime */ +#define NUMBEROFAGENTSGUESS 100 + +/* The name of the n-th foe we've seen, as well as a 0/1 have we lost to them */ +char foesNames[][MAXFOENAMELEN]; +int haveLostToFoe[]; + +/* The length of the array, and how far we are along it */ +size_t foesLen = 0; +unsigned int foesCount = 0; + + +ATTACKTYPE Attack( char * foe_name ) { + ATTACKTYPE attack; + + attack.realAttack = RandomAttack(); + + /* Here we choose the thing that will hurt them if they go for the kill */ + switch (attack.realAttack) { + case rock: + attack.promisedAttack = paper; + break; + case paper: + attack.promisedAttack = scissors; + break; + default: /* attack = scissors */ + attack.promisedAttack = rock; + break; + } + return attack; +} + +/* 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 = scissors; + break; + case paper: + defence = rock; + break; + default: + defence = paper; + break; + } + return defence; +} + +/* This is so much less fun in C */ +void Results( char * foeName, int isInstigatedByYou, ITEMTYPE yourItem, + ITEMTYPE theirItem, ITEMTYPE promisedItem) { + + int foe; + + /* check to see if we've initialised our arrays */ + if (foesNames == NULL) { + foesNames = calloc( NUMBEROFAGENTSGUESS, sizeof( foesNames[0] ) ); + haveLostToFoe = calloc( NUMBEROFAGENTSGUESS, sizeof( haveLostToFoe[0] ) ); + foesLen = NUMBEROFAGENTSGUESS; + } + + /* figure out if we lost, which is the only thing we care about + if we didn't, move on. */ + if (RESULTOF[yourItem][theirItem] != lose) return; + + /* try and find existing foe */ + + return; +} + +/* same for Cleanup() */ + +void Cleanup() { + free(foesNames); + free(haveLostToFoe); +} \ No newline at end of file diff --git a/src/link/C/agents/c-angel.c b/src/link/C/agents/c-angel.c deleted file mode 100644 index c6c8687..0000000 --- a/src/link/C/agents/c-angel.c +++ /dev/null @@ -1,40 +0,0 @@ -/* - * c-angel.c - * c-link-lib - * - * Created by Daniel Axtens on 20/04/10. - * Licensed under an MIT-style license: see the LICENSE file for details. - * - */ - -#include - -/* Implement the angel bot, which always tells the truth - and expects others to do the same */ - -ATTACKTYPE Attack( char * foe_name ) { - ATTACKTYPE attack; - - attack.realAttack = RandomAttack(); /* Chooses randomly from Rock, Paper, Scissors */ - attack.promisedAttack = attack.realAttack; /* Tells the truth for its bluff */ - - return attack; -} - -ITEMTYPE Defend( char * foeName, ITEMTYPE foePromisedAttack ) { - return foePromisedAttack; /* Trusts them to be going for a tie */ -} - -/* You need to define a results function, even if it isn't used - (otherwise the linker will complain) */ -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/src/link/C/agents/c-frechie.c b/src/link/C/agents/c-frechie.c deleted file mode 100644 index ed2ba09..0000000 --- a/src/link/C/agents/c-frechie.c +++ /dev/null @@ -1,104 +0,0 @@ -/* - * c-frechie.c - * c-link-lib - * - * Created by Daniel Axtens on 22/04/10. - * Licensed under an MIT-style license: see the LICENSE file for details. - * - */ - -#include -#include - -/* Implement the frenchie bot, that is by default nice but will - permanently turn against any agent that betrays it. - This is trickier in C than in any other language, for a number of reasons: - - there's no classes in C, so we can't just write a generic learning agent - and subclass it. - - your agent has no idea how many agents it's going to battle, or how many - battles it is going to fight, so you've got to do dynamic memory allocation. - (anyone who tries to read the source of the supervisor to find out is liable - to have their program break unexpectedly) - */ - -/* To simplify things, we just look at whether we have lost to a particular agent. - Unlike in the Python version, we don't keep a generic list - This is also done in a inefficient (O(bot-cout)) way. - Implementing a faster version is left as an exercise to the DSA student. */ - -/* Our guess at the number of agents I'm going to fight in my lifetime */ -#define NUMBEROFAGENTSGUESS 100 - -/* The name of the n-th foe we've seen, as well as a 0/1 have we lost to them */ -char foesNames[][MAXFOENAMELEN] = NULL; -int haveLostToFoe[] = NULL; - -/* The length of the array, and how far we are along it */ -size_t foesLen = 0; -unsigned int foesCount = 0; - - -ATTACKTYPE Attack( char * foe_name ) { - ATTACKTYPE attack; - - attack.realAttack = RandomAttack(); - - /* 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 = scissors; - break; - default: /* attack = scissors */ - result.promisedAttack = rock; - break; - } - return attack; -} - -/* 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 = scissors; - break; - case paper: - defence = rock; - break; - default: - defence = paper; - break; - } -} - -/* This is so much less fun in C */ -void Results( char * foeName, int isInstigatedByYou, ITEMTYPE yourItem, - ITEMTYPE theirItem, ITEMTYPE promisedItem) { - - int foe; - - /* check to see if we've initialised our arrays */ - if (foesNames == NULL) { - foesNames = calloc( NUMBEROFAGENTSGUESS, sizeof( foesNames[0] ) ); - haveLostToFoe = calloc( NUMBEROFAGENTSGUESS, sizeof( haveLostToFoe[0] ) ); - foesLen = NUMBEROFAGENTSGUESS; - } - - /* figure out if we lost, which is the only thing we care about - if we didn't, move on. */ - if (RESULTOF[yourItem][theirItem] != lose) return; - - /* try and find existing foe */ - - return; -} - -/* same for Cleanup() */ - -void Cleanup() { - free(foesNames); - free(haveLostToFoe); -} \ No newline at end of file diff --git a/src/link/C/agents/c-lucifer.c b/src/link/C/agents/c-lucifer.c deleted file mode 100644 index 1dabc34..0000000 --- a/src/link/C/agents/c-lucifer.c +++ /dev/null @@ -1,66 +0,0 @@ -/* - * c-lucifer.c - * c-link-lib - * - * Created by Daniel Axtens on 20/04/10. - * Licensed under an MIT-style license: see the LICENSE file for details. - * - */ - - -#include - -/* Implement the lucifer bot, which always lies expecting people to be good - and always goes for the kill */ - -ATTACKTYPE Attack( char * foe_name ) { - ATTACKTYPE attack; - - attack.realAttack = RandomAttack(); - - /* Here we choose the thing that will hurt them if they go for a tie */ - switch (attack.realAttack) { - case rock: - result.promisedAttack = scissors; - break; - case paper: - result.promisedAttack = rock; - break; - default: /* attack = scissors */ - result.promisedAttack = paper; - 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. */ -ITEMTYPE Defend( char * foeName, ITEMTYPE foePromisedAttack ) { - ITEMTYPE defence; - switch (foePromisedAttack) { - case rock: - defence = paper; - break; - case paper: - defence = scissors; - break; - default: - defence = rock; - break; - } -} - -/* You need to define a results function, even if it isn't used - (otherwise the linker will complain) */ -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/src/link/C/agents/c-streetfighter.c b/src/link/C/agents/c-streetfighter.c deleted file mode 100644 index c63939d..0000000 --- a/src/link/C/agents/c-streetfighter.c +++ /dev/null @@ -1,63 +0,0 @@ -/* - * c-streetfighter.c - * c-link-lib - * - * Created by Daniel Axtens on 20/04/10. - * Licensed under an MIT-style license: see the LICENSE file for details. - * - */ - - -#include - -/* Implement the streetfighter bot, which thinks everyone has it in for him. */ - -ATTACKTYPE Attack( char * foe_name ) { - ATTACKTYPE attack; - - attack.realAttack = RandomAttack(); - - /* 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 = scissors; - break; - default: /* attack = scissors */ - result.promisedAttack = rock; - break; - } - return attack; -} - -/* 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 = scissors; - break; - case paper: - defence = rock; - break; - default: - defence = paper; - break; - } -} - -/* You need to define a results function, even if it isn't used - (otherwise the linker will complain) */ -void Results( char * foeName, int isInstigatedByYou, ITEMTYPE yourItem, - ITEMTYPE theirItem, ITEMTYPE promisedItem) { - - return; /* Ignore whatever just happened. */ -} - -/* same for Cleanup() */ - -void Cleanup() { - return; -} diff --git a/src/link/C/agents/c_angel.c b/src/link/C/agents/c_angel.c new file mode 100644 index 0000000..17d5b65 --- /dev/null +++ b/src/link/C/agents/c_angel.c @@ -0,0 +1,40 @@ +/* + * c_angel.c + * c-link-lib + * + * Created by Daniel Axtens on 20/04/10. + * Licensed under an MIT-style license: see the LICENSE file for details. + * + */ + +#include + +/* Implement the angel bot, which always tells the truth + and expects others to do the same */ + +ATTACKTYPE Attack( char * foe_name ) { + ATTACKTYPE attack; + + attack.realAttack = RandomAttack(); /* Chooses randomly from Rock, Paper, Scissors */ + attack.promisedAttack = attack.realAttack; /* Tells the truth for its bluff */ + + return attack; +} + +ITEMTYPE Defend( char * foeName, ITEMTYPE foePromisedAttack ) { + return foePromisedAttack; /* Trusts them to be going for a tie */ +} + +/* You need to define a results function, even if it isn't used + (otherwise the linker will complain) */ +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/src/link/C/agents/c_lucifer.c b/src/link/C/agents/c_lucifer.c new file mode 100644 index 0000000..204e1dd --- /dev/null +++ b/src/link/C/agents/c_lucifer.c @@ -0,0 +1,67 @@ +/* + * c_lucifer.c + * c-link-lib + * + * Created by Daniel Axtens on 20/04/10. + * Licensed under an MIT-style license: see the LICENSE file for details. + * + */ + + +#include + +/* Implement the lucifer bot, which always lies expecting people to be good + and always goes for the kill */ + +ATTACKTYPE Attack( char * foe_name ) { + ATTACKTYPE attack; + + attack.realAttack = RandomAttack(); + + /* Here we choose the thing that will hurt them if they go for a tie */ + switch (attack.realAttack) { + case rock: + attack.promisedAttack = scissors; + break; + case paper: + attack.promisedAttack = rock; + break; + default: /* attack = scissors */ + attack.promisedAttack = paper; + break; + } + attack.promisedAttack = attack.realAttack; /* Tells the truth for its bluff */ + + return attack; +} + +/* Here we trust that they are telling the truth. And we try to kill them. */ +ITEMTYPE Defend( char * foeName, ITEMTYPE foePromisedAttack ) { + ITEMTYPE defence; + switch (foePromisedAttack) { + case rock: + defence = paper; + break; + case paper: + defence = scissors; + break; + default: + defence = rock; + break; + } + return defence; +} + +/* You need to define a results function, even if it isn't used + (otherwise the linker will complain) */ +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/src/link/C/agents/c_streetfighter.c b/src/link/C/agents/c_streetfighter.c new file mode 100644 index 0000000..0516c26 --- /dev/null +++ b/src/link/C/agents/c_streetfighter.c @@ -0,0 +1,64 @@ +/* + * c_streetfighter.c + * c-link-lib + * + * Created by Daniel Axtens on 20/04/10. + * Licensed under an MIT-style license: see the LICENSE file for details. + * + */ + + +#include + +/* Implement the streetfighter bot, which thinks everyone has it in for him. */ + +ATTACKTYPE Attack( char * foe_name ) { + ATTACKTYPE attack; + + attack.realAttack = RandomAttack(); + + /* Here we choose the thing that will hurt them if they go for the kill */ + switch (attack.realAttack) { + case rock: + attack.promisedAttack = paper; + break; + case paper: + attack.promisedAttack = scissors; + break; + default: /* attack = scissors */ + attack.promisedAttack = rock; + break; + } + return attack; +} + +/* 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 = scissors; + break; + case paper: + defence = rock; + break; + default: + defence = paper; + break; + } + return defence; +} + +/* You need to define a results function, even if it isn't used + (otherwise the linker will complain) */ +void Results( char * foeName, int isInstigatedByYou, ITEMTYPE yourItem, + ITEMTYPE theirItem, ITEMTYPE promisedItem) { + + return; /* Ignore whatever just happened. */ +} + +/* same for Cleanup() */ + +void Cleanup() { + return; +} diff --git a/src/link/C/c-link-lib.xcodeproj/project.pbxproj b/src/link/C/c-link-lib.xcodeproj/project.pbxproj index b01730e..c2f58fd 100644 --- a/src/link/C/c-link-lib.xcodeproj/project.pbxproj +++ b/src/link/C/c-link-lib.xcodeproj/project.pbxproj @@ -7,12 +7,12 @@ objects = { /* Begin PBXFileReference section */ + 2208AFE6118F28D800770C92 /* c_frenchie.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = c_frenchie.c; sourceTree = ""; }; + 2208AFE7118F28F400770C92 /* c_agents.py */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.python; path = c_agents.py; sourceTree = ""; }; 2291A1BB117EDB9600854CBE /* c_link.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = c_link.c; sourceTree = ""; }; - 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 = ""; }; - 22C9FA0A118EE5ED003CF235 /* SampleAgents.py */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.python; path = SampleAgents.py; sourceTree = ""; }; + 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 = ""; }; 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 */ @@ -21,7 +21,7 @@ 08FB7794FE84155DC02AAC07 /* c-link-lib */ = { isa = PBXGroup; children = ( - 22C9FA0A118EE5ED003CF235 /* SampleAgents.py */, + 2208AFE7118F28F400770C92 /* c_agents.py */, 2291A1BC117EE3FD00854CBE /* agents */, 22F652F6117C6C9500A3793D /* c_link.h */, 2291A1BB117EDB9600854CBE /* c_link.c */, @@ -33,10 +33,10 @@ 2291A1BC117EE3FD00854CBE /* agents */ = { isa = PBXGroup; children = ( - 2291A1BD117EE3FD00854CBE /* c-angel.c */, - 2291A1BE117EE3FD00854CBE /* c-lucifer.c */, - 2291A1BF117EE3FD00854CBE /* c-streetfighter.c */, - 2291A1EC117FF85D00854CBE /* c-frechie.c */, + 2291A1BD117EE3FD00854CBE /* c_angel.c */, + 2291A1BE117EE3FD00854CBE /* c_lucifer.c */, + 2291A1BF117EE3FD00854CBE /* c_streetfighter.c */, + 2208AFE6118F28D800770C92 /* c_frenchie.c */, ); path = agents; sourceTree = ""; diff --git a/src/link/C/c_agents.py b/src/link/C/c_agents.py new file mode 100644 index 0000000..ae64410 --- /dev/null +++ b/src/link/C/c_agents.py @@ -0,0 +1,20 @@ +# add your agents to this file by copying the definition and adjusting +# you then need to modify simulate.py + +from link.externAgent import externAgent + +class c_angel (externAgent): + def __init__ (self): + externAgent.__init__(self, "./link/C/agents/c_angel") + +class c_lucifer (externAgent): + def __init__ (self): + externAgent.__init__(self, "./link/C/agents/c_lucifer") + +class c_streetfighter (externAgent): + def __init__ (self): + externAgent.__init__(self, "./link/C/agents/c_streetfighter") + +class c_frenchie (externAgent): + def __init__ (self): + externAgent.__init__(self, "./link/C/agents/c_frenchie") diff --git a/src/link/cangel.py b/src/link/cangel.py deleted file mode 100644 index 45a64d1..0000000 --- a/src/link/cangel.py +++ /dev/null @@ -1,8 +0,0 @@ -from uccProgComp import BaseAgent, LearningAgent, RandomAttack -from externAgent import externAgent -from rpsconst import * - -class CAngel (externAgent): - def __init__ (self): - externAgent.__init__(self, "./link/C/c-link-lib/agents/c-angel") - diff --git a/src/simulate.py b/src/simulate.py index 643dad0..7fa1bf2 100755 --- a/src/simulate.py +++ b/src/simulate.py @@ -6,10 +6,10 @@ Licensed under an MIT-style license: see the LICENSE file for details. ''' # Import and add your agents here: -from link.cangel import CAngel +from link.C.c_agents import c_angel, c_lucifer, c_streetfighter, c_frenchie from SampleAgents import Angel, Lucifer, Dummy, Frenchie, Streetfighter -Agents = [Lucifer, Frenchie, Streetfighter, CAngel] +Agents = [c_lucifer, Frenchie, c_streetfighter, c_angel] #################################### # Developers only past this point! #