main strategies
This commit is contained in:
parent
63cf1b6792
commit
f372fcc6cd
2 changed files with 64 additions and 0 deletions
0
index.html
Normal file
0
index.html
Normal file
64
main.js
Normal file
64
main.js
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
const ALLC = function allwaysCooperate(foeHistory) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ALLD = function allwaysDefect(foeHistory) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const RAND = function randomChoice(foeHistory) {
|
||||||
|
if (Math.random() > 0.5) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const TFT = function titForThat(foeHistory) {
|
||||||
|
if (foeHistory.length === 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return foeHistory[foeHistory.length - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
const GRIM = function grimTrigger(foeHistory) {
|
||||||
|
if (foeHistory.some(e => e === false)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const MAJ = function majority(foeHistory) {
|
||||||
|
let nbTrue = 0;
|
||||||
|
let nbFalse = 0;
|
||||||
|
|
||||||
|
for (const valeur of foeHistory) {
|
||||||
|
if (valeur) {
|
||||||
|
nbTrue++;
|
||||||
|
} else {
|
||||||
|
nbFalse++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (nbTrue < nbFalse) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Party {
|
||||||
|
constructor(length) {
|
||||||
|
this.length = length;
|
||||||
|
|
||||||
|
this.functions = [ALLC, ALLD, RAND, TFT, GRIM, MAJ];
|
||||||
|
|
||||||
|
this.grid = [];
|
||||||
|
|
||||||
|
for (let i = 0; i < length; i++) {
|
||||||
|
this.grid[i] = [];
|
||||||
|
|
||||||
|
for (let j = 0; j < length; j++) {
|
||||||
|
const index = Math.floor(Math.random() * this.functions.length);
|
||||||
|
this.grid[i][j] = this.functions[index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue