Created
April 11, 2022 17:28
-
-
Save iqbalfasri/061fce6fe4fa78e4c1279bd983754b02 to your computer and use it in GitHub Desktop.
This file contains 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 { | |
topSideval; | |
getTopSideVal() { | |
return this.topSideval | |
} | |
roll() { | |
this.topSideval = Math.floor(Math.random() * 6) + 1 | |
return this; | |
} | |
setTopSideVal(topSideVal) { | |
this.topSideval = topSideVal | |
return this; | |
} | |
} | |
class Player { | |
diceInCup = []; | |
name; | |
position; | |
point; | |
getDiceInCup() { | |
return this.diceInCup | |
} | |
getName() { | |
return this.name | |
} | |
getPosition() { | |
return this.position | |
} | |
constructor(numberOfDice, position, name = "") { | |
this.point = 0; | |
this.position = position | |
this.name = name; | |
for (let i = 0; i < numberOfDice; i++) { | |
this.diceInCup.push(new Dice()) | |
} | |
} | |
addPoint(point) { | |
this.point += point | |
} | |
getPoint() { | |
return this.point | |
} | |
play() { | |
for (const dice of this.diceInCup) { | |
dice.roll(); | |
} | |
} | |
removeDice(key) { | |
this.diceInCup.splice(key, 1) | |
} | |
insertDice(dice) { | |
this.diceInCup.push(dice); | |
} | |
} | |
class Game { | |
players = []; | |
round; | |
numberOfPlayer; | |
numberOfDicePerPlayer; | |
removeWhenDiceTop = 6; | |
moveWhenDiceTop = 1; | |
constructor(numberOfPlayer, numberOfDicePerPlayer) { | |
this.round = 0; | |
this.numberOfPlayer = numberOfPlayer; | |
this.numberOfDicePerPlayer = numberOfDicePerPlayer | |
for (let i = 0; i < numberOfPlayer; i++) { | |
this.players[i] = new Player(this.numberOfPlayer, i, Math.random().toString(36).substr(2, 5) | |
) | |
} | |
} | |
displayRound() { | |
console.log(`Giliran -> ${this.round}`); | |
return this; | |
} | |
displayTopSideDice(title = "Lempar Dadu") { | |
console.log(`${title}`); | |
for (const [, player] of this.players.entries()) { | |
console.log(`${player.getName()}`); | |
let diceTopSide = "" | |
for (const dice of player.diceInCup) { | |
diceTopSide = dice.getTopSideVal().toString() | |
} | |
} | |
return this; | |
} | |
displayWinner(player) { | |
console.log("Pemenang") | |
console.log(`Pemain ${player.getName()}`); | |
return this; | |
} | |
start() { | |
console.log(`Pemain = ${this.numberOfPlayer}, Dadu = ${this.numberOfDicePerPlayer}`); | |
while (true) { | |
this.round++; | |
let diceCarryForward = [] | |
for (const player of this.players) { | |
player.play() | |
} | |
this.displayRound() | |
for (const [index, player] of this.players.entries()) { | |
let tmpDiceArray = [] | |
for (const [diceIndex, dice] of player.getDiceInCup().entries()) { | |
if (dice.getTopSideVal() === this.removeWhenDiceTop) { | |
player.addPoint(1) | |
player.removeDice(diceIndex) | |
} | |
if (dice.getTopSideVal() === this.moveWhenDiceTop) { | |
if (player.getPosition() === (this.numberOfPlayer - 1)) { | |
this.players[0].insertDice(dice) | |
player.removeDice(diceIndex) | |
} else { | |
tmpDiceArray.push(dice) | |
player.removeDice(diceIndex) | |
} | |
} | |
} | |
diceCarryForward[index + 1] = tmpDiceArray | |
if (typeof diceCarryForward[index] !== 'undefined') { | |
for (const [, dice] of diceCarryForward[index].entries()) { | |
player.insertDice(dice) | |
} | |
diceCarryForward = [] | |
} | |
} | |
this.displayTopSideDice("Setelah Evaluasi"); | |
let playerHasDice = this.numberOfPlayer | |
for (const [, player] of this.players.entries()) { | |
if (player.getDiceInCup().length <= 0) { | |
playerHasDice-- | |
} | |
} | |
if (playerHasDice === 1) { | |
this.displayWinner(this.getWinner()) | |
break | |
} | |
} | |
} | |
getWinner() { | |
let winner = null; | |
let highScore = 0; | |
for (const [, player] of this.players.entries()) { | |
if (player.getPoint() > highScore) { | |
highScore = player.getPoint() | |
winner = player | |
} | |
} | |
return winner | |
} | |
} | |
const game = new Game(3, 4); | |
game.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment