114 lines
No EOL
2.6 KiB
JavaScript
114 lines
No EOL
2.6 KiB
JavaScript
import "./tile";
|
|
|
|
class Hand {
|
|
constructor(tiles = []) {
|
|
this.tiles = tiles;
|
|
this.drawn= null;
|
|
}
|
|
|
|
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;
|
|
});
|
|
}
|
|
|
|
flatten() {
|
|
this.tiles.push(this.drawn);
|
|
this.drawn = null;
|
|
this.sort();
|
|
}
|
|
}
|
|
|
|
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]];
|
|
}
|
|
}
|
|
|
|
drawTile() {
|
|
return this.tiles.pop();
|
|
}
|
|
|
|
drawHand() {
|
|
const handTiles = [];
|
|
for (let i = 0; i < 13; i++) {
|
|
handTiles.push(this.drawTile());
|
|
}
|
|
return new Hand(handTiles);
|
|
}
|
|
}
|
|
|
|
class Discard {
|
|
constructor() {
|
|
this.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.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;
|
|
}
|
|
|
|
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++;
|
|
}
|
|
}
|
|
} |