diff --git a/frontend/js/game.js b/frontend/js/game.js index f6b007f..9ab8eeb 100644 --- a/frontend/js/game.js +++ b/frontend/js/game.js @@ -1,8 +1,17 @@ -import "./tile"; +import "./tile.js"; + +class Group { + constructor(tiles, stollenTile = null, dir = null) { + this.tiles = tiles; + this.stollenTile = stollenTile; + this.dir = dir; // 1 for left, 2 for middle, 3 for right + } +} class Hand { constructor(tiles = []) { this.tiles = tiles; + this.groups = []; this.drawn= null; } @@ -34,6 +43,18 @@ class Hand { this.drawn = null; this.sort(); } + + makeGroup(tiles, stollenTile = null, dir = null) { + const group = new Group(tiles, stollenTile, dir); + this.groups.push(group); + for (let tile of tiles) { + const index = this.tiles.findIndex(t => t.equals(tile)); + if (index !== -1) { + this.tiles.splice(index, 1); + } + } + this.flatten(); + } } class Draw { @@ -74,6 +95,7 @@ class Discard { beeingStollen() { this.hiddenTiles.push(this.tiles.pop()); + return this.hiddenTiles[this.hiddenTiles.length - 1]; } isIn(tile) { @@ -83,27 +105,42 @@ class Discard { class Game { constructor() { - 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.discards = [ + new Discard(), + new Discard(), + new Discard(), + new Discard() ]; - this.firstDealer = Math.floor(Math.random() * 4); + this.wall = new Draw(); + this.hands = [ + this.wall.drawHand(), + this.wall.drawHand(), + this.wall.drawHand(), + this.wall.drawHand() + ]; + for (let hand of this.hands) { + hand.sort(); + } + this.firstDealer = Math.floor(Math.random() * 4) * 0; this.turn = this.firstDealer; this.repeat = 0; + + this.draw(this.turn); } newDeal(hasRepeat = false) { - this.discards = [new Discard(), new Discard(), new Discard(), new Discard()]; - this.draw = new Draw(); + this.discards = [ + new Discard(), + new Discard(), + new Discard(), + new Discard() + ]; + this.wall = 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.wall.drawHand(), + this.wall.drawHand(), + this.wall.drawHand(), + this.wall.drawHand() ]; if (!hasRepeat) { this.turn = (this.firstDealer + 1) % 4; @@ -111,4 +148,35 @@ class Game { this.repeat++; } } -} \ No newline at end of file + + nextTurn() { + this.turn = (this.turn + 1) % 4; + } + + draw(player) { + this.hands[player].drawTile(this.wall.drawTile()); + } + + discard(player, k) { + const tile = this.hands[player].discard(k); + this.discards[player].add(tile); + this.hands[player].flatten(); + } + + chii(player, tiles, dir) { + const hand = this.hands[player]; + dir = (player - this.turn) % 4; + console.assert(dir == 1); + this.hands[player].makeGroup(tiles, this.discards[player].beingStollen(), dir); + } + + pon(player, tiles, dir) { + const hand = this.hands[player]; + dir = (player - this.turn) % 4; + console.assert(dir == 1); + this.hands[player].makeGroup(tiles, this.discards[player].beingStollen(), dir); + } +} + +export { Discard, Draw, Game, Group, Hand }; + diff --git a/frontend/pages/riichi/chap5.html b/frontend/pages/riichi/chap5.html index 0cc8a33..fd71f4e 100644 --- a/frontend/pages/riichi/chap5.html +++ b/frontend/pages/riichi/chap5.html @@ -131,7 +131,7 @@ padding: 20px 0 0 20px; } - #hand-0 div:hover { + .player-tile:hover { transform: translateY(-15px); transition: transform 0.2s; z-index: 2; @@ -158,8 +158,9 @@ - -