64 lines
No EOL
1.3 KiB
JavaScript
64 lines
No EOL
1.3 KiB
JavaScript
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];
|
|
}
|
|
}
|
|
}
|
|
} |