From da3879474d0af90a50ff5837e3c2763e281a7204 Mon Sep 17 00:00:00 2001 From: Nicky Case Date: Fri, 23 Jun 2017 17:57:22 -0400 Subject: [PATCH] lil fixes yo --- TODO.md | 14 +++ css/slides.css | 2 +- index.html | 2 +- js/main.js | 4 +- js/sims/PD.js | 121 +++++++++++++++++--- js/sims/{TournamentSim.js => Tournament.js} | 45 ++++---- 6 files changed, 145 insertions(+), 43 deletions(-) create mode 100644 TODO.md rename js/sims/{TournamentSim.js => Tournament.js} (92%) diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..aab6f16 --- /dev/null +++ b/TODO.md @@ -0,0 +1,14 @@ +MAJOR SHTUFF + +1. Sandbox with all parameters exposed // exposed... but time to make the UI! +2. Repeated game with all parameters exposed +3. Splash, with params for cool outro +4. Integrate *everything*. // slides, outro +5. Credits and rewards + +MINOR SHTUFF + +- Word box class less annoying +- Refactoring, ugh +- Logic: Prober, Pavlov, TF2T, Random? // done +- Slides in different files \ No newline at end of file diff --git a/css/slides.css b/css/slides.css index a10402c..9246727 100644 --- a/css/slides.css +++ b/css/slides.css @@ -77,7 +77,7 @@ body{ text-align: center; position: absolute; - top:17px; left:5px; + top:18px; left:5px; } .button #hitbox{ diff --git a/index.html b/index.html index 83520a2..7b78f8a 100644 --- a/index.html +++ b/index.html @@ -32,7 +32,7 @@ - + diff --git a/js/main.js b/js/main.js index d00717e..ec06036 100644 --- a/js/main.js +++ b/js/main.js @@ -4,7 +4,7 @@ var slides = [ { id: "sim", add:[ - {id:"tournament", type:"TournamentSim", x:0, y:20}, + {id:"tournament", type:"Tournament", x:0, y:20}, { id:"_w1", type:"WordBox", x:500, y:0, width:460, height:50, @@ -39,7 +39,7 @@ var slides = [ id:"_w3", type:"WordBox", x:500, y:370, width:460, height:200, text:"Always Cheat dominates at first, but when it runs out of suckers to exploit, "+ - "its empires collapses – and the fairer Tit For Tat takes over.
"+ + "its empire collapses – and the fairer Tit For Tat takes over.
"+ "
"+ "We are not punished for our sins, but by them.
"+ "- Elbert Hubbard" diff --git a/js/sims/PD.js b/js/sims/PD.js index cc7bc3a..10fe785 100644 --- a/js/sims/PD.js +++ b/js/sims/PD.js @@ -2,28 +2,43 @@ var PD = {}; PD.COOPERATE = "COOPERATE"; PD.CHEAT = "CHEAT"; -PD.P = 0; // punishment: neither of you get anything -PD.S = -1; // sucker: you put in coin, other didn't. -PD.R = 2; // reward: you both put 1 coin in, both got 3 back -PD.T = 3; // temptation: you put no coin, got 3 coins anyway +PD.PAYOFFS_DEFAULT = { + P: 0, // punishment: neither of you get anything + S: -1, // sucker: you put in coin, other didn't. + R: 2, // reward: you both put 1 coin in, both got 3 back + T: 3 // temptation: you put no coin, got 3 coins anyway +}; + +PD.PAYOFFS = PD.PAYOFFS_DEFAULT; + +PD.NOISE = 0.0; PD.getPayoffs = function(move1, move2){ - if(move1==PD.CHEAT && move2==PD.CHEAT) return [PD.P, PD.P]; // both punished - if(move1==PD.COOPERATE && move2==PD.CHEAT) return [PD.S, PD.T]; // sucker - temptation - if(move1==PD.CHEAT && move2==PD.COOPERATE) return [PD.T, PD.S]; // temptation - sucker - if(move1==PD.COOPERATE && move2==PD.COOPERATE) return [PD.R, PD.R]; // both rewarded + var payoffs = PD.PAYOFFS; + if(move1==PD.CHEAT && move2==PD.CHEAT) return [payoffs.P, payoffs.P]; // both punished + if(move1==PD.COOPERATE && move2==PD.CHEAT) return [payoffs.S, payoffs.T]; // sucker - temptation + if(move1==PD.CHEAT && move2==PD.COOPERATE) return [payoffs.T, payoffs.S]; // temptation - sucker + if(move1==PD.COOPERATE && move2==PD.COOPERATE) return [payoffs.R, payoffs.R]; // both rewarded }; PD.playOneGame = function(playerA, playerB){ + // Make your moves! var A = playerA.play(); var B = playerB.play(); + + // Noise: random mistakes, flip around! + if(Math.random()=2){ + return PD.CHEAT; // retaliate ONLY after two betrayals + }else{ + return PD.COOPERATE; + } + }; + self.remember = function(own, other){ + if(other==PD.CHEAT){ + howManyTimesCheated++; + }else{ + howManyTimesCheated = 0; + } + }; +} + function Logic_grim(){ var self = this; var everCheatedMe = false; @@ -81,34 +116,84 @@ function Logic_grim(){ if(everCheatedMe) return PD.CHEAT; return PD.COOPERATE; }; - self.remember = function(other){ + self.remember = function(own, other){ if(other==PD.CHEAT) everCheatedMe=true; }; } + function Logic_all_d(){ var self = this; self.play = function(){ return PD.CHEAT; }; - self.remember = function(other){ + self.remember = function(own, other){ // nah }; } + function Logic_all_c(){ var self = this; self.play = function(){ return PD.COOPERATE; }; - self.remember = function(other){ + self.remember = function(own, other){ // nah }; } -/* -function Logic_prober(){ + +function Logic_random(){ var self = this; self.play = function(){ + return (Math.random()>0.5 ? PD.COOPERATE : PD.CHEAT); }; - self.remember = function(other){ + self.remember = function(own, other){ + // nah }; } -*/ \ No newline at end of file + +// Start off Cooperating +// Then, if opponent cooperated, repeat past move. otherwise, switch. +function Logic_pavlov(){ + var self = this; + var myLastMove = PD.COOPERATE; + self.play = function(){ + return myLastMove; + }; + self.remember = function(own, other){ + myLastMove = own; // remember MISTAKEN move + if(other==PD.CHEAT) myLastMove = ((myLastMove==PD.COOPERATE) ? PD.CHEAT : PD.COOPERATE); // switch! + }; +} + +// TEST by Cooperate | Cheat | Cooperate | Cooperate +// If EVER retaliates, keep playing TFT +// If NEVER retaliates, switch to ALWAYS DEFECT +function Logic_prober(){ + + var self = this; + + var moves = [PD.COOPERATE, PD.CHEAT, PD.COOPERATE, PD.COOPERATE]; + var everCheatedMe = false; + + var otherMove = PD.COOPERATE; + self.play = function(){ + if(moves.length>0){ + // Testing phase + var move = moves.pop(); + return move; + }else{ + if(everCheatedMe){ + return otherMove; // TFT + }else{ + return PD.CHEAT; // Always Cheat + } + } + }; + self.remember = function(own, other){ + if(moves.length>0){ + if(other==PD.CHEAT) everCheatedMe=true; // Testing phase: ever retaliated? + } + otherMove = other; // for TFT + }; + +} diff --git a/js/sims/TournamentSim.js b/js/sims/Tournament.js similarity index 92% rename from js/sims/TournamentSim.js rename to js/sims/Tournament.js index a9d278d..39b23c1 100644 --- a/js/sims/TournamentSim.js +++ b/js/sims/Tournament.js @@ -1,4 +1,15 @@ -function TournamentSim(config){ +Tournament.SELECTION = 5; +Tournament.NUM_TURNS = 5; + +// CREATE A RING OF AGENTS +Tournament.AGENTS = [ + {strategy:"all_c", count:15}, + {strategy:"all_d", count:5}, + {strategy:"grim", count:0}, + {strategy:"tft", count:5}, +]; + +function Tournament(config){ var self = this; self.id = config.id; @@ -16,18 +27,10 @@ function TournamentSim(config){ self.dom.style.top = config.y+"px"; //self.dom.style.border = "1px solid rgba(0,0,0,0.2)"; - // CREATE A RING OF AGENTS - var AGENTS = [ - {strategy:"all_c", count:15}, - {strategy:"all_d", count:5}, - {strategy:"grim", count:0}, - {strategy:"tft", count:5}, - ]; - var _convertCountToArray = function(countList){ var array = []; - for(var i=0; i