Created
February 1, 2025 18:10
-
-
Save carefree-ladka/01659e44e559bce0fdb5e17dd8da397d to your computer and use it in GitHub Desktop.
Ludo Game Based on OOPS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Dice { | |
roll() { | |
return Math.floor(Math.random() * 6) + 1; | |
} | |
} | |
class Token { | |
constructor(color, id) { | |
this.color = color; | |
this.id = id; | |
this.position = -1; // -1 means inside home | |
this.isFinished = false; | |
} | |
move(steps, board) { | |
if (this.isFinished) return false; // Token already reached home | |
if (this.position === -1 && steps !== 6) { | |
console.log(`${this.color} Token ${this.id} needs a 6 to start.`); | |
return false; | |
} | |
let newPosition = this.position === -1 ? 0 : this.position + steps; | |
if (newPosition > board.pathLength) { | |
console.log(`${this.color} Token ${this.id} can't move beyond home path.`); | |
return false; | |
} | |
this.position = newPosition; | |
console.log(`${this.color} Token ${this.id} moved to position ${this.position}`); | |
if (this.position === board.pathLength) { | |
this.isFinished = true; | |
console.log(`${this.color} Token ${this.id} reached home! π`); | |
} | |
return true; | |
} | |
} | |
class Player { | |
constructor(color) { | |
this.color = color; | |
this.tokens = [new Token(color, 1), new Token(color, 2), new Token(color, 3), new Token(color, 4)]; | |
} | |
getMovableTokens(diceValue) { | |
return this.tokens.filter(token => token.isFinished === false && (token.position !== -1 || diceValue === 6)); | |
} | |
moveToken(tokenIndex, steps, board) { | |
const token = this.tokens[tokenIndex]; | |
return token.move(steps, board); | |
} | |
hasWon() { | |
return this.tokens.every(token => token.isFinished); | |
} | |
} | |
class Board { | |
constructor() { | |
this.pathLength = 56; // Standard Ludo path length before home | |
this.players = []; | |
this.dice = new Dice(); | |
this.turn = 0; | |
} | |
addPlayer(color) { | |
this.players.push(new Player(color)); | |
} | |
playTurn() { | |
let player = this.players[this.turn % this.players.length]; | |
let diceRoll = this.dice.roll(); | |
console.log(`π² ${player.color} rolled a ${diceRoll}`); | |
let movableTokens = player.getMovableTokens(diceRoll); | |
if (movableTokens.length === 0) { | |
console.log(`${player.color} has no valid moves.`); | |
} else { | |
player.moveToken(0, diceRoll, this); | |
} | |
if (player.hasWon()) { | |
console.log(`π ${player.color} wins the game!`); | |
return; | |
} | |
if (diceRoll !== 6) { | |
this.turn++; // Only move to next player if they didn't roll a 6 | |
} | |
} | |
start() { | |
console.log("π² Starting Ludo Game! π²"); | |
while (true) { | |
this.playTurn(); | |
if (this.players.some(player => player.hasWon())) break; | |
} | |
} | |
} | |
// Example Game | |
const game = new Board(); | |
game.addPlayer("Red"); | |
game.addPlayer("Blue"); | |
game.start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment