initiate a game
This commit is contained in:
parent
4bdf3597c4
commit
f205ccf70e
2 changed files with 72 additions and 59 deletions
|
|
@ -1,14 +1,24 @@
|
||||||
// import "./tile.js";
|
import "./tile";
|
||||||
|
|
||||||
class Hand {
|
class Hand {
|
||||||
constructor(option = {}) {
|
constructor(tiles = []) {
|
||||||
this.tiles = option.tiles || [];
|
this.tiles = tiles;
|
||||||
this.draw = null;
|
this.drawn= null;
|
||||||
this.sort();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setDraw(t) {
|
drawTile(tile) {
|
||||||
this.draw = t;
|
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() {
|
sort() {
|
||||||
|
|
@ -20,49 +30,85 @@ class Hand {
|
||||||
}
|
}
|
||||||
|
|
||||||
flatten() {
|
flatten() {
|
||||||
if (this.draw) {
|
this.tiles.push(this.drawn);
|
||||||
this.tiles.push(this.draw);
|
this.drawn = null;
|
||||||
this.draw = null;
|
this.sort();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Wall {
|
class Draw {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.tiles = Tile.generateAll();
|
this.tiles = Tile.generateAll();
|
||||||
|
this.shuffle();
|
||||||
|
}
|
||||||
|
|
||||||
|
shuffle() {
|
||||||
for (let i = this.tiles.length - 1; i > 0; i--) {
|
for (let i = this.tiles.length - 1; i > 0; i--) {
|
||||||
const j = Math.floor(Math.random() * (i + 1));
|
const j = Math.floor(Math.random() * (i + 1));
|
||||||
[this.tiles[i], this.tiles[j]] = [this.tiles[j], this.tiles[i]];
|
[this.tiles[i], this.tiles[j]] = [this.tiles[j], this.tiles[i]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
draw() {
|
drawTile() {
|
||||||
return this.tiles.pop();
|
return this.tiles.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
drawHand() {
|
drawHand() {
|
||||||
let tiles = [];
|
const handTiles = [];
|
||||||
for (let i = 0; i < 13; i++) {
|
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 {
|
class Discard {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.tiles = [];
|
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 {
|
class Game {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.wall = new Wall();
|
this.discards = [new Discard(), new Discard(), new Discard(), new Discard()];
|
||||||
this.hands = Array.from({ length: 4 }, () => this.wall.drawHand());
|
this.draw = new Draw();
|
||||||
this.discards = Array.from({ length: 4 }, () => new Discard());
|
this.hands = [
|
||||||
this.turn = 0;
|
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">';
|
: '<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>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue