diff --git a/index.html b/index.html index 0a9cd71..329fe6d 100644 --- a/index.html +++ b/index.html @@ -4,64 +4,89 @@ Strategy Grid @@ -69,87 +94,177 @@

Strategy Grid

- + +
-
-
+
+
+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
diff --git a/main.js b/main.js index c61fc4c..5c0d061 100644 --- a/main.js +++ b/main.js @@ -20,6 +20,13 @@ const TFT = function titForThat(foeHistory) { return foeHistory[foeHistory.length - 1]; } +const STFT = function suspiciousTitForTat(foeHistory) { + if (foeHistory.length === 0) { + return false; + } + return foeHistory[foeHistory.length - 1]; +} + const GRIM = function grimTrigger(foeHistory) { if (foeHistory.some(e => e === false)) { return false; @@ -45,29 +52,46 @@ const MAJ = function majority(foeHistory) { } class Party { - constructor(length) { + constructor(length, functions) { this.length = length; - this.functions = [ALLC, ALLD, RAND, TFT, GRIM, MAJ]; + // if a list of active strategies is provided, use it, + // otherwise fall back to all seven by default + this.functions = (functions && functions.length > 0) + ? functions + : [ALLC, ALLD, RAND, TFT, STFT, GRIM, MAJ]; this.grid = []; - for (let i = 0; i < length; i++) { + for (let i = 0; i < this.length; i++) { this.grid[i] = []; - for (let j = 0; j < length; j++) { + for (let j = 0; j < this.length; j++) { const index = Math.floor(Math.random() * this.functions.length); this.grid[i][j] = this.functions[index]; } } } - step() { - const T = 5; - const R = 3; - const P = 1; - const S = 0; + reset(functions) { + // if a new list of active strategies is passed, update it; + // otherwise keep the current selection + if (functions && functions.length > 0) { + this.functions = functions; + } + this.grid = []; + for (let i = 0; i < this.length; i++) { + this.grid[i] = []; + + for (let j = 0; j < this.length; j++) { + const index = Math.floor(Math.random() * this.functions.length); + this.grid[i][j] = this.functions[index]; + } + } + } + + step(T = 5, R = 3, P = 1, S = 0) { const nbRound = 50; let scores = []; @@ -130,7 +154,7 @@ class Party { } } - // changing strarts + // changing strats for (let i = 0; i < this.length; i++) { for (let j = 0; j < this.length; j++) { let bestScore = scores[i][j];