Enhance pawn movement logic to allow attackers to get another turn when beating opponent pawns; refactor beatPawns method to return count of beaten pawns
This commit is contained in:
parent
f7f8798f43
commit
99d24daff2
@ -12,12 +12,13 @@ module.exports = socket => {
|
|||||||
if (isMoveValid(req.session, pawn, room)) {
|
if (isMoveValid(req.session, pawn, room)) {
|
||||||
const newPositionOfMovedPawn = pawn.getPositionAfterMove(room.rolledNumber);
|
const newPositionOfMovedPawn = pawn.getPositionAfterMove(room.rolledNumber);
|
||||||
room.changePositionOfPawn(pawn, newPositionOfMovedPawn);
|
room.changePositionOfPawn(pawn, newPositionOfMovedPawn);
|
||||||
room.beatPawns(newPositionOfMovedPawn, req.session.color);
|
const beaten = room.beatPawns(newPositionOfMovedPawn, req.session.color);
|
||||||
// If player rolled a 6 they get another turn: keep same player but reset turn timer
|
// If pawn killed any opponent pawns, attacker gets another turn.
|
||||||
if (room.rolledNumber !== 6) {
|
// Also a roll of 6 grants another turn.
|
||||||
room.changeMovingPlayer();
|
if (beaten > 0 || room.rolledNumber === 6) {
|
||||||
} else {
|
|
||||||
room.resetTurnForSamePlayer();
|
room.resetTurnForSamePlayer();
|
||||||
|
} else {
|
||||||
|
room.changeMovingPlayer();
|
||||||
}
|
}
|
||||||
const winner = room.getWinner();
|
const winner = room.getWinner();
|
||||||
if (winner) {
|
if (winner) {
|
||||||
|
|||||||
@ -49,16 +49,20 @@ const RoomSchema = new mongoose.Schema({
|
|||||||
RoomSchema.methods.beatPawns = function (position, attackingPawnColor) {
|
RoomSchema.methods.beatPawns = function (position, attackingPawnColor) {
|
||||||
// Do not beat pawns on safe/colored positions
|
// Do not beat pawns on safe/colored positions
|
||||||
if (isSafePosition(position)) {
|
if (isSafePosition(position)) {
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
|
let beatenCount = 0;
|
||||||
const pawnsOnPosition = this.pawns.filter(pawn => pawn.position === position);
|
const pawnsOnPosition = this.pawns.filter(pawn => pawn.position === position);
|
||||||
pawnsOnPosition.forEach(pawn => {
|
pawnsOnPosition.forEach(pawn => {
|
||||||
if (pawn.color !== attackingPawnColor) {
|
if (pawn.color !== attackingPawnColor) {
|
||||||
const index = this.getPawnIndex(pawn._id);
|
const index = this.getPawnIndex(pawn._id);
|
||||||
|
if (index !== -1 && this.pawns[index].position !== this.pawns[index].basePos) {
|
||||||
this.pawns[index].position = this.pawns[index].basePos;
|
this.pawns[index].position = this.pawns[index].basePos;
|
||||||
|
beatenCount++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
return beatenCount;
|
||||||
};
|
};
|
||||||
|
|
||||||
RoomSchema.methods.changeMovingPlayer = function () {
|
RoomSchema.methods.changeMovingPlayer = function () {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user