diff --git a/frontend/js/game.js b/frontend/js/game.js index f0d757e..f6b007f 100644 --- a/frontend/js/game.js +++ b/frontend/js/game.js @@ -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++; + } } } \ No newline at end of file diff --git a/frontend/pages/riichi/chap5.html b/frontend/pages/riichi/chap5.html index b5ce2eb..0cc8a33 100644 --- a/frontend/pages/riichi/chap5.html +++ b/frontend/pages/riichi/chap5.html @@ -180,39 +180,6 @@ : '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 = ` - - - `; - } - 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 = ` - - - `; - } - handDiv.appendChild(drawDiv); - } - } });