-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathround.js
More file actions
197 lines (186 loc) · 6.46 KB
/
Copy pathround.js
File metadata and controls
197 lines (186 loc) · 6.46 KB
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
import { shuffleAndDistribute } from "./deck.js";
import createTrick from "./trick.js";
export function createRound(
deck,
game,
roundNumber,
onPlayerPlayed,
onRoundStarted,
onRoundFinished,
onTrickStarted,
onTrickFinished,
onNextPlayerTurn
) {
const numberOfPlayers = 4;
const round = {
game: game,
number: roundNumber,
playersHands: shuffleAndDistribute(deck, numberOfPlayers),
tricks: [],
};
const actions = {
roundStarted: onRoundStarted,
roundFinished: function (round) {
updatePontuation();
onRoundFinished(round);
},
};
const listeners = {
onPlayerPlayed: function (playerIdx, card) {
onPlayerPlayed(playerIdx, card);
},
onTrickStarted: function (trick) {
onTrickStarted(trick);
},
onTrickFinished: function (trick) {
onTrickFinished(trick);
startNextTrick(trick.winnerIdx);
},
onNextPlayerTurn: function (turnPlayerIdx) {
onNextPlayerTurn(turnPlayerIdx);
},
};
function start() {
const starterRoundPlayerIdx = (roundNumber * 1 - 1) % 4;
actions.roundStarted(round);
startNextTrick(starterRoundPlayerIdx);
}
function startNextTrick(starterPlayerIdx) {
if (checkRoundIsFinished()) {
actions.roundFinished(round);
return;
}
const trickNumber = round.tricks.length + 1;
const trick = createTrick(
round,
trickNumber,
starterPlayerIdx,
listeners.onPlayerPlayed,
listeners.onTrickStarted,
listeners.onTrickFinished,
listeners.onNextPlayerTurn
);
round.tricks.push(trick);
trick.start();
}
function listCardsOfTricks(tricks) {
let cards = tricks.map((x) => Object.values(x.trick.cardPlays));
let cardList = cards.reduce((a, b) => a.concat(b), []);
return cardList;
}
function checkRoundIsFinished() {
const defaultRule = (x) => x.length >= 52;
const cardList = listCardsOfTricks(round.tricks);
const roundFInishedRules = {
1: defaultRule,
2: (x) => x.filter((x) => x.suit === "heart").length >= 13,
3: (x) =>
x.filter((x) => x.rank === "j" || x.rank === "k").length >= 8,
4: (x) => x.filter((x) => x.rank === "q").length >= 4,
5: defaultRule,
6: (x) =>
x.filter((x) => x.rank === "k" && x.suit === "heart").length >=
1,
//7,8,9,10 são default
};
const roundRule = roundFInishedRules[round.number] || defaultRule;
return roundRule(cardList);
}
function updatePontuation() {
const players = round.game.players;
let playersTricks = [[], [], [], []];
const roundPontuationRules = {
1: (x) => x.length * -20,
2: (x) =>
x.reduce(
(acc, curr) =>
acc + curr.filter((x) => x.suit === "heart").length,
0
) * -20,
3: (x) =>
x.reduce(
(acc, curr) =>
acc +
curr.filter((x) => x.rank === "j" || x.rank === "k").length,
0
) * -30,
4: (x) =>
x.reduce(
(acc, curr) => acc + curr.filter((x) => x.rank === "q").length,
0
) * -50,
5: (x) => x.length * -90,
6: (x) =>
x.reduce(
(acc, curr) =>
acc +
curr.filter((x) => x.rank === "k" && x.suit === "heart")
.length,
0
) * -160,
7: (x) => x.length * +20,
8: (x) => x.length * +20,
9: (x) => x.length * +20,
10: (x) => x.length * +20,
};
function fillPlayerTricks(tricks) {
for (let i = round.number === 5 ? 11 : 0; i < tricks.length; i++) {
const trick = tricks[i];
const winnerIdx = trick.checkTrickWinnerPlayerId();
playersTricks[winnerIdx].push(listCardsOfTricks([trick]));
}
}
fillPlayerTricks(round.tricks);
for (let i = 0; i < playersTricks.length; i++) {
const rule = roundPontuationRules[round.number];
const res = rule(playersTricks[i]);
players[i].points += res;
}
}
function roundFirstTrickPlayRestriction(playerHand, card) {
const noRestricition = (x) => true;
const cantPlayHeart = (x) => x.suit !== "heart";
const roundRestrictions = {
2: cantPlayHeart,
6: cantPlayHeart,
//restante não possuem restrições
};
const roundRestriction =
roundRestrictions[round.number] || noRestricition;
const res =
roundRestriction(card) || !playerHand.find(roundRestriction);
return res;
}
function playCard(playerIdx, index) {
const playerHand = round.playersHands[playerIdx];
const card = playerHand.splice(index, 1)[0];
getCurrentTrick().playCard(card);
}
function getCurrentTrick() {
const tricks = round.tricks;
return tricks[tricks.length - 1];
}
function getValidPlayOrError(playerIdx, cardIdx) {
const currentTrick = getCurrentTrick();
let playerHand = round.playersHands[playerIdx];
if (!playerHand[cardIdx]) {
const error = {
error: "index inválido",
};
return error;
}
return currentTrick.getValidPlayOrError(
playerHand,
playerHand[cardIdx],
roundFirstTrickPlayRestriction
);
}
return {
round,
updatePontuation,
start,
getValidPlayOrError,
playCard,
getCurrentTrick,
};
}