-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoPlayer.js
68 lines (62 loc) · 1.67 KB
/
autoPlayer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//Implement naive strategy for playing the solitare game
(function(){
var _ = require("underscore");
function naivePick(choices, currentCardNum, buckets, discardedPile)
{
//Always return choice 1
return choices[0];
}
function pickClosestNumber(choices, currentCardNum, buckets, discardedPile)
{
console.log("Card number " + currentCardNum);
console.log("Choices " + choices);
//Take the abs difference of choices - currentCardNum, get the one with minimum difference.
var closestIndex;
var currentDistance;
_.each(choices, function (val, index)
{
var dis = Math.abs(currentCardNum - val);
if (currentDistance == null || dis < currentDistance)
{
closestIndex = val;
currentDistance = dis;
}
});
console.log("Picked index " + closestIndex + " dis " + currentDistance);
return closestIndex;
}
function pickClosesMod(choices, currentCardNum, buckets, discardedPile)
{
return pickClosestNumber(choices, Math.floor((currentCardNum-1) * 10 / 13), buckets, discardedPile);
}
function pickRandom(choices, currentCardNum, buckets, discardedPile)
{
return _.sample(choices);
}
function headTail(choices, currentCardNum, buckets, discardedPile)
{
if (currentCardNum == 1 && choices[0] == 0)
{
return choices[0];
}
else if (currentCardNum == 13 && choices[choices.length-1] == 10)
{
return choices[choices.length-1];
}
else if (buckets.indexOf(currentCardNum+1) != -1)
{
return choices[choices.length-1];
}
else if (buckets.indexOf(currentCardNum-1) != -1)
{
return choices[0];
}
else
{
return pickClosesMod(choices, currentCardNum, buckets, discardedPile);
}
}
module.exports = {
pick:headTail
}
}());