-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
353 lines (302 loc) · 9.1 KB
/
main.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
console.log("欢迎来到吹牛逼小游戏!");
const table = {
cards: [],
};
class sexyDealer {
leftAmount = 0;
userList = [];
lastPlayerId = null;
nextId = 1;
isTrue = true;
passAmount = 0;
constructor(playerAmount) {
this.leftAmount = playerAmount;
this.generateCards();
this.shuffle();
}
generateCards() {
const textArr = ['A', 'B', 'C', 'D'];
const END = 13;
for (let j = 0; j < textArr.length; j+=1) {
for (let i = 1; i <= END; i+=1) {
table.cards.push(`${textArr[j]}${i}`);
}
}
table.cards.push("X1");
table.cards.push("X2");
};
shuffle() {
let randomEnd = table.cards.length;
while (randomEnd > 0) {
const index = parseInt(Math.random()*randomEnd, 10);
const temp = table.cards[randomEnd-1];
table.cards[randomEnd-1] = table.cards[index];
table.cards[index] = temp;
randomEnd -= 1;
}
}
generateNextId(pid) {
let status = true;
let i = 0;
while (status && i < this.userList.length) {
if (pid === this.userList[i].id) {
status = false;
if (i+1 >= this.userList.length) {
this.nextId = this.userList[0].id;
} else {
this.nextId = this.userList[i+1].id;
}
} else {
i += 1;
}
}
}
showCards = () => console.log(`荷官手中的余牌: `, table.cards);
addUser = (player) => {
this.userList.push(player);
};
canIPlay = (pid) => {
if (pid === this.nextId) {
return true;
} else {
return false;
}
};
handlePlay = (pid, fact, statement) => {
const count = fact.length;
let userName = "";
let status = false;
for (let i = 0; i < this.userList.length; i+=1) {
if (pid === this.userList[i].id) {
status = true;
userName = this.userList[i].name;
}
}
this.isTrue = true;
if (status) {
this.lastPlayerId = pid;
while (fact.length > 0) {
const temp = fact.pop();
table.cards.push(temp);
if (temp !== "X1"
&& temp !== "X2"
&& temp.replace(/\w/, "") !== statement) {
this.isTrue = false;
}
}
this.notify(`${userName}: ${count}张${digitToWord(statement)}`);
this.generateNextId(pid);
} else {
console.error(`查无此人: ${pid},不许出牌`);
}
};
handleChallenge = (pid) => {
let status = false;
for (let i = 0; i < this.userList.length; i+=1) {
if (this.userList[i].id === pid &&
pid !== this.lastPlayerId) {
status = true;
}
}
if (status && this.lastPlayerId !== null) {
let id;
if (this.isTrue) {
id = pid;
} else {
id = this.lastPlayerId;
}
for (let i = 0; i < this.userList.length; i+=1) {
if (this.userList[i].id === id) {
this.notify(`本轮,${this.userList[i].name}负,桌上的牌全归他!`);
while (table.cards.length > 0) {
this.userList[i].cards.push(table.cards.pop());
}
}
}
this.lastPlayerId = null;
this.generateNextId(pid);
} else {
console.error("质疑者身份非法,质疑无效");
}
};
handlePass = (pid) => {
let status = false;
let name = "";
for (let i = 0; i < this.userList.length; i+=1) {
if (this.userList[i].id === pid &&
pid !== this.lastPlayerId) {
status = true;
name = this.userList[i].name;
}
}
if (status) {
this.passAmount = this.passAmount + 1;
console.log(`${name}选择Pass`);
if (this.passAmount >= this.userList.length-1) {
this.notify("所有用户均选择Pass,桌上的牌将清空");
while (table.cards.length > 0) {
table.cards.pop();
}
}
this.generateNextId(pid);
} else {
console.error("不允许当前用户Pass");
}
};
deal = () => {
let onePlayerAmount = Math.ceil(table.cards.length / this.leftAmount);
while (table.cards.length > 0 &&
table.cards.length < onePlayerAmount) {
onePlayerAmount -= 1;
}
const tempCards = [];
for (let i = 0; i < onePlayerAmount; i += 1) {
tempCards.push(table.cards.pop());
}
this.leftAmount = this.leftAmount -1;
return tempCards;
};
}
function getDigit(str) {
return parseInt(str.replace(/\w/, ""), 10);
}
function getCardType(str) {
return str.slice(0, 1);
}
function digitToWord(digit) {
switch (digit) {
case "1":
return "A";
case "11":
return "J";
case "12":
return "Q";
case "13":
return "K";
default:
return digit;
}
}
function wordToPic(str) {
const type = getCardType(str);
const digit = getDigit(str);
let picStr = "";
switch (type) {
case 'A':
picStr = "♥";
break;
case 'B':
picStr = "♠";
break;
case 'C':
picStr = "♦";
break;
case 'D':
picStr = "♣";
break;
}
if (type === "X") {
if (digit === 1) {
picStr = "☆";
} else {
picStr = "★";
}
} else {
picStr += digitToWord(`${digit}`);
}
return picStr;
}
class Player {
id = 0;
name = "";
cards = [];
cardsTemp = [];
constructor(id, name) {
this.id = id;
this.name = name;
}
handleCardClick = (index) => {
const cards = document.querySelectorAll(".card");
if (cards[index].getAttribute("selected") === "true") {
const card = this.cards[index];
let i = 0;
let status = true;
while (status && i < this.cardsTemp.length) {
if (card === this.cardsTemp[i]) {
status = false;
this.cardsTemp.splice(i, 1);
}
i += 1;
}
cards[index].setAttribute("selected", "false");
cards[index].setAttribute("class", "card");
}
else {
this.cardsTemp.push(this.cards[index]);
cards[index].setAttribute("selected", "true");
cards[index].setAttribute("class", "card selected");
}
const statementSelector = document.querySelector("#statementSelector");
if (this.cardsTemp.length > 0) {
statementSelector.style.display = "block";
} else {
statementSelector.style.display = "none";
}
};
showInfo = () => {
const userInfo = document.querySelector("#userInfo");
userInfo.innerText = `编号: ${this.id} 用户名: ${this.name}`;
};
showCards = () => {
const myCardStage = document.querySelector('#myCards');
myCardStage.innerHTML = "";
for (let i = 0; i < this.cards.length; i++) {
const card = document.createElement('div');
card.innerText = wordToPic(this.cards[i]);
card.setAttribute("class", "card");
card.setAttribute("selected", "false");
card.onclick = () => this.handleCardClick(i);
let color = "";
switch (getCardType(this.cards[i])) {
case 'A':
case 'C':
color = "#f00";
break;
case 'B':
case 'D':
color = "#000";
break;
case 'X':
color = "#5347ff";
break;
}
card.style.color = color;
myCardStage.appendChild(card);
}
};
getCards = (cards) => {
this.cards = cards;
this.cards.sort((a, b) => getDigit(a)-getDigit(b));
};
play = () => {
let status = false;
let cards = [];
for (let j = 0; j < this.cardsTemp.length; j+=1) {
let i = 0;
while (i < this.cards.length) {
if (this.cardsTemp[j] === this.cards[i]) {
status = true;
cards.push(this.cards.splice(i, 1)[0]);
}
i += 1;
}
}
if (status) {
this.showCards();
while (this.cardsTemp.length > 0) {
this.cardsTemp.pop();
}
}
return cards;
};
}