From 1831a7ac91102b872ab0f7241d883bedd88ee923 Mon Sep 17 00:00:00 2001 From: Didictateur Date: Mon, 24 Mar 2025 23:10:20 +0100 Subject: [PATCH] Everyone go ponya ! --- src/game.ts | 42 +++++++++++++++++++++++++++------------ src/group.ts | 56 +++++++++++++++++++++++++++++++--------------------- src/hand.ts | 6 +++--- 3 files changed, 65 insertions(+), 39 deletions(-) diff --git a/src/game.ts b/src/game.ts index 4759e9b..f20e757 100644 --- a/src/game.ts +++ b/src/game.ts @@ -122,7 +122,10 @@ export class Game { } else { // nothing unusual if (this.turn === 0 && this.selectedTile !== undefined) { this.discard(0, this.selectedTile as NonNullable); - this.turn++; + this.checkPon(); + console.log("turn", this.turn, "\n"); + this.turn = (this.turn + 1) % 4; + console.log("new turn", this.turn, "\n"); } } } @@ -157,6 +160,7 @@ export class Game { if ( this.turn !== 0 ) { // bot playing + console.log(this.turn, '\n'); if (!this.hasPicked) { // begin of his turn this.lastPlayed = Date.now(); this.pick(this.turn); @@ -167,6 +171,7 @@ export class Game { let n = Math.floor(this.hands[this.turn].length() * Math.random()); this.discard(this.turn, n); this.hasPlayed = true; + this.checkPon(); this.canCall = this.canDoAChii().length > 0 || this.canDoAPon(); } } else if (!this.canCall) { // end of his turn @@ -232,28 +237,39 @@ export class Game { console.log("Chii !\n"); } - private canDoAPon(): boolean { + private checkPon(): void { + for (var p = 1; p < 4; p++) { + if (this.canDoAPon(p)) { + console.log(p, '\n'); + this.pon(this.lastDiscard as NonNullable, p); + break; + } + } + } + + private canDoAPon(player: number = 0): boolean { if ( this.lastDiscard !== undefined && - this.lastDiscard !== 0 && - this.turn !== 0 + this.lastDiscard !== player && + this.turn !== player ) { let t = this.discards[this.lastDiscard][this.discards[this.lastDiscard].length-1]; - return this.hands[0].count(t.getFamily(), t.getValue()) >= 2; + return this.hands[player].count(t.getFamily(), t.getValue()) >= 2; } else { return false; } } - private pon(p: number): void { + private pon(p: number, thief: number = 0): void { + console.log(thief, "stole", p, '\n'); let t = this.discards[p].pop() as NonNullable; this.lastDiscard = undefined; - let t2 = this.hands[0].find(t.getFamily(), t.getValue()) as NonNullable; - let t3 = this.hands[0].find(t.getFamily(), t.getValue()) as NonNullable; + let t2 = this.hands[thief].find(t.getFamily(), t.getValue()) as NonNullable; + let t3 = this.hands[thief].find(t.getFamily(), t.getValue()) as NonNullable; [t, t2, t3].forEach(t => t.setTilt()); - this.groups[0].push(new Group([t, t2, t3], p)); + this.groups[thief].push(new Group([t, t2, t3], p, thief)); - this.turn = 0; + this.turn = thief; this.hasPicked = true; this.hasPlayed = false; } @@ -310,7 +326,7 @@ export class Game { 5 * this.sizeHiddenHand, this.sizeHiddenHand, undefined, - true, + false, - pi / 2 ); this.hands[2].drawHand( @@ -320,7 +336,7 @@ export class Game { 5 * this.sizeHiddenHand, this.sizeHiddenHand, undefined, - true, + false, - pi ); this.hands[3].drawHand( @@ -330,7 +346,7 @@ export class Game { 5 * this.sizeHiddenHand, this.sizeHiddenHand, undefined, - true, + false, pi / 2 ); diff --git a/src/group.ts b/src/group.ts index d761375..d0df868 100644 --- a/src/group.ts +++ b/src/group.ts @@ -2,11 +2,17 @@ import { Tile } from "./tile" export class Group { private tiles: Array; - private stolenFrom: number|undefined; + private stolenFrom: number; + private belongsTo: number - public constructor(tiles: Array = [], stolenFrom: number|undefined = undefined) { + public constructor( + tiles: Array, + stolenFrom: number, + belongsTo: number + ) { this.tiles = tiles; this.stolenFrom = stolenFrom; + this.belongsTo = belongsTo; } public push(tile: Tile): void { @@ -29,21 +35,23 @@ export class Group { size: number, rotation: number, ): void { + ctx.save(); + ctx.translate(525, 525); + ctx.rotate(rotation); + ctx.translate(-525, -525); + + console.log(x, y, '\n'); + + rotation = 0; let v = 75 * size; let w = 90 * size; - let vx = Math.cos(rotation); - let vy = Math.sin(rotation); - let osx = Math.sin(rotation) * 25 * size / 2; - let osy = Math.cos(rotation) * 25 * size / 2; - if (!this.stolenFrom) { - //TODO error - } - let p = 3 - (this.stolenFrom as NonNullable); + let osy = 25 * size / 2; + let p = (this.belongsTo - this.stolenFrom - 1 + 4) % 4; if (p === 0) { this.tiles[0].drawTile( ctx, - x + osx, + x, y + osy, size, false, @@ -52,8 +60,8 @@ export class Group { ); this.tiles[1].drawTile( ctx, - x + vx * w, - y + vy * w, + x + w, + y, size, false, 0, @@ -61,8 +69,8 @@ export class Group { ); this.tiles[2].drawTile( ctx, - x + vx * (w + v + os * size), - y + vy * (w + v + os * size), + x + w + v + os * size, + y, size, false, 0, @@ -81,8 +89,8 @@ export class Group { ); this.tiles[1].drawTile( ctx, - x + vx * w + osx, - y + vy * w + osy, + x + w, + y + osy, size, false, 0 - 3.141592 / 2, @@ -90,8 +98,8 @@ export class Group { ); this.tiles[2].drawTile( ctx, - x + vx * (w + v + 3 *os * size), - y + vy * (w + v + 3 *os * size), + x + w + v + 3 *os * size, + y, size, false, 0, @@ -110,8 +118,8 @@ export class Group { ); this.tiles[1].drawTile( ctx, - x + vx * (v + os * size), - y + vy * (v + os * size), + x + v + os * size, + y, size, false, 0, @@ -119,8 +127,8 @@ export class Group { ); this.tiles[2].drawTile( ctx, - x + vx * (w + v + os * size) + osx, - y + vy * (w + v + os * size) + osy, + x + w + v + os * size, + y + osy, size, false, 0 - 3.141592 / 2, @@ -130,5 +138,7 @@ export class Group { } else { //TODO error } + + ctx.restore(); } } diff --git a/src/hand.ts b/src/hand.ts index 115c235..e47c40a 100644 --- a/src/hand.ts +++ b/src/hand.ts @@ -97,7 +97,7 @@ export class Hand { if (groups !== undefined) { this.tiles.push(t1); this.sort(); - groups.push(new Group([t1, t2])); + groups.push(new Group([t1, t2], 0, 0)); return groups; } } @@ -109,7 +109,7 @@ export class Hand { this.tiles.push(t3); this.sort(); if (groups !== undefined) { - groups.push(new Group([t1, t2, t3])); + groups.push(new Group([t1, t2, t3], 0, 0)); this.tiles.push(t1); this.sort(); return groups; @@ -126,7 +126,7 @@ export class Hand { this.tiles.push(t3); this.sort(); if (groups !== undefined) { - groups.push(new Group([t3, t2, t1])); + groups.push(new Group([t3, t2, t1], 0, 0)); this.tiles.push(t1); this.sort(); return groups;