-
Notifications
You must be signed in to change notification settings - Fork 0
/
ss-dateonly-ver1.js
143 lines (120 loc) · 3.91 KB
/
ss-dateonly-ver1.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
(function () {
var Game = (function () {
function Game() {
this.heroines = [];
this.turn = 1;
}
Game.prototype.initialize = function (numheroes) {
this.numheroes = numheroes;
this.populateHeroines(numheroes * 2.5);
};
Game.prototype.populateHeroines = function (numHeroines) {
this.heroines = [];
for (var i = 0; i < numHeroines; i++) {
this.heroines.push(new Heroine(Math.floor(Math.random() * 6) + 1, this.numheroes));
}
};
Game.prototype.isHoliday = function () {
return this.turn % 2 === 0;
};
Game.prototype.processTurn = function (moves) {
for (var i = 0; i < (this.isHoliday() ? 2 : 5) ; i++) {
for (var heroIndex = 0; heroIndex < this.numheroes; heroIndex++) {
var targetHeroineIndex = parseInt(moves[heroIndex][i]);
if (!(targetHeroineIndex >= 0 && targetHeroineIndex < this.heroines.length)) {
targetHeroineIndex = 0;
}
this.heroines[targetHeroineIndex].date(heroIndex, this.isHoliday());
}
}
this.turn += 1;
};
Game.prototype.isFinished = function () {
return this.turn === 11;
};
Game.prototype.getRanking = function () {
var heroes = [];
for (var index = 0; index < this.numheroes; index++) {
heroes.push(new Hero(index));
}
_.each(this.heroines, function (heroine) {
var bestheroes = heroine.getBestheroes(heroes);
_.each(bestheroes, function (bestHero) {
bestHero.star += heroine.value / bestheroes.length;
});
});
var text = "Game Is Over!\n";
text += this.getScoreText(true);
var rankedheroes = heroes.slice(0).sort(Hero.compareTo).reverse();
for (var rank = 0; rank < rankedheroes.length; rank++) {
var hero = rankedheroes[rank];
text += (rank + 1) + ": Player " + hero.index + ", " + hero.star + " pts.\n";
}
return text;
};
Game.prototype.getStatus = function () {
return _.map(_.range(this.numheroes), function(i) {
var status = "";
status += "Turn " + (this.turn) + "\n";
status += (this.isHoliday() ? "Holiday" : "Weekday") + "\n";
status += this.getScoreText(false, i);
return status;
}, this);
};
Game.prototype.getScoreText = function (useRealScore, playerIndex) {
var text = "";
for (var i = 0; i < this.heroines.length; i++) {
var heroine = this.heroines[i];
text += "Heroine " + i + ": " + heroine.value + ","
for (var j = 0; j < this.numheroes; j++) {
text += " " + (useRealScore ? heroine.realScore[j] : heroine.revealedScore[j]);
if (j === playerIndex) {
text += " (" + heroine.realScore[j] + ")";
}
}
text += "\n";
}
return text;
};
return Game;
})();
var Hero = (function () {
function Hero(index) {
this.index = index;
this.star = 0;
}
Hero.compareTo = function (self, other) {
return self.star > other.star ? 1 : -1;
};
return Hero;
})();
var Heroine = (function () {
function Heroine(value, numheroes) {
this.value = value;
this.revealedScore = [];
this.realScore = [];
for (var i = 0; i < numheroes; i++) {
this.revealedScore.push(0);
this.realScore.push(0);
}
}
Heroine.prototype.date = function (heroIndex, isHoliday) {
this.realScore[heroIndex] += 1;
if (!isHoliday) {
this.revealedScore[heroIndex] += 1;
}
};
Heroine.prototype.getBestheroes = function (heroes) {
var maxScore = Math.max.apply(null, this.realScore);
var bestheroes = [];
_.each(heroes, function (hero) {
if (this.realScore[hero.index] === maxScore) {
bestheroes.push(hero);
}
}, this);
return bestheroes;
};
return Heroine;
})();
return new Game();
})();