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)) {
|
||||
const newPositionOfMovedPawn = pawn.getPositionAfterMove(room.rolledNumber);
|
||||
room.changePositionOfPawn(pawn, newPositionOfMovedPawn);
|
||||
room.beatPawns(newPositionOfMovedPawn, req.session.color);
|
||||
// If player rolled a 6 they get another turn: keep same player but reset turn timer
|
||||
if (room.rolledNumber !== 6) {
|
||||
room.changeMovingPlayer();
|
||||
} else {
|
||||
const beaten = room.beatPawns(newPositionOfMovedPawn, req.session.color);
|
||||
// If pawn killed any opponent pawns, attacker gets another turn.
|
||||
// Also a roll of 6 grants another turn.
|
||||
if (beaten > 0 || room.rolledNumber === 6) {
|
||||
room.resetTurnForSamePlayer();
|
||||
} else {
|
||||
room.changeMovingPlayer();
|
||||
}
|
||||
const winner = room.getWinner();
|
||||
if (winner) {
|
||||
|
||||
@ -49,16 +49,20 @@ const RoomSchema = new mongoose.Schema({
|
||||
RoomSchema.methods.beatPawns = function (position, attackingPawnColor) {
|
||||
// Do not beat pawns on safe/colored positions
|
||||
if (isSafePosition(position)) {
|
||||
return;
|
||||
return 0;
|
||||
}
|
||||
|
||||
let beatenCount = 0;
|
||||
const pawnsOnPosition = this.pawns.filter(pawn => pawn.position === position);
|
||||
pawnsOnPosition.forEach(pawn => {
|
||||
if (pawn.color !== attackingPawnColor) {
|
||||
const index = this.getPawnIndex(pawn._id);
|
||||
this.pawns[index].position = this.pawns[index].basePos;
|
||||
if (index !== -1 && this.pawns[index].position !== this.pawns[index].basePos) {
|
||||
this.pawns[index].position = this.pawns[index].basePos;
|
||||
beatenCount++;
|
||||
}
|
||||
}
|
||||
});
|
||||
return beatenCount;
|
||||
};
|
||||
|
||||
RoomSchema.methods.changeMovingPlayer = function () {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user