-
Notifications
You must be signed in to change notification settings - Fork 0
/
ss-dateonly-penalty-double.js
148 lines (125 loc) · 4.1 KB
/
ss-dateonly-penalty-double.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
144
145
146
147
148
(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() * 4) + 3, 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) {
for (var i = 0; i < 2; i++) {
var func = [Math.max, Math.min][i];
var targetHeroes = heroine.filterHeroesByScore(heroes, func);
_.each(targetHeroes, function (targetHero) {
targetHero.star += (i == 0 ? 1 : -1) * heroine.value;
});
}
});
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) {
if (isHoliday) {
this.realScore[heroIndex] += 2;
} else {
this.realScore[heroIndex] += 1;
this.revealedScore[heroIndex] += 1;
}
};
Heroine.prototype.filterHeroesByScore = function (heroes, func) {
var targetScore = func.apply(null, this.realScore);
var targetHeroes = [];
_.each(heroes, function (hero) {
if (this.realScore[hero.index] === targetScore) {
targetHeroes.push(hero);
}
}, this);
return targetHeroes;
};
return Heroine;
})();
return new Game();
})();