-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplain.js
77 lines (65 loc) · 1.81 KB
/
plain.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
69
70
71
72
73
74
75
76
77
function PlainModel() {
var self = this;
Model.call(self);
self.title("Blocky Plain");
self.reverse = ko.observable(false);
self.checkSucceeded = function() {
var valid = true;
var rev = self.reverse();
for (var i=1; i<self.array().length; i++) {
if ((!rev & self.array()[i-1] > self.array()[i]) ||
(rev & self.array()[i-1] < self.array()[i]) ) {
valid = false;
break;
}
};
self.succeeded(valid);
}
self.newProblem = function() {
self.seed(Math.ceil(Math.random() * 1000));
self.reset();
}
self.sortedProblem = function() {
self.seed(Math.ceil(Math.random() * 20) - 20);
self.reset();
}
self.invSortedProblem = function() {
self.seed(Math.ceil(Math.random() * 80) - 100);
self.reset();
}
self.reset = function() {
var seed = self.seed();
self.interpreter = null;
self.succeeded(false);
self.steps(0);
self.array.removeAll();
if (seed < 0) {
var plus = (seed > -20) ? 1.0 : -1.0;
var rng = new MersenneTwister(Math.abs(seed));
var ar = [];
var last = 0;
var ma = -1E6;
var mi = 1E6;
for (var i=0; i<self.n(); i++) {
last += rng.random() * 50 * plus;
if (last>ma) ma = last;
if (last<mi) mi = last;
ar.push(last);
}
range = rng.random() * 40 + 50;
for (var x in ar) {
self.array.push( Math.floor((ar[x]-mi) / (ma-mi) * range));
}
} else {
var rng = new MersenneTwister(seed);
for (var i=0; i<self.n(); i++) {
self.array.push(Math.round(Math.ceil(rng.random() * 90) + 10))
}
}
}
self.newProblem();
}
PlainModel.prototype = Object.create(Model.prototype);
PlainModel.prototype.init = function() {
Object.getPrototypeOf(SortModel.prototype).init.call(this);
}