-
Notifications
You must be signed in to change notification settings - Fork 0
/
red7.js
268 lines (232 loc) · 6 KB
/
red7.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
'use strict'
function Game(num_players, removed_cards, score_0, score_1) {
this.num_players = num_players;
this.hands = new Array(num_players);
this.palette = new Array(num_players);
this.current_rule = 7;
this.duration = ""
//this.log_string = "";
var whole = []
for (var i = 1; i <= 7; i++) {
for (var j = 1; j <= 7; j++) {
var c = i*10 + j;
if (removed_cards.indexOf(c) >= 0) {
continue;
}
whole.push(c);
}
}
this.duration += score_0 + " " + score_1 + " \n";
this.duration += whole.length + " ";
for (var x of whole) this.duration += x + " ";
this.duration += "\n";
for (var i = 0; i < whole.length; i++) {
var x = Math.floor(Math.random() * (i+1));
[whole[x], whole[i]] = [whole[i], whole[x]];
}
for (var player = 0; player < this.num_players; player++) {
this.hands[player] = [];
this.palette[player] = [whole.pop()];
for (var i = 0; i < 7; i++) {
this.hands[player].push(whole.pop());
}
}
//console.log(whole);
//console.log(this.hands);
//console.log(this.palette);
}
function max_element(a){
//console.log(a);
var tmp = a[0];
for(var x of a){
//console.log("x = " + x);
if(x>tmp) tmp=x;
}
return tmp;
}
function compare(a,b){
if (a.length===0) return false;
else if (a.length>b.length) return true;
else if (a.length<b.length) return false;
else return (max_element(a) > max_element(b));
}
function red(a){
// Highest
var r=[];
r.push(max_element(a));
return r;
}
function orange(a){
// same number
var n =[[],[],[],[],[],[],[],[]];
for(var x of a){
n[Math.floor(x/10)].push(x);
}
var best=n[1];
for(var i=2;i<8;i++){
if(compare(n[i],best)) best=n[i];
}
return best;
}
function yellow(a){
// same color
var n =[[],[],[],[],[],[],[],[]];
for(var x of a){
n[x%10].push(x);
}
var best=n[1];
for(var i=2;i<8;i++){
if(compare(n[i],best)) best=n[i];
}
return best;
}
function green(a){
// even number
var r=[];
for(var x of a){
if(Math.floor(x/10)%2 === 0) r.push(x);
}
return r;
}
function blue(a){
// different color
var m_color =Array(8).fill(0);
for(var x of a){
if(x>m_color[x%10]) m_color[x%10]=x;
}
var r = [];
for (var x of m_color) {
if (x > 0) r.push(x);
}
return r;
}
function indigo(a) {
// in a row
var m_number = Array(8).fill(0);
for (var x of a) {
if (x > m_number[Math.floor(x/10)]) m_number[Math.floor(x/10)] = x;
}
var best = [], current = [];
for (var i = 1; i < 8; i++) {
if (m_number[i] === 0) current = [];
else {
current.push(m_number[i]);
if (compare(current, best)) best = current;
}
}
return best;
}
function violet(a) {
// below 4
var r = [];
for (var x of a) {
if (Math.floor(x/10) < 4) r.push(x);
}
return r;
}
var _get_top = {
7: red,
6: orange,
5: yellow,
4: green,
3: blue,
2: indigo,
1: violet
};
function _get_winner(rule){
// palette
if (rule === 0) rule = this.current_rule;
var best_hand = [];
var win = -1;
for (var i = 0; i < this.num_players; i++) {
var cur = _get_top[rule](this.palette[i]);
if (compare(cur, best_hand)) {
best_hand = cur;
win = i;
}
}
return win;
}
function _get_player_highest(player, rule) {
if (rule === 0) rule = this.current_rule;
return _get_top[rule](this.palette[player]);
}
function _try_play(player, card, rule_card) {
var hands = [];
for (var x of this.hands[player]) hands.push(x);
if (card > 0) {
var cid = hands.indexOf(card);
if (cid < 0) return false;
hands.splice(cid, 1);
}
if (rule_card > 0) {
var cid = hands.indexOf(rule_card);
if (cid < 0) return false;
hands.splice(cid, 1);
}
var winning = false;
if (card > 0) this.palette[player].push(card);
winning = this.get_winner(rule_card % 10) === player;
if (card > 0) this.palette[player].pop();
return winning;
}
function _play(player, card, rule_card) {
if (card > 0) {
var cid = this.hands[player].indexOf(card);
if (cid < 0) {
console.log("Card no in player " + player + "'s hand");
card = 0;
} else {
this.palette[player].push(card);
this.hands[player].splice(cid, 1);
}
}
if (rule_card > 0) {
var cid = this.hands[player].indexOf(rule_card);
if (cid < 0) {
console.log("Rule card not in player " + player + "'s hand");
rule_card = 0;
} else {
this.hands[player].splice(cid, 1);
}
}
var current_winning = false;
if (card > 0 || rule_card > 0) {
var rule = rule_card % 10;
if (this.get_winner(rule) === player) {
if (rule_card > 0) {
this.current_rule = rule;
}
current_winning = true;
}
}
this.duration += player + " " + rule_card + " " + card + "\n";
return current_winning;
}
function _gen_input(player) {
var s = "";
s += this.num_players + " " + player + "\n";
s += this.current_rule + "\n";
s += this.hands[player].length + " ";
for (var x of this.hands[player]) s += x + " ";
s += "\n"
for (var i = 0; i < this.num_players; i++) {
s += this.hands[i].length + "\n";
s += this.palette[i].length + " ";
for (var x of this.palette[i]) s += x + " ";
s += "\n";
}
s += this.duration;
return s;
}
Game.prototype = {
constructor: Game,
get_winner: _get_winner,
get_player_highest: _get_player_highest,
try_play: _try_play,
play: _play,
gen_input: _gen_input,
get_top: _get_top
}
//Game(2, [11,12,13,14,15,16,17])
module.exports = Game;