diff --git a/frontend/js/game.js b/frontend/js/game.js index 7163256..e69de29 100644 --- a/frontend/js/game.js +++ b/frontend/js/game.js @@ -1,135 +0,0 @@ -// frontend/js/game.js -// Adaptation frontend de la logique de game.py en JavaScript - -async function think(t) { - return new Promise(resolve => setTimeout(resolve, 1000 * t)); -} - -class Wall { - 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() { - return this.tiles.shift(); - } - drawHand() { - const handTiles = this.tiles.splice(0, 13); - return new Hand(handTiles); - } -} - -class Hand { - constructor(tiles) { - this.tiles = tiles; - this.draw = null; - } - drawTile(tile) { - this.draw = tile; - } - discardTile(tileId) { - let idx = this.tiles.findIndex(t => t.id === tileId); - if (idx !== -1) { - return this.tiles.splice(idx, 1)[0]; - } - if (this.draw && this.draw.id === tileId) { - const discarded = this.draw; - this.draw = null; - return discarded; - } - return null; - } - flatten() { - if (this.draw) { - this.tiles.push(this.draw); - this.draw = null; - } - } -} - -class Game { - constructor(onUpdate) { - this.wall = new Wall(); - this.handPlayer = this.wall.drawHand(); - this.discardPlayer = []; - this.handsBots = [this.wall.drawHand(), this.wall.drawHand(), this.wall.drawHand()]; - this.discardsBots = [[], [], []]; - this.turn = 0; - this.hasDraw = true; - this.end = false; - this.onUpdate = onUpdate || (() => {}); - // Est pioche une tuile - const tile = this.wall.draw(); - if (tile) this.handPlayer.drawTile(tile); - this.onUpdate(this); - } - - drawTile(hand) { - const tile = this.wall.draw(); - if (tile) hand.drawTile(tile); - } - - putTile(hand, tileId) { - const discarded = hand.discardTile(tileId); - if (discarded) { - this.wall.tiles.push(discarded); - this.wall.shuffle(); - } - } - - async nextTurn() { - this.turn = (this.turn + 1) % 4; - this.hasDraw = false; - this.onUpdate(this); - if (this.turn !== 0) { - await this.botTurn(); - } else { - this.drawTile(this.handPlayer); - this.hasDraw = true; - this.onUpdate(this); - } - } - - async botTurn() { - const bot = this.handsBots[this.turn - 1]; - this.drawTile(bot); - this.onUpdate(this); // Affiche la pioche - await think(2); - // Défausse une tuile aléatoire - const idx = Math.floor(Math.random() * bot.tiles.length); - const tileToDiscard = bot.tiles[idx]; - const discarded = bot.discardTile(tileToDiscard.id); - bot.flatten() - this.onUpdate(this); // Affiche la défausse - await think(0.5); - if (discarded) this.discardsBots[this.turn - 1].push(discarded); - await this.nextTurn(); - } - - async discardTile(tileId) { - if (this.turn === 0 && this.hasDraw) { - const wasDraw = (this.handPlayer.draw && this.handPlayer.draw.id === tileId); - const discarded = this.handPlayer.discardTile(tileId); - // Si la tuile défaussée n'était pas draw, on intègre draw à la main avant le rendu - if (!wasDraw) { - this.handPlayer.flatten(); - } - this.onUpdate(this); // Affiche la défausse - if (discarded) this.discardPlayer.push(discarded); - await this.nextTurn(); - return discarded; - } - return null; - } -} - -// Expose dans le scope global -window.Game = Game; -window.Hand = Hand; -window.Wall = Wall; diff --git a/frontend/pages/riichi/chap5.html b/frontend/pages/riichi/chap5.html index 606db22..fc809c6 100644 --- a/frontend/pages/riichi/chap5.html +++ b/frontend/pages/riichi/chap5.html @@ -133,6 +133,7 @@
'
: '
';
});
-
- // Logique de jeu frontend
- let game;
- function renderPlayerHand(gameInstance) {
- const handDiv = document.getElementById('hand-0');
- handDiv.innerHTML = '';
- let handTiles = gameInstance.handPlayer.tiles.slice();
- sortHand(handTiles);
- handTiles.forEach((tile, idx) => {
- const tileDiv = document.createElement('div');
- tileDiv.className = 'hand-tile-item';
- tileDiv.innerHTML = tile.getSVG();
- tileDiv.dataset.tileId = tile.id;
- tileDiv.style.cursor = 'pointer';
- tileDiv.addEventListener('click', () => discardTile(tile.id));
- handDiv.appendChild(tileDiv);
- });
- // Pioche
- if (gameInstance.handPlayer.draw) {
- const drawTileDiv = document.createElement('div');
- drawTileDiv.className = 'hand-tile-item draw-tile';
- drawTileDiv.innerHTML = gameInstance.handPlayer.draw.getSVG();
- drawTileDiv.dataset.tileId = gameInstance.handPlayer.draw.id;
- drawTileDiv.style.cursor = 'pointer';
- drawTileDiv.addEventListener('click', () => discardTile(gameInstance.handPlayer.draw.id));
- handDiv.appendChild(drawTileDiv);
- }
- // Afficher les mains des bots
- for (let bot = 0; bot < 3; bot++) {
- const botDiv = document.getElementById('hand-' + (bot + 1));
- botDiv.innerHTML = '';
- let botTiles = gameInstance.handsBots[bot].tiles.slice();
- botTiles.forEach(() => {
- const tileDiv = document.createElement('div');
- tileDiv.innerHTML = '';
- botDiv.appendChild(tileDiv);
- });
- if (gameInstance.handsBots[bot].draw) {
- const drawTileDiv = document.createElement('div');
- drawTileDiv.innerHTML = '';
- botDiv.appendChild(drawTileDiv);
- }
- }
- }
-
- game = new window.Game(renderPlayerHand);
-
- function sortHand(hand) {
- const familyOrder = { 'man': 0, 'pin': 1, 'sou': 2, 'wind': 3, 'dragon': 4 };
- return hand.sort((a, b) => {
- const famA = familyOrder[a.family] ?? 99;
- const famB = familyOrder[b.family] ?? 99;
- if (famA !== famB) return famA - famB;
- if (a.value !== undefined && b.value !== undefined) return a.value - b.value;
- if (a.wind !== undefined && b.wind !== undefined) return a.wind - b.wind;
- if (a.dragon !== undefined && b.dragon !== undefined) return a.dragon - b.dragon;
- return 0;
- });
- }
-
- function renderPlayerHand() {
- const handDiv = document.getElementById('hand-0');
- handDiv.innerHTML = '';
- let handTiles = game.handPlayer.tiles.slice();
- sortHand(handTiles);
- handTiles.forEach((tile, idx) => {
- const tileDiv = document.createElement('div');
- tileDiv.className = 'hand-tile-item';
- tileDiv.innerHTML = tile.getSVG();
- tileDiv.dataset.tileId = tile.id;
- tileDiv.style.cursor = 'pointer';
- tileDiv.addEventListener('click', () => discardTile(tile.id));
- handDiv.appendChild(tileDiv);
- });
- // Pioche
- if (game.handPlayer.draw) {
- const drawTileDiv = document.createElement('div');
- drawTileDiv.className = 'hand-tile-item draw-tile';
- drawTileDiv.innerHTML = game.handPlayer.draw.getSVG();
- drawTileDiv.dataset.tileId = game.handPlayer.draw.id;
- drawTileDiv.style.cursor = 'pointer';
- drawTileDiv.addEventListener('click', () => discardTile(game.handPlayer.draw.id));
- handDiv.appendChild(drawTileDiv);
- }
- // Afficher les mains des bots
- for (let bot = 0; bot < 3; bot++) {
- const botDiv = document.getElementById('hand-' + (bot + 1));
- botDiv.innerHTML = '';
- let botTiles = game.handsBots[bot].tiles.slice();
- // Affiche le dos des tuiles
- botTiles.forEach(() => {
- const tileDiv = document.createElement('div');
- tileDiv.innerHTML = '';
- botDiv.appendChild(tileDiv);
- });
- // Pioche du bot (si présente)
- if (game.handsBots[bot].draw) {
- const drawTileDiv = document.createElement('div');
- drawTileDiv.innerHTML = '';
- botDiv.appendChild(drawTileDiv);
- }
- }
- }
- function renderPlayerHand(gameInstance) {
- const handDiv = document.getElementById('hand-0');
- handDiv.innerHTML = '';
- let handTiles = gameInstance.handPlayer.tiles.slice();
- sortHand(handTiles);
- handTiles.forEach((tile, idx) => {
- const tileDiv = document.createElement('div');
- tileDiv.className = 'hand-tile-item';
- tileDiv.innerHTML = tile.getSVG();
- tileDiv.dataset.tileId = tile.id;
- tileDiv.style.cursor = 'pointer';
- tileDiv.addEventListener('click', () => discardTile(tile.id));
- handDiv.appendChild(tileDiv);
- });
- // Pioche
- if (gameInstance.handPlayer.draw) {
- const drawTileDiv = document.createElement('div');
- drawTileDiv.className = 'hand-tile-item draw-tile';
- drawTileDiv.innerHTML = gameInstance.handPlayer.draw.getSVG();
- drawTileDiv.dataset.tileId = gameInstance.handPlayer.draw.id;
- drawTileDiv.style.cursor = 'pointer';
- drawTileDiv.addEventListener('click', () => discardTile(gameInstance.handPlayer.draw.id));
- handDiv.appendChild(drawTileDiv);
- }
- // Afficher les mains des bots
- for (let bot = 0; bot < 3; bot++) {
- const botDiv = document.getElementById('hand-' + (bot + 1));
- botDiv.innerHTML = '';
- let botTiles = gameInstance.handsBots[bot].tiles.slice();
- botTiles.forEach(() => {
- const tileDiv = document.createElement('div');
- tileDiv.innerHTML = '';
- botDiv.appendChild(tileDiv);
- });
- if (gameInstance.handsBots[bot].draw) {
- const drawTileDiv = document.createElement('div');
- drawTileDiv.innerHTML = '';
- botDiv.appendChild(drawTileDiv);
- }
- }
- }
-
- async function discardTile(tileId) {
- await game.discardTile(tileId);
- }
-
- // renderPlayerHand(game); // Removed direct call to renderPlayerHand with game
});
+