initiate a game
This commit is contained in:
parent
4bdf3597c4
commit
f205ccf70e
2 changed files with 72 additions and 59 deletions
|
|
@ -1,68 +1,114 @@
|
|||
// import "./tile.js";
|
||||
import "./tile";
|
||||
|
||||
class Hand {
|
||||
constructor(option = {}) {
|
||||
this.tiles = option.tiles || [];
|
||||
this.draw = null;
|
||||
this.sort();
|
||||
constructor(tiles = []) {
|
||||
this.tiles = tiles;
|
||||
this.drawn= null;
|
||||
}
|
||||
|
||||
setDraw(t) {
|
||||
this.draw = t;
|
||||
drawTile(tile) {
|
||||
this.drawn = tile;
|
||||
}
|
||||
|
||||
discard(k) {
|
||||
if (k = this.tiles.length) {
|
||||
tile = this.drawn;
|
||||
return tile;
|
||||
} else {
|
||||
tile = this.tiles[k];
|
||||
this.tiles.splice(k, 1);
|
||||
return tile;
|
||||
}
|
||||
}
|
||||
|
||||
sort() {
|
||||
this.tiles.sort((a, b) => {
|
||||
if (a.lessThan(b)) return -1;
|
||||
if (b.lessThan(a)) return 1;
|
||||
return 0;
|
||||
});
|
||||
if (a.lessThan(b)) return -1;
|
||||
if (b.lessThan(a)) return 1;
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
|
||||
flatten() {
|
||||
if (this.draw) {
|
||||
this.tiles.push(this.draw);
|
||||
this.draw = null;
|
||||
}
|
||||
this.tiles.push(this.drawn);
|
||||
this.drawn = null;
|
||||
this.sort();
|
||||
}
|
||||
}
|
||||
|
||||
class Wall {
|
||||
class Draw {
|
||||
constructor() {
|
||||
this.tiles = Tile.generateAll();
|
||||
this.shuffle();
|
||||
}
|
||||
|
||||
shuffle() {
|
||||
for (let i = this.tiles.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
[this.tiles[i], this.tiles[j]] = [this.tiles[j], this.tiles[i]];
|
||||
}
|
||||
}
|
||||
|
||||
draw() {
|
||||
drawTile() {
|
||||
return this.tiles.pop();
|
||||
}
|
||||
|
||||
drawHand() {
|
||||
let tiles = [];
|
||||
const handTiles = [];
|
||||
for (let i = 0; i < 13; i++) {
|
||||
tiles.push(this.draw());
|
||||
handTiles.push(this.drawTile());
|
||||
}
|
||||
return new Hand({ tiles });
|
||||
return new Hand(handTiles);
|
||||
}
|
||||
}
|
||||
|
||||
class Discard {
|
||||
constructor() {
|
||||
this.tiles = [];
|
||||
this.hiddenTiles = []; // for stollen tiles
|
||||
this.hiddenTiles = [];
|
||||
}
|
||||
|
||||
add(tile) {
|
||||
this.tiles.push(tile);
|
||||
}
|
||||
|
||||
beeingStollen() {
|
||||
this.hiddenTiles.push(this.tiles.pop());
|
||||
}
|
||||
|
||||
isIn(tile) {
|
||||
return this.tiles.some(t => t.equals(tile)) || this.hiddenTiles.some(t => t.equals(tile));
|
||||
}
|
||||
}
|
||||
|
||||
class Game {
|
||||
constructor() {
|
||||
this.wall = new Wall();
|
||||
this.hands = Array.from({ length: 4 }, () => this.wall.drawHand());
|
||||
this.discards = Array.from({ length: 4 }, () => new Discard());
|
||||
this.turn = 0;
|
||||
this.discards = [new Discard(), new Discard(), new Discard(), new Discard()];
|
||||
this.draw = new Draw();
|
||||
this.hands = [
|
||||
new Hand(this.draw.drawHand()),
|
||||
new Hand(this.draw.drawHand()),
|
||||
new Hand(this.draw.drawHand()),
|
||||
new Hand(this.draw.drawHand())
|
||||
];
|
||||
this.firstDealer = Math.floor(Math.random() * 4);
|
||||
this.turn = this.firstDealer;
|
||||
this.repeat = 0;
|
||||
}
|
||||
|
||||
this.hands[this.turn].setDraw(this.wall.draw());
|
||||
newDeal(hasRepeat = false) {
|
||||
this.discards = [new Discard(), new Discard(), new Discard(), new Discard()];
|
||||
this.draw = new Draw();
|
||||
this.hands = [
|
||||
new Hand(this.draw.drawHand()),
|
||||
new Hand(this.draw.drawHand()),
|
||||
new Hand(this.draw.drawHand()),
|
||||
new Hand(this.draw.drawHand())
|
||||
];
|
||||
if (!hasRepeat) {
|
||||
this.turn = (this.firstDealer + 1) % 4;
|
||||
} else {
|
||||
this.repeat++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -180,39 +180,6 @@
|
|||
: '<img src="/img/tilineau/idle.png" alt="Tilineau">';
|
||||
});
|
||||
|
||||
// Créer une nouvelle partie et afficher les mains
|
||||
const game = new Game();
|
||||
for (let i = 0; i < 4; i++) {
|
||||
const handDiv = document.getElementById(`hand-${i}`);
|
||||
handDiv.innerHTML = '';
|
||||
for (const tile of game.hands[i].tiles) {
|
||||
const tileDiv = document.createElement('div');
|
||||
if (i === 0) {
|
||||
tileDiv.innerHTML = tile.getSVG();
|
||||
} else {
|
||||
tileDiv.innerHTML = `<svg class="tile tile-back" viewBox="0 0 310 410" height="82" width="60">
|
||||
<image href="/img/Regular/Gray.svg" x="10" y="10" height="410" width="310"/>
|
||||
<image href="/img/Regular/Back.svg" x="0" y="0" height="410" width="310"/>
|
||||
</svg>`;
|
||||
}
|
||||
handDiv.appendChild(tileDiv);
|
||||
}
|
||||
// Affiche la tuile draw à droite de la main, séparée
|
||||
const drawTile = game.hands[i].draw;
|
||||
if (drawTile) {
|
||||
const drawDiv = document.createElement('div');
|
||||
drawDiv.className = 'draw-tile';
|
||||
if (i === 0) {
|
||||
drawDiv.innerHTML = drawTile.getSVG();
|
||||
} else {
|
||||
drawDiv.innerHTML = `<svg class="tile tile-back" viewBox="0 0 310 410" height="82" width="60">
|
||||
<image href="/img/Regular/Gray.svg" x="10" y="10" height="410" width="310"/>
|
||||
<image href="/img/Regular/Back.svg" x="0" y="0" height="410" width="310"/>
|
||||
</svg>`;
|
||||
}
|
||||
handDiv.appendChild(drawDiv);
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
|
|
|||
Loading…
Reference in a new issue