Skip to content

Daminos #2

Open
Open

Description

@qurbanovucal9-ops

class Domino101 {
constructor(players) {
this.players = players;
this.round = 1;
this.scoreLimit = 101;
this.currentPlayer = null;
this.winnerTeam = null;
this.gameOver = false;
}

// Daşları qarışdırmaq
shuffle(tiles) {
return [...tiles].sort(() => Math.random() - 0.5);
}

// İlk oyunda 1-1 qoşa ilə başlamaq
startFirstRound() {
const starter = this.players.find(player =>
player.tiles.some(tile =>
tile.left === 1 && tile.right === 1
)
);

if (!starter) {
  throw new Error("1-1 qoşa daşı tapılmadı");
}

this.currentPlayer = starter;
return starter;

}

// Daş atmaq
playTile(player, tile) {
if (this.currentPlayer !== player) {
throw new Error("Sənin növbən deyil");
}

const index = player.tiles.indexOf(tile);

if (index === -1) {
  throw new Error("Bu daş sənin əlində yoxdur");
}

player.tiles.splice(index, 1);

}

// Əl bitəndə xalı qalib komandaya yazmaq
finishRound(winnerTeam, losingPlayers) {
let points = 0;

losingPlayers.forEach(player => {
  player.tiles.forEach(tile => {
    points += tile.left + tile.right;
  });
});

winnerTeam.score += points;
this.winnerTeam = winnerTeam;

if (winnerTeam.score >= this.scoreLimit) {
  this.gameOver = true;
}

return {
  points,
  totalScore: winnerTeam.score,
  gameOver: this.gameOver
};

}

// Növbəti oyunda qalib komanda başlayır
startNextRound(selectedPlayer) {
if (this.gameOver) {
throw new Error("Oyun artıq bitib");
}

if (selectedPlayer.team !== this.winnerTeam) {
  throw new Error("Yalnız qalib komanda başlaya bilər");
}

this.round++;
this.currentPlayer = selectedPlayer;
return selectedPlayer;

}
}

Originally posted by @qurbanovucal9-ops in #1

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions