Correctly implement end of game

This commit is contained in:
Anthony Wang 2021-05-08 21:06:42 -05:00
parent 8cf4626e99
commit e5c942c941
Signed by: a
GPG key ID: 6FD3502572299774
2 changed files with 31 additions and 17 deletions

View file

@ -67,19 +67,17 @@ export default class Game {
}
const startingPlayer = this.players[0]; // Pick a random starting player instead??
this.playerTurn = this.players.indexOf(startingPlayer);
this.playersFinished = this.room.clients.length;
// Run the game
while (true) {
// Check if game ended
// Rewrite
const playersLeft: Player[] = [];
this.players.forEach((p: Player) => {
if (!p.rank && !p.disconnected)
playersLeft.push(p);
});
if (playersLeft.length < 2) {
if (playersLeft.length === 1)
playersLeft[0].rank = ++this.playersFinished; // rank is reversed, fix later
break;
if (!p.rank && !p.disconnected) {
if (p.cards.length === 0) p.rank = this.playersFinished--;
else playersLeft.push(p);
}
});
if (playersLeft.length === 1) break;
await this.round();
}
this.broadcastGameState();

View file

@ -18,6 +18,20 @@ interface GameState {
const rankStrs = ['', 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
const suitChars = ['♣', '♦', '♥', '♠'];
const rules = String.raw`Welcome to BSX!\n
<br/>
There are only 5 simple rules!<br />
1. You will first be dealt 5 cards.
2. At the beginning of each round, you must rearrange the order of your cards and place them face down in a stack.
3. During the round, players go around in a circle, claiming increasingly greater numbers. If it is you turn to claim a number, you must either claim a larger number than the previously claimed number or call BS. If you call BS, the previous player must flip over their claimed number of black (clubs or spades) cards from the tops of everyone's stacks.
4. If the previous player manages to flip over their claimed number of black cards, you must choose a card from your stack to give up. Otherwise, the previous player must choose one of their cards to give up.
5. If you give up all your cards, you lose! Last player remaining wins!`
function useForceUpdate(){
const [value, setValue] = useState(0);
return () => setValue(value => value + 1);
@ -71,6 +85,7 @@ export default function Game() {
if (!socket) return null;
if (!loggedIn) {
return (
<>
<LoginForm
socket={socket}
finish={(s) => {
@ -79,6 +94,7 @@ export default function Game() {
}}
username={username}
/>
</>
);
}
if (!room) {