mirror of
https://github.com/wassname/cardsforscience.git
synced 2026-08-02 12:30:06 +08:00
Updated tests to simulate rules
This commit is contained in:
+20
-2
@@ -91,13 +91,15 @@ var Game = module.exports =(function (Helpers, GameObjects, ObjectStorage,Rules,
|
||||
// deal new initial cards that follow the rule
|
||||
this.lastCards.splice(0,this.lastCards.length);
|
||||
this.lastCards.push(_.sample(this.cards));
|
||||
for (var i = 0; i < 52; i++) {
|
||||
var error,i;
|
||||
for (i = 0; i < 52; i++) {
|
||||
if (this.lastCards.length>2) break; // stop here
|
||||
var card = _.sample(this.cards);
|
||||
var res;
|
||||
try{
|
||||
res = this.rule.test(card,this.lastCards,this.cards);
|
||||
} catch(e){
|
||||
error=e;
|
||||
// in case of an error just add a random card
|
||||
// this is probobly because it is looking back 2 or 3 cards
|
||||
// yet we only have 1
|
||||
@@ -105,7 +107,23 @@ var Game = module.exports =(function (Helpers, GameObjects, ObjectStorage,Rules,
|
||||
}
|
||||
if (res) this.lastCards.push(_.sample(this.cards));
|
||||
}
|
||||
if (this.lastCards.length<3) console.error('Could not deal cards for rule',this.rule.key,this.rule.options);
|
||||
if (this.lastCards.length<3) {
|
||||
console.warn(
|
||||
'Could not deal cards for rule after:',
|
||||
i,
|
||||
this.rule.key,
|
||||
this.rule.options,
|
||||
this.rule.describe(),
|
||||
_.map(this.lastCards,'key'),
|
||||
error?error.message:''
|
||||
);
|
||||
// feck, just deal 3 random then
|
||||
this.lastCards.splice(0,this.lastCards.length);
|
||||
this.lastCards.push(_.sample(this.cards));
|
||||
this.lastCards.push(_.sample(this.cards));
|
||||
this.lastCards.push(_.sample(this.cards));
|
||||
|
||||
}
|
||||
// this.lastCards.push.apply(this.lastCards,_.sampleSize(this.cards,3));
|
||||
|
||||
this.ruleInfo.splice(0,this.ruleInfo.length);
|
||||
|
||||
+27
-7
@@ -15,6 +15,21 @@ if (!chai){
|
||||
*/
|
||||
var Rules = module.exports = (function functionName(_,chai) {
|
||||
|
||||
var Simulation = function(rules,cards){
|
||||
this.results=[];
|
||||
this.cards=cards;
|
||||
this.rules=rules;
|
||||
};
|
||||
Simulation.prototype.run = function () {
|
||||
var allPromises = [];
|
||||
for (var i = 0; i < this.rules.length; i++) {
|
||||
var rule = this.rules[i];
|
||||
var promises = rule.simulate();
|
||||
allPromises.push(promises);
|
||||
}
|
||||
return Promise.all(allPromises).then(function(data){return _.concat(data);});
|
||||
};
|
||||
|
||||
|
||||
/** Generate all combination of arguments from array using functional programming instead of recursive
|
||||
* @param {Object} opts - An array or arrays with keys describing options [['Ben','Jade','Darren'],['Smith','Miller']]
|
||||
@@ -306,21 +321,24 @@ var Rules = module.exports = (function functionName(_,chai) {
|
||||
$.extend(this.state, state);
|
||||
};
|
||||
Rule.prototype.simulateOne = function (options,cards,n) {
|
||||
var self = this;
|
||||
// return new Promise(function(resolve, reject) {
|
||||
if (!n) n=52*2;
|
||||
var t0=new Date();
|
||||
var rights=[];
|
||||
var wrongs=[];
|
||||
var errors=[];
|
||||
var lastCards = _.sampleSize(cards,3);
|
||||
this.setOptions(options);
|
||||
self.setOptions(options);
|
||||
for (var i = 0; i < n; i++) {
|
||||
var res=undefined;
|
||||
var error=undefined;
|
||||
var card = _.sample(cards);
|
||||
|
||||
try{
|
||||
var res = this.testAndTell(card,lastCards,cards);
|
||||
var res = self.testAndTell(card,lastCards,cards);
|
||||
} catch(e){
|
||||
console.error('simulateOne',err);
|
||||
error=e;
|
||||
}
|
||||
|
||||
@@ -339,6 +357,7 @@ var Rules = module.exports = (function functionName(_,chai) {
|
||||
}
|
||||
}
|
||||
var ratioRight = rights.length/(rights.length+wrongs.length);
|
||||
if (isNaN(ratioRight)) ratioRight = 0;
|
||||
var ok = ratioRight>0.1&&ratioRight<0.66;
|
||||
return {
|
||||
wrongs:wrongs,
|
||||
@@ -347,9 +366,9 @@ var Rules = module.exports = (function functionName(_,chai) {
|
||||
rights:rights,
|
||||
error:errors.length,
|
||||
errors:errors,
|
||||
key:this.key,
|
||||
description:this.describe(),
|
||||
options:this.options,
|
||||
key:self.key,
|
||||
description:self.describe(),
|
||||
options:self.options,
|
||||
n:n,
|
||||
time:new Date()-t0,
|
||||
ok:ok,
|
||||
@@ -360,8 +379,8 @@ var Rules = module.exports = (function functionName(_,chai) {
|
||||
Rule.prototype.simulate = function (cards) {
|
||||
var results = [];
|
||||
for (var i = 0; i < this.optionsPossible.length; i++) {
|
||||
var res = this.simulateOne(this.optionsPossible[i],cards);
|
||||
results.push(res);
|
||||
var result = this.simulateOne(this.optionsPossible[i],cards);
|
||||
results.push(result);
|
||||
}
|
||||
return results;
|
||||
};
|
||||
@@ -656,6 +675,7 @@ var Rules = module.exports = (function functionName(_,chai) {
|
||||
return {
|
||||
Rule: Rule,
|
||||
rules: rules,
|
||||
Simulation: Simulation,
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user