+++ /dev/null
-'''SampleAgents.py - A collection of sample agents for playing Rock Paper Scissors.
-Written by Luke Williams <
[email protected]> for the UCC Programming Competition in 2008.
-Requires Python 2.5.
-
-Licensed under an MIT-style license: see the LICENSE file for details.
-'''
-
-from uccProgComp import BaseAgent, LearningAgent, RandomAttack
-from rpsconst import *
-
-# Angel is a very simple bot that always tells the truth and expects others to do the same.
-class Dummy (BaseAgent):
- def Attack (self, foe):
- return Paper, Paper
- def Defend (self, foe, bluff):
- return bluff
-
-class Angel (BaseAgent):
- def Attack (self, foe):
- attack = RandomAttack () # Chooses randomly from Rock, Paper, Scissors
- return attack, attack # Tells the truth for its bluff.
- def Defend (self, foe, bluff):
- return bluff # Trusts them to be going for a tie.
-
-# Lucifer here is the opposite. He always lies expecting people to be good and always goes for the kill.
-class Lucifer (BaseAgent):
- def Attack (self, foe):
- attack = RandomAttack ()
- if attack == Rock: bluff = Scissors # Here we choose the thing
- elif attack == Paper: bluff = Rock # that will hurt them
- else: bluff = Paper # if they go for a tie.
- return attack, bluff
- def Defend (self, foe, bluff):
- if bluff == Rock: attack = Paper # Here we trust that they
- elif bluff == Paper: attack = Scissors # are telling the truth.
- else: attack = Rock # And we try to kill them.
- return attack
-# def Results (self, foeName, wasAttacker, winner, attItem, defItem, bluffItem, pointDelta):
-# BaseAgent.Results (self, foeName, wasAttacker, winner, attItem, defItem, bluffItem, pointDelta)
-# print "I just scored " + str(pointDelta) + " points!"
-
-
-# Streetfighter assumes everyone has it in for him.
-class Streetfighter (BaseAgent):
- def Attack (self, foe):
- attack = RandomAttack ()
- if attack == Rock: bluff = Paper # Here we choose the thing
- elif attack == Paper: bluff = Scissors # that will hurt them
- else: bluff = Rock # if they go for a tie.
- return attack, bluff
- def Defend (self, foe, bluff):
- if bluff == Rock: attack = Paper # Here we trust that they
- elif bluff == Paper: attack = Scissors # are telling the truth.
- else: attack = Rock # And we try to kill them.
- return attack
-
-# This is our first bot with any sort of learning capability, based on the LearningAgent base.
-# Experienced programmers might opt to write their own learning code based on BaseAgent, but it's up to you.
-# Frenchie is a simple bot that is by default nice but will permanently turn against any agent that betrays it.
-class Frenchie (LearningAgent):
- def Attack (self, foe):
- attack = RandomAttack ()
- if Loss in LearningAgent.GetWinHistory (self, foe):
- if attack == Rock: bluff = Scissors
- elif attack == Paper: bluff = Rock
- else: bluff = Paper
- else:
- bluff = attack
- return attack, bluff
- def Defend (self, foe, bluff):
- if Loss in LearningAgent.GetWinHistory (self, foe):
- if bluff == Rock: attack = Scissors # They've fucked us in the past,
- elif bluff == Paper: attack = Rock # so we assume they're lying and
- else: attack = Paper # hoping we go for a kill.
- else:
- attack = bluff
- return attack
-
-
-# If you want to implement your own Results () callback, you have to call the parent class's first:
-class Blank (BaseAgent):
- def Attack (self, foe):
- return Paper, Paper
- def Defend (self, foe, bluff):
- return bluff
- def Results (self, foeName, wasAttacker, winner, attItem, defItem, bluffItem, pointDelta):
- BaseAgent.Results (self, foeName, wasAttacker, winner, attItem, defItem, bluffItem, pointDelta)
- # Now you can do your own thing
-
--- /dev/null
+agents/
+!agents/*.c
--- /dev/null
+CC=gcc
+AR=ar
+
+CFLAGS=-Wall -I.
+LDFLAGS=-lc_link -L.
+
+LINKSRCS=c_link.c
+LINKOBJS=$(LINKSRCS:.c=.o)
+LINKLIB=libc_link.a
+
+AGENTSRCS=$(wildcard agents/*.c)
+AGENTS=$(AGENTSRCS:.c=)
+
+all: $(LINKSRCS) $(LINKLIB) $(AGENTS)
+
+$(LINKLIB): $(LINKOBJS)
+ $(AR) rcs $(LINKLIB) $(LINKOBJS)
+
+$(AGENTS): $(AGENTSRCS)
+ @echo Building $<
+ $(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
+
+.c.o: c_link.h
+ $(CC) -c $(CFLAGS) $< -o $@
+
+.PHONY : clean
+clean:
+ rm $(LINKOBJS) $(LINKLIB) $(AGENTS)
--- /dev/null
+/*
+ * 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 <c_link.h>
+
+/* 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
--- /dev/null
+/*
+ * 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 <c_link.h>
+#include <stdlib.h>
+
+/* 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
--- /dev/null
+/*
+ * 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 <c_link.h>
+
+/* 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
--- /dev/null
+/*
+ * 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 <c_link.h>
+
+/* 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;
+}
--- /dev/null
+// !$*UTF8*$!
+{
+ archiveVersion = 1;
+ classes = {
+ };
+ objectVersion = 45;
+ objects = {
+
+/* Begin PBXFileReference section */
+ 2291A1BB117EDB9600854CBE /* c_link.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = c_link.c; sourceTree = "<group>"; };
+ 2291A1BD117EE3FD00854CBE /* c-angel.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "c-angel.c"; sourceTree = "<group>"; };
+ 2291A1BE117EE3FD00854CBE /* c-lucifer.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "c-lucifer.c"; sourceTree = "<group>"; };
+ 2291A1BF117EE3FD00854CBE /* c-streetfighter.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "c-streetfighter.c"; sourceTree = "<group>"; };
+ 2291A1EC117FF85D00854CBE /* c-frechie.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "c-frechie.c"; sourceTree = "<group>"; };
+ 22C9FA0A118EE5ED003CF235 /* SampleAgents.py */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.python; path = SampleAgents.py; sourceTree = "<group>"; };
+ 22F652F5117C679300A3793D /* Makefile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.make; path = Makefile; sourceTree = "<group>"; };
+ 22F652F6117C6C9500A3793D /* c_link.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = c_link.h; sourceTree = "<group>"; };
+/* End PBXFileReference section */
+
+/* Begin PBXGroup section */
+ 08FB7794FE84155DC02AAC07 /* c-link-lib */ = {
+ isa = PBXGroup;
+ children = (
+ 22C9FA0A118EE5ED003CF235 /* SampleAgents.py */,
+ 2291A1BC117EE3FD00854CBE /* agents */,
+ 22F652F6117C6C9500A3793D /* c_link.h */,
+ 2291A1BB117EDB9600854CBE /* c_link.c */,
+ 22F652F5117C679300A3793D /* Makefile */,
+ );
+ name = "c-link-lib";
+ sourceTree = "<group>";
+ };
+ 2291A1BC117EE3FD00854CBE /* agents */ = {
+ isa = PBXGroup;
+ children = (
+ 2291A1BD117EE3FD00854CBE /* c-angel.c */,
+ 2291A1BE117EE3FD00854CBE /* c-lucifer.c */,
+ 2291A1BF117EE3FD00854CBE /* c-streetfighter.c */,
+ 2291A1EC117FF85D00854CBE /* c-frechie.c */,
+ );
+ path = agents;
+ sourceTree = "<group>";
+ };
+/* End PBXGroup section */
+
+/* Begin PBXLegacyTarget section */
+ D28A88AD04BDD90700651E21 /* c-link-lib */ = {
+ isa = PBXLegacyTarget;
+ buildArgumentsString = "$(ACTION)";
+ buildConfigurationList = 1DEB918F08733D9F0010E9CD /* Build configuration list for PBXLegacyTarget "c-link-lib" */;
+ buildPhases = (
+ );
+ buildToolPath = /usr/bin/make;
+ buildWorkingDirectory = "";
+ dependencies = (
+ );
+ name = "c-link-lib";
+ passBuildSettingsInEnvironment = 1;
+ productName = "c-link-lib";
+ };
+/* End PBXLegacyTarget section */
+
+/* Begin PBXProject section */
+ 08FB7793FE84155DC02AAC07 /* Project object */ = {
+ isa = PBXProject;
+ buildConfigurationList = 1DEB919308733D9F0010E9CD /* Build configuration list for PBXProject "c-link-lib" */;
+ compatibilityVersion = "Xcode 3.1";
+ hasScannedForEncodings = 1;
+ mainGroup = 08FB7794FE84155DC02AAC07 /* c-link-lib */;
+ projectDirPath = "";
+ projectRoot = "";
+ targets = (
+ D28A88AD04BDD90700651E21 /* c-link-lib */,
+ );
+ };
+/* End PBXProject section */
+
+/* Begin XCBuildConfiguration section */
+ 1DEB919008733D9F0010E9CD /* Debug */ = {
+ isa = XCBuildConfiguration;
+ buildSettings = {
+ COPY_PHASE_STRIP = NO;
+ DEBUGGING_SYMBOLS = YES;
+ GCC_DYNAMIC_NO_PIC = NO;
+ GCC_ENABLE_FIX_AND_CONTINUE = YES;
+ GCC_GENERATE_DEBUGGING_SYMBOLS = YES;
+ GCC_OPTIMIZATION_LEVEL = 0;
+ OTHER_CFLAGS = "";
+ OTHER_LDFLAGS = "";
+ PRODUCT_NAME = "c-link-lib";
+ };
+ name = Debug;
+ };
+ 1DEB919108733D9F0010E9CD /* Release */ = {
+ isa = XCBuildConfiguration;
+ buildSettings = {
+ COPY_PHASE_STRIP = YES;
+ GCC_ENABLE_FIX_AND_CONTINUE = NO;
+ OTHER_CFLAGS = "";
+ OTHER_LDFLAGS = "";
+ PRODUCT_NAME = "c-link-lib";
+ };
+ name = Release;
+ };
+ 1DEB919408733D9F0010E9CD /* Debug */ = {
+ isa = XCBuildConfiguration;
+ buildSettings = {
+ ARCHS = "$(ARCHS_STANDARD_32_BIT)";
+ GCC_WARN_ABOUT_RETURN_TYPE = YES;
+ GCC_WARN_UNUSED_VARIABLE = YES;
+ ONLY_ACTIVE_ARCH = YES;
+ PREBINDING = NO;
+ SDKROOT = macosx10.6;
+ };
+ name = Debug;
+ };
+ 1DEB919508733D9F0010E9CD /* Release */ = {
+ isa = XCBuildConfiguration;
+ buildSettings = {
+ ARCHS = "$(ARCHS_STANDARD_32_BIT)";
+ GCC_WARN_ABOUT_RETURN_TYPE = YES;
+ GCC_WARN_UNUSED_VARIABLE = YES;
+ PREBINDING = NO;
+ SDKROOT = macosx10.6;
+ };
+ name = Release;
+ };
+/* End XCBuildConfiguration section */
+
+/* Begin XCConfigurationList section */
+ 1DEB918F08733D9F0010E9CD /* Build configuration list for PBXLegacyTarget "c-link-lib" */ = {
+ isa = XCConfigurationList;
+ buildConfigurations = (
+ 1DEB919008733D9F0010E9CD /* Debug */,
+ 1DEB919108733D9F0010E9CD /* Release */,
+ );
+ defaultConfigurationIsVisible = 0;
+ defaultConfigurationName = Release;
+ };
+ 1DEB919308733D9F0010E9CD /* Build configuration list for PBXProject "c-link-lib" */ = {
+ isa = XCConfigurationList;
+ buildConfigurations = (
+ 1DEB919408733D9F0010E9CD /* Debug */,
+ 1DEB919508733D9F0010E9CD /* Release */,
+ );
+ defaultConfigurationIsVisible = 0;
+ defaultConfigurationName = Release;
+ };
+/* End XCConfigurationList section */
+ };
+ rootObject = 08FB7793FE84155DC02AAC07 /* Project object */;
+}
+++ /dev/null
-agents/
-!agents/*.c
+++ /dev/null
-CC=gcc
-AR=ar
-
-CFLAGS=-Wall -I.
-LDFLAGS=-lc_link -L.
-
-LINKSRCS=c_link.c
-LINKOBJS=$(LINKSRCS:.c=.o)
-LINKLIB=libc_link.a
-
-AGENTSRCS=$(wildcard agents/*.c)
-AGENTS=$(AGENTSRCS:.c=)
-
-all: $(LINKSRCS) $(LINKLIB) $(AGENTS)
-
-$(LINKLIB): $(LINKOBJS)
- $(AR) rcs $(LINKLIB) $(LINKOBJS)
-
-$(AGENTS): $(AGENTSRCS)
- @echo Building $<
- $(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
-
-.c.o: c_link.h
- $(CC) -c $(CFLAGS) $< -o $@
-
-.PHONY : clean
-clean:
- rm $(LINKOBJS) $(LINKLIB) $(AGENTS)
+++ /dev/null
-/*
- * 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 <c_link.h>
-
-/* 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
+++ /dev/null
-/*
- * 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 <c_link.h>
-#include <stdlib.h>
-
-/* 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
+++ /dev/null
-/*
- * 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 <c_link.h>
-
-/* 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
+++ /dev/null
-/*
- * 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 <c_link.h>
-
-/* 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;
-}
+++ /dev/null
-// !$*UTF8*$!
-{
- archiveVersion = 1;
- classes = {
- };
- objectVersion = 45;
- objects = {
-
-/* Begin PBXFileReference section */
- 2291A1BB117EDB9600854CBE /* c_link.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = c_link.c; sourceTree = "<group>"; };
- 2291A1BD117EE3FD00854CBE /* c-angel.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "c-angel.c"; sourceTree = "<group>"; };
- 2291A1BE117EE3FD00854CBE /* c-lucifer.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "c-lucifer.c"; sourceTree = "<group>"; };
- 2291A1BF117EE3FD00854CBE /* c-streetfighter.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "c-streetfighter.c"; sourceTree = "<group>"; };
- 2291A1EC117FF85D00854CBE /* c-frechie.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "c-frechie.c"; sourceTree = "<group>"; };
- 22C9FA0A118EE5ED003CF235 /* SampleAgents.py */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.python; path = SampleAgents.py; sourceTree = "<group>"; };
- 22F652F5117C679300A3793D /* Makefile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.make; path = Makefile; sourceTree = "<group>"; };
- 22F652F6117C6C9500A3793D /* c_link.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = c_link.h; sourceTree = "<group>"; };
-/* End PBXFileReference section */
-
-/* Begin PBXGroup section */
- 08FB7794FE84155DC02AAC07 /* c-link-lib */ = {
- isa = PBXGroup;
- children = (
- 22C9FA0A118EE5ED003CF235 /* SampleAgents.py */,
- 2291A1BC117EE3FD00854CBE /* agents */,
- 22F652F6117C6C9500A3793D /* c_link.h */,
- 2291A1BB117EDB9600854CBE /* c_link.c */,
- 22F652F5117C679300A3793D /* Makefile */,
- );
- name = "c-link-lib";
- sourceTree = "<group>";
- };
- 2291A1BC117EE3FD00854CBE /* agents */ = {
- isa = PBXGroup;
- children = (
- 2291A1BD117EE3FD00854CBE /* c-angel.c */,
- 2291A1BE117EE3FD00854CBE /* c-lucifer.c */,
- 2291A1BF117EE3FD00854CBE /* c-streetfighter.c */,
- 2291A1EC117FF85D00854CBE /* c-frechie.c */,
- );
- path = agents;
- sourceTree = "<group>";
- };
-/* End PBXGroup section */
-
-/* Begin PBXLegacyTarget section */
- D28A88AD04BDD90700651E21 /* c-link-lib */ = {
- isa = PBXLegacyTarget;
- buildArgumentsString = "$(ACTION)";
- buildConfigurationList = 1DEB918F08733D9F0010E9CD /* Build configuration list for PBXLegacyTarget "c-link-lib" */;
- buildPhases = (
- );
- buildToolPath = /usr/bin/make;
- buildWorkingDirectory = "";
- dependencies = (
- );
- name = "c-link-lib";
- passBuildSettingsInEnvironment = 1;
- productName = "c-link-lib";
- };
-/* End PBXLegacyTarget section */
-
-/* Begin PBXProject section */
- 08FB7793FE84155DC02AAC07 /* Project object */ = {
- isa = PBXProject;
- buildConfigurationList = 1DEB919308733D9F0010E9CD /* Build configuration list for PBXProject "c-link-lib" */;
- compatibilityVersion = "Xcode 3.1";
- hasScannedForEncodings = 1;
- mainGroup = 08FB7794FE84155DC02AAC07 /* c-link-lib */;
- projectDirPath = "";
- projectRoot = "";
- targets = (
- D28A88AD04BDD90700651E21 /* c-link-lib */,
- );
- };
-/* End PBXProject section */
-
-/* Begin XCBuildConfiguration section */
- 1DEB919008733D9F0010E9CD /* Debug */ = {
- isa = XCBuildConfiguration;
- buildSettings = {
- COPY_PHASE_STRIP = NO;
- DEBUGGING_SYMBOLS = YES;
- GCC_DYNAMIC_NO_PIC = NO;
- GCC_ENABLE_FIX_AND_CONTINUE = YES;
- GCC_GENERATE_DEBUGGING_SYMBOLS = YES;
- GCC_OPTIMIZATION_LEVEL = 0;
- OTHER_CFLAGS = "";
- OTHER_LDFLAGS = "";
- PRODUCT_NAME = "c-link-lib";
- };
- name = Debug;
- };
- 1DEB919108733D9F0010E9CD /* Release */ = {
- isa = XCBuildConfiguration;
- buildSettings = {
- COPY_PHASE_STRIP = YES;
- GCC_ENABLE_FIX_AND_CONTINUE = NO;
- OTHER_CFLAGS = "";
- OTHER_LDFLAGS = "";
- PRODUCT_NAME = "c-link-lib";
- };
- name = Release;
- };
- 1DEB919408733D9F0010E9CD /* Debug */ = {
- isa = XCBuildConfiguration;
- buildSettings = {
- ARCHS = "$(ARCHS_STANDARD_32_BIT)";
- GCC_WARN_ABOUT_RETURN_TYPE = YES;
- GCC_WARN_UNUSED_VARIABLE = YES;
- ONLY_ACTIVE_ARCH = YES;
- PREBINDING = NO;
- SDKROOT = macosx10.6;
- };
- name = Debug;
- };
- 1DEB919508733D9F0010E9CD /* Release */ = {
- isa = XCBuildConfiguration;
- buildSettings = {
- ARCHS = "$(ARCHS_STANDARD_32_BIT)";
- GCC_WARN_ABOUT_RETURN_TYPE = YES;
- GCC_WARN_UNUSED_VARIABLE = YES;
- PREBINDING = NO;
- SDKROOT = macosx10.6;
- };
- name = Release;
- };
-/* End XCBuildConfiguration section */
-
-/* Begin XCConfigurationList section */
- 1DEB918F08733D9F0010E9CD /* Build configuration list for PBXLegacyTarget "c-link-lib" */ = {
- isa = XCConfigurationList;
- buildConfigurations = (
- 1DEB919008733D9F0010E9CD /* Debug */,
- 1DEB919108733D9F0010E9CD /* Release */,
- );
- defaultConfigurationIsVisible = 0;
- defaultConfigurationName = Release;
- };
- 1DEB919308733D9F0010E9CD /* Build configuration list for PBXProject "c-link-lib" */ = {
- isa = XCConfigurationList;
- buildConfigurations = (
- 1DEB919408733D9F0010E9CD /* Debug */,
- 1DEB919508733D9F0010E9CD /* Release */,
- );
- defaultConfigurationIsVisible = 0;
- defaultConfigurationName = Release;
- };
-/* End XCConfigurationList section */
- };
- rootObject = 08FB7793FE84155DC02AAC07 /* Project object */;
-}
+++ /dev/null
-/*
- * c_link.c
- * c-link-lib
- *
- * Created by Daniel Axtens on 19/04/10.
- * Licensed under an MIT-style license: see the LICENSE file for details.
- *
- */
-
-#include "c_link.h"
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
-#include <time.h>
-
-/* You don't need to read this file.
- All you have to do is implement the bot 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"};
-
-/* 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;
-}
-
-ITEMTYPE stringToItem( char * str ) {
- if (strcasecmp( str, "Rock" ) == 0) return rock;
- if (strcasecmp( str, "Paper" ) == 0) return paper;
- if (strcasecmp( str, "Scissors" ) == 0) return scissors;
- /* If we reach this point, we've got real problems. */
- fprintf( stderr, "Attempt to convert invalid string \"%s\" into an ITEMTYPE! Aborting.\n", str );
- exit(EXIT_FAILURE);
- return -1;
-}
-
-
-int main( int argc, char * argv[] ) {
- srand( time( NULL ) );
-
- char command[MAXCOMMANDLEN];
- char foeName[MAXFOENAMELEN];
- char yourItem[MAXITEMLEN], theirItem[MAXITEMLEN], promisedItem[MAXITEMLEN];
- char didYouInstigate[MAXBOOLLEN], childSpawned[MAXBOOLLEN];
- int pointChange;
-
- ATTACKTYPE attack;
- ITEMTYPE defence;
-
- scanf( "%s", command );
-
- while (strcasecmp("BYE",command) != 0) {
-
- if (strcasecmp("ATTACK", command) == 0) {
- scanf( "%s", foeName );
- attack = Attack( foeName );
- printf("ATTACKING %s %s\n", ITEMNAMES[attack.realAttack], ITEMNAMES[attack.promisedAttack]);
-
- } else if (strcasecmp("DEFEND", command) == 0) {
- scanf( "%s %s", foeName, promisedItem );
- defence = Defend(foeName, stringToItem(promisedItem));
- printf("DEFENDING %s\n", ITEMNAMES[defence]);
-
- } else if (strcasecmp("RESULTS", command) == 0) {
- scanf( "%s %s %s %s %s %d %s", foeName, didYouInstigate, yourItem, theirItem, promisedItem, &pointChange, childSpawned );
- Results(foeName, (strcasecmp("False",didYouInstigate)==0),
- stringToItem(yourItem), stringToItem(theirItem), stringToItem(promisedItem));
- printf("OK\n");
- }
-
- // read the next command!
- scanf( "%s", command );
- }
-
- Cleanup();
-
- return 0;
-}
\ No newline at end of file
+++ /dev/null
-/*
- * c_link.h
- * c-link-lib
- *
- * Created by Daniel Axtens on 19/04/10.
- * Licensed under an MIT-style license: see the LICENSE file for details.
- *
- */
-
-#include <stdio.h>
-
-#define MAXCOMMANDLEN 15
-#define MAXFOENAMELEN 50
-#define MAXITEMLEN 10
-#define MAXBOOLLEN 6
-
-/********** Type definitions **********/
-
-/* 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 {
- ITEMTYPE realAttack;
- ITEMTYPE promisedAttack;
-} ATTACKTYPE;
-
-
-/********** 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();
-
-/* A useful translation table
- eg printf( "I use %s.\n", ITEMNAMES[rock] ); */
-
-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 */
-
-/* Defend( foeName : string - the name of your foe;
- foePromisedAttack : ITEMTYPE - the item your foe promised to use
- ) : ITEMTYPE - the item you wish to use to defend;
-
- Called when your agent needs to defend itself.
-
- */
-ITEMTYPE Defend( char * foeName, ITEMTYPE foePromisedAttack );
-
-
-/* Attack( foeName : string - the name of your foe
- ) : ATTACKTYPE - the real and promised attack you wish to use
-
- Called when your agent needs to attack another agent.
-
- */
-ATTACKTYPE Attack( char * foeName );
-
-
-/* Results( foeName : string - the name of your foe;
- isInstigatedByYou : 0=you defended/1=you attacked;
- yourItem : ITEMTYPE - the item you used;
- theirItem : ITEMTYPE - the item they used;
- promisedItem : ITEMTYPE - the item that was promised
- );
-
- Called after your agent battles another agent, to tell you how the battle goes.
-
- */
-void Results( char * foeName, int isInstigatedByYou, ITEMTYPE yourItem,
- 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
--- /dev/null
+/*
+ * c_link.c
+ * c-link-lib
+ *
+ * Created by Daniel Axtens on 19/04/10.
+ * Licensed under an MIT-style license: see the LICENSE file for details.
+ *
+ */
+
+#include "c_link.h"
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <time.h>
+
+/* You don't need to read this file.
+ All you have to do is implement the bot 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"};
+
+/* 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;
+}
+
+ITEMTYPE stringToItem( char * str ) {
+ if (strcasecmp( str, "Rock" ) == 0) return rock;
+ if (strcasecmp( str, "Paper" ) == 0) return paper;
+ if (strcasecmp( str, "Scissors" ) == 0) return scissors;
+ /* If we reach this point, we've got real problems. */
+ fprintf( stderr, "Attempt to convert invalid string \"%s\" into an ITEMTYPE! Aborting.\n", str );
+ exit(EXIT_FAILURE);
+ return -1;
+}
+
+
+int main( int argc, char * argv[] ) {
+ srand( time( NULL ) );
+
+ char command[MAXCOMMANDLEN];
+ char foeName[MAXFOENAMELEN];
+ char yourItem[MAXITEMLEN], theirItem[MAXITEMLEN], promisedItem[MAXITEMLEN];
+ char didYouInstigate[MAXBOOLLEN], childSpawned[MAXBOOLLEN];
+ int pointChange;
+
+ ATTACKTYPE attack;
+ ITEMTYPE defence;
+
+ scanf( "%s", command );
+
+ while (strcasecmp("BYE",command) != 0) {
+
+ if (strcasecmp("ATTACK", command) == 0) {
+ scanf( "%s", foeName );
+ attack = Attack( foeName );
+ printf("ATTACKING %s %s\n", ITEMNAMES[attack.realAttack], ITEMNAMES[attack.promisedAttack]);
+
+ } else if (strcasecmp("DEFEND", command) == 0) {
+ scanf( "%s %s", foeName, promisedItem );
+ defence = Defend(foeName, stringToItem(promisedItem));
+ printf("DEFENDING %s\n", ITEMNAMES[defence]);
+
+ } else if (strcasecmp("RESULTS", command) == 0) {
+ scanf( "%s %s %s %s %s %d %s", foeName, didYouInstigate, yourItem, theirItem, promisedItem, &pointChange, childSpawned );
+ Results(foeName, (strcasecmp("False",didYouInstigate)==0),
+ stringToItem(yourItem), stringToItem(theirItem), stringToItem(promisedItem));
+ printf("OK\n");
+ }
+
+ // read the next command!
+ scanf( "%s", command );
+ }
+
+ Cleanup();
+
+ return 0;
+}
\ No newline at end of file
--- /dev/null
+/*
+ * c_link.h
+ * c-link-lib
+ *
+ * Created by Daniel Axtens on 19/04/10.
+ * Licensed under an MIT-style license: see the LICENSE file for details.
+ *
+ */
+
+#include <stdio.h>
+
+#define MAXCOMMANDLEN 15
+#define MAXFOENAMELEN 50
+#define MAXITEMLEN 10
+#define MAXBOOLLEN 6
+
+/********** Type definitions **********/
+
+/* 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 {
+ ITEMTYPE realAttack;
+ ITEMTYPE promisedAttack;
+} ATTACKTYPE;
+
+
+/********** 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();
+
+/* A useful translation table
+ eg printf( "I use %s.\n", ITEMNAMES[rock] ); */
+
+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 */
+
+/* Defend( foeName : string - the name of your foe;
+ foePromisedAttack : ITEMTYPE - the item your foe promised to use
+ ) : ITEMTYPE - the item you wish to use to defend;
+
+ Called when your agent needs to defend itself.
+
+ */
+ITEMTYPE Defend( char * foeName, ITEMTYPE foePromisedAttack );
+
+
+/* Attack( foeName : string - the name of your foe
+ ) : ATTACKTYPE - the real and promised attack you wish to use
+
+ Called when your agent needs to attack another agent.
+
+ */
+ATTACKTYPE Attack( char * foeName );
+
+
+/* Results( foeName : string - the name of your foe;
+ isInstigatedByYou : 0=you defended/1=you attacked;
+ yourItem : ITEMTYPE - the item you used;
+ theirItem : ITEMTYPE - the item they used;
+ promisedItem : ITEMTYPE - the item that was promised
+ );
+
+ Called after your agent battles another agent, to tell you how the battle goes.
+
+ */
+void Results( char * foeName, int isInstigatedByYou, ITEMTYPE yourItem,
+ 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
+++ /dev/null
-'''uccProgComp.py - A supervisor candidate for Rock Paper Scissors.
-Written by Luke Williams <
[email protected]> for the UCC Programming Competition in 2008.
-Requires Python 2.5.
-
-Licensed under an MIT-style license: see the LICENSE file for details.
-'''
-
-
-import random, uuid
-random.seed ()
-
-from rpsconst import *
-
-DEFAULT_HEALTH = 50
-REPRODUCE_HEALTH = 100
-DIE_HEALTH = 0
-MAX_AGE = 100
-
-DEBUG_MODE = False
-
-# Game dynamics - these are not final:
-# WINNER TRUTH WINNER, LOSER
-pointsTable [Attacker] [False] = (2, -2)
-pointsTable [Attacker] [True] = (2, -2)
-pointsTable [Defender] [False] = (-2, 2)
-pointsTable [Defender] [True] = (-2, 2)
-pointsTable [Tie] [False] = (-1, -1)
-pointsTable [Tie] [True] = (1, 1)
-
-def Debug (f):
- def g (*args):
- if DEBUG_MODE:
- print f.__name__, args[1].__class__.__name__, args[1].GetID ()
- return f (*args)
- return g
-
-class BaseAgent:
- def __init__ (self):
- self.id = uuid.uuid4().int
- self.__points = DEFAULT_HEALTH
- # The index will be changing all the time. It can go stale as soon as something dies.
- # So use it cautiously.
- self.__currentIndex = 0
- self.__reproduced = False
- self.__age = 0
-
- def GetCurrentIndex (self):
- return self.__currentIndex
-
- def SetCurrentIndex (self, index):
- self.__currentIndex = index
-
- def GetID (self):
- return self.id
-
- def GetPoints (self):
- return self.__points
-
- def SetPoints (self, points):
- self.__points = points
-
- def Defend (self, foe, bluff):
- return Rock
-
- def Attack (self, foe):
- return Rock
-
- def IsDead (self):
- return self.__points <= DIE_HEALTH
-
- def Reproduced (self):
- self.__points = DEFAULT_HEALTH
- self.__reproduced = True
-
- def HasReproduced (self):
- return self.__reproduced
-
- def SetReproduced (self, repro):
- self.__reproduced = repro
-
- def Results (self, foeName, wasAttacker, winner, attItem, defItem, bluffItem, pointDelta):
- self.__points += pointDelta
- self.__age += 1
- if self.__age > MAX_AGE: self.__points = DIE_HEALTH
-
-class LearningAgent (BaseAgent):
- def __init__ (self):
- BaseAgent.__init__ (self)
- self.winHistory = {}
-
- def Results (self, foeName, wasAttacker, winner, attItem, defItem, bluffItem, pointDelta):
- BaseAgent.Results (self, foeName, wasAttacker, winner, attItem, defItem, bluffItem, pointDelta)
- if wasAttacker:
- if winner == Attacker: result = Win
- elif winner == Tie: result = Tie
- else: result = Loss
- else:
- if winner == Attacker: result = Loss
- elif winner == Tie: result = Tie
- else: result = Win
-
- if foeName in self.winHistory: self.winHistory [foeName].append (result)
- else: self.winHistory [foeName] = [result]
-
- def GetWinHistory (self, foeName):
- if foeName in self.winHistory: return self.winHistory [foeName]
- else: return []
-
-class Supervisor:
- def __init__ (self):
- # The full list of living agents
- self.population = []
- # A list of classes for each agent type
- self.agentTypes = []
- # The current iteration
- self.iteration = 0
- self.agentStats = {}
- self.pendingDeaths = []
-
- def RegisterAgent (self, agent):
- self.agentTypes.append (agent)
-
- def GeneratePopulation (self, nAgentsPerClass):
- for Agent in self.agentTypes:
- for i in range (0,nAgentsPerClass): self.population.append (Agent ())
- self.agentStats [str(Agent)] = [nAgentsPerClass,0,0]
-
- def Iterate (self):
- self.ClearStats ()
- self.UpdateIndexes ()
- self.iteration += 1
- for attacker, defender in self.Select ():
- attack, bluff = attacker.Attack (defender.GetID ())
- defense = defender.Defend (attacker.GetID (), bluff)
- winner = resultTable [attack] [defense]
- attPoints, defPoints = pointsTable [winner][attack == bluff]
- attacker.Results (defender.GetID (), True, winner, attack, defense, bluff, attPoints)
- defender.Results (attacker.GetID (), False, winner, attack, defense, bluff, defPoints)
- if attacker.IsDead (): self.KillAgent (attacker)
- elif attacker.GetPoints () >= REPRODUCE_HEALTH: self.SpawnAgent (attacker)
- if defender.IsDead (): self.KillAgent (defender)
- elif defender.GetPoints () >= REPRODUCE_HEALTH: self.SpawnAgent (defender)
-
- def IsGameOver (self):
- if self.population == []: return True
- liveAgents = [id for id,stats in self.agentStats.iteritems () if stats[0] > 0]
- print liveAgents
- if len(liveAgents) < 2: return True
- return False
-
- # This is needed because when we pick the players we also need a way of identifying them in the
- # population list without manually searching each time. O(n) each iteration is better than O(n)
- # each death. It also resets the check for if the agent has reproduced this round.
- def UpdateIndexes (self):
- for agentID in reversed(sorted(self.pendingDeaths)): del self.population [agentID]
- for index, agent in enumerate(self.population):
- agent.SetCurrentIndex (index)
- agent.SetReproduced (False)
- self.pendingDeaths = []
-
- @Debug
- def KillAgent (self, agent):
- self.pendingDeaths.append (agent.GetCurrentIndex ())
- stat = self.agentStats [str(agent.__class__)]
- stat [0] -= 1
- stat [2] += 1
-
- @Debug
- def SpawnAgent (self, agent):
- child = agent.__class__ ()
- self.population.append (child)
- agent.Reproduced ()
- stat = self.agentStats [str(agent.__class__)]
- stat [0] += 1
- stat [1] += 1
-
- def Select (self):
- # This approach causes agents to keep fighting until they've either died or reproduced.
- remaining = self.population[:]
- attackerID = defenderID = random.randint (0,len(remaining)-1)
- attacker = defender = remaining [attackerID]
- while len (remaining) >= 2:
- # Check to see if the attacker from last round needs to be dropped.
- if attacker.IsDead () or attacker.HasReproduced ():
- remaining.pop (attackerID)
- if not len(remaining) >= 2: continue
- if defenderID > attackerID: defenderID -= 1
- # Check to see if the defender from last round is up for some attacking.
- if defender.IsDead () or defender.HasReproduced ():
- remaining.pop (defenderID)
- if not len(remaining) >= 2: continue
- attackerID = random.randint (0,len(remaining)-1)
- attacker = remaining [attackerID]
- else:
- attacker = defender
- attackerID = defenderID
- defender = None
- defenderID = random.randint (0,len(remaining)-2)
- if defenderID >= attackerID: defenderID += 1
- defender = remaining [defenderID]
-
- yield attacker, defender
-
- def GetStats (self):
- return self.agentStats
-
- def ClearStats (self):
- for agent in self.agentTypes: self.agentStats [str(agent)] = self.agentStats [str(agent)] [:1] + [0,0]
-
-def RandomAttack ():
- return random.randint (0,2)