diff --git a/build/dp4.js b/build/dp4.js index cd0f8c4..e5a6dbb 100644 --- a/build/dp4.js +++ b/build/dp4.js @@ -46,7 +46,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpac \*********************/ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { -eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ Game: () => (/* binding */ Game)\n/* harmony export */ });\n/* harmony import */ var _deck__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./deck */ \"./src/deck.ts\");\n/* harmony import */ var _group__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./group */ \"./src/group.ts\");\n/* harmony import */ var _button__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./button */ \"./src/button.ts\");\nvar __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nvar __generator = (undefined && undefined.__generator) || function (thisArg, body) {\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === \"function\" ? Iterator : Object).prototype);\n return g.next = verb(0), g[\"throw\"] = verb(1), g[\"return\"] = verb(2), typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\n function verb(n) { return function (v) { return step([n, v]); }; }\n function step(op) {\n if (f) throw new TypeError(\"Generator is already executing.\");\n while (g && (g = 0, op[0] && (_ = 0)), _) try {\n if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\n if (y = 0, t) op = [op[0] & 2, t.value];\n switch (op[0]) {\n case 0: case 1: t = op; break;\n case 4: _.label++; return { value: op[1], done: false };\n case 5: _.label++; y = op[1]; op = [0]; continue;\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\n default:\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\n if (t[2]) _.ops.pop();\n _.trys.pop(); continue;\n }\n op = body.call(thisArg, _);\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\n }\n};\n\n\n\nvar Game = /** @class */ (function () {\n function Game(ctx, cv, staticCtx, staticCv, red) {\n if (red === void 0) { red = false; }\n // game values\n this.turn = 0;\n this.selectedTile = undefined;\n this.canCall = false;\n this.hasPicked = false;\n this.hasPlayed = false;\n this.lastPlayed = Date.now();\n // display parameter\n this.BG_RECT = { color: \"#007730\", x: 0, y: 0, w: 1050, h: 1050 };\n this.sizeHand = 0.7;\n this.sizeHiddenHand = 0.6;\n this.sizeDiscard = 0.6;\n this.ctx = ctx;\n this.cv = cv;\n this.staticCtx = staticCtx;\n this.staticCv = staticCv;\n this.deck = new _deck__WEBPACK_IMPORTED_MODULE_0__.Deck(red);\n this.hands = [\n this.deck.getRandomHand(),\n this.deck.getRandomHand(),\n this.deck.getRandomHand(),\n this.deck.getRandomHand()\n ];\n this.discards = [[], [], [], []];\n this.lastDiscard = undefined;\n this.groups = [[], [], [], []];\n this.hands[0].sort();\n this.pick(0);\n }\n Game.prototype.draw = function (mp) {\n // background\n this.staticCtx.clearRect(0, 0, this.cv.width, this.cv.height);\n this.staticCtx.fillStyle = this.BG_RECT.color;\n this.staticCtx.fillRect(this.BG_RECT.x, this.BG_RECT.y, this.BG_RECT.w, this.BG_RECT.h);\n this.getSelected(mp);\n this.drawGame();\n this.ctx.clearRect(0, 0, this.cv.width, this.cv.height);\n this.ctx.drawImage(this.staticCv, 0, 0);\n };\n Game.prototype.getDeck = function () {\n return this.deck;\n };\n Game.prototype.getHands = function () {\n return this.hands;\n };\n Game.prototype.click = function (mp) {\n var action = (0,_button__WEBPACK_IMPORTED_MODULE_2__.clickAction)(mp.x, mp.y, this.canDoAChii().length > 0, this.canDoAPon(), false, false, false);\n if (this.canCall && action !== -1) { // can call\n if (action === 0) { // pass\n this.canCall = false;\n if (this.turn === 3) {\n this.turn = 0;\n this.pick(0);\n }\n else {\n this.turn++;\n }\n this.hasPicked = false;\n this.hasPlayed = false;\n }\n else if (action == 2) { // pon\n this.pon(this.turn);\n }\n }\n else { // nothing unusual\n if (this.turn === 0 && this.selectedTile !== undefined) {\n this.discard(0, this.selectedTile);\n this.turn++;\n }\n }\n };\n Game.prototype.getSelected = function (mp) {\n var rect = this.cv.getBoundingClientRect();\n var x = 2.5 * 75 * 0.75;\n var y = 1050 - 250 * 0.6;\n var mouseX = mp.x - rect.left - x;\n var mouseY = mp.y - rect.top;\n var s = 83.9;\n var q = Math.floor(mouseX / (s * this.sizeHand));\n var r = mouseX - q * s * this.sizeHand;\n if (r <= (s - 3) * this.sizeHand &&\n q >= 0 &&\n q < this.hands[0].length() &&\n mouseY >= y &&\n mouseY <= y + 100 * this.sizeHand) {\n this.selectedTile = q;\n }\n else {\n this.selectedTile = undefined;\n }\n };\n ;\n Game.prototype.play = function () {\n if (this.turn !== 0) { // bot playing\n if (!this.hasPicked) { // begin of his turn\n this.pick(this.turn);\n this.hasPicked = true;\n }\n else if (!this.hasPlayed) { // middle of his turn\n if (Date.now() - this.lastPlayed > 700) {\n this.lastPlayed = Date.now();\n var n = Math.floor(this.hands[this.turn].length() * Math.random());\n this.discard(this.turn, n);\n this.hasPlayed = true;\n this.canCall = this.canDoAPon();\n }\n }\n else if (!this.canCall) { // end of his turn\n if (this.turn === 3) {\n this.turn = 0;\n this.pick(0);\n }\n else {\n this.turn++;\n }\n this.hasPicked = false;\n this.hasPlayed = false;\n }\n }\n };\n Game.prototype.pick = function (player) {\n this.hands[player].push(this.deck.pop());\n this.hands[player].isolate = true;\n };\n Game.prototype.discard = function (player, n) {\n var tile = this.hands[player].eject(n);\n this.hands[player].sort();\n tile.setTilt();\n this.discards[player].push(tile);\n this.hands[player].isolate = false;\n this.hands[player].sort();\n this.lastDiscard = player;\n this.lastPlayed = Date.now();\n };\n Game.prototype.canDoAPon = function () {\n if (this.lastDiscard !== undefined && this.lastDiscard !== 0) {\n var t = this.discards[this.lastDiscard][this.discards[this.lastDiscard].length - 1];\n return this.hands[0].count(t.getFamily(), t.getValue()) >= 2;\n }\n else {\n return false;\n }\n };\n Game.prototype.pon = function (p) {\n var t = this.discards[p].pop();\n this.lastDiscard = undefined;\n var t2 = this.hands[0].find(t.getFamily(), t.getValue());\n var t3 = this.hands[0].find(t.getFamily(), t.getValue());\n [t, t2, t3].forEach(function (t) { return t.setTilt(); });\n this.groups[0].push(new _group__WEBPACK_IMPORTED_MODULE_1__.Group([t, t2, t3], p));\n this.turn = 0;\n this.hasPicked = true;\n this.hasPlayed = false;\n };\n Game.prototype.canDoAChii = function () {\n var chii = [];\n if (this.lastDiscard !== undefined &&\n this.lastDiscard === 3 &&\n this.discards[this.lastDiscard][this.discards[this.lastDiscard].length - 1].getFamily() < 4) {\n var t = this.discards[this.lastDiscard][this.discards[this.lastDiscard].length - 1];\n var h = this.hands[0];\n if (h.count(t.getFamily(), t.getValue() - 2) > 0 &&\n h.count(t.getFamily(), t.getValue() - 1) > 0) {\n chii.push(t.getValue() - 2);\n }\n else if (h.count(t.getFamily(), t.getValue() - 1) > 0 &&\n h.count(t.getFamily(), t.getValue() + 1) > 0) {\n chii.push(t.getValue() - 1);\n }\n else if (h.count(t.getFamily(), t.getValue() + 1) > 0 &&\n h.count(t.getFamily(), t.getValue() + 2) > 0) {\n chii.push(t.getValue());\n }\n }\n return chii;\n };\n Game.prototype.chii = function (minValue) {\n console.log(\"Chii !\\n\");\n };\n Game.prototype.drawGame = function () {\n // update game\n this.play();\n // hands\n this.drawHands();\n // groups\n this.drawGroups(0.6);\n // discards\n for (var i = 0; i < 4; i++) {\n this.drawDiscard(i, this.hands[0].get(this.selectedTile));\n }\n // called\n (0,_button__WEBPACK_IMPORTED_MODULE_2__.drawButtons)(this.staticCtx, this.canDoAChii().length > 0, this.canDoAPon(), false, false, false);\n };\n Game.prototype.drawHands = function () {\n var pi = 3.141592;\n this.hands[0].drawHand(this.staticCtx, 2.5 * 75 * 0.75, 1000 - 150 * this.sizeHand, 5 * this.sizeHand, 0.75, this.selectedTile, false, 0);\n this.hands[1].drawHand(this.staticCtx, 1000 - 150 * this.sizeHiddenHand, 1000 - 75 * 5 * this.sizeHiddenHand, 5 * this.sizeHiddenHand, this.sizeHiddenHand, undefined, true, -pi / 2);\n this.hands[2].drawHand(this.staticCtx, 1000 - 75 * 5 * this.sizeHiddenHand, 150 * this.sizeHiddenHand, 5 * this.sizeHiddenHand, this.sizeHiddenHand, undefined, true, -pi);\n this.hands[3].drawHand(this.staticCtx, 150 * this.sizeHiddenHand, 75 * 5 * this.sizeHiddenHand, 5 * this.sizeHiddenHand, this.sizeHiddenHand, undefined, true, pi / 2);\n };\n Game.prototype.drawGroups = function (size) {\n var os = 25;\n var pi = 3.141592;\n for (var p = 0; p < 4; p++) {\n var rotation = [0, -pi / 2, -pi, pi / 2][p];\n if (this.groups[p].length > 0) {\n for (var i = this.groups[p].length - 1; i >= 0; i--) {\n this.groups[p][i].drawGroup(this.staticCtx, 1050 - 240 - (260 + os) * size * i, 1050 - 62, 5, 0.6, rotation);\n }\n }\n }\n };\n Game.prototype.drawDiscard = function (p, highlitedTile) {\n var pi = 3.141592;\n this.staticCtx.save();\n this.staticCtx.translate(525, 525);\n this.staticCtx.rotate([0, -pi / 2, -pi, pi / 2][p]);\n var x = -this.sizeDiscard * 475 / 2;\n var y = this.sizeDiscard * (475 / 2 + 5);\n for (var i = 0; i < this.discards[p].length; i++) {\n var tile = this.discards[p][i];\n if (i < 12) {\n tile.drawTile(this.staticCtx, x + (i % 6) * 80 * this.sizeDiscard, y + Math.floor(i / 6) * 105 * this.sizeDiscard, this.sizeDiscard, false, 0, highlitedTile === null || highlitedTile === void 0 ? void 0 : highlitedTile.isEqual(tile.getFamily(), tile.getValue()));\n }\n else {\n tile.drawTile(this.staticCtx, x + (i - 12) * 80 * this.sizeDiscard, y + 2 * 105 * this.sizeDiscard, this.sizeDiscard, false, 0, highlitedTile === null || highlitedTile === void 0 ? void 0 : highlitedTile.isEqual(tile.getFamily(), tile.getValue()));\n }\n }\n if (this.lastDiscard === p) {\n var j = this.discards[p].length - 1;\n var tx = j < 12 ?\n x + (j % 6) * 80 * this.sizeDiscard :\n x + (j - 12) * 80 * this.sizeDiscard;\n var ty = j < 12 ?\n y + Math.floor(j / 6) * 105 * this.sizeDiscard :\n y + 2 * 105 * this.sizeDiscard;\n tx += 75 / 2 * this.sizeDiscard;\n ty += 115 * this.sizeDiscard;\n var ts = 10;\n this.staticCtx.fillStyle = \"#ff0000\";\n this.staticCtx.beginPath();\n this.staticCtx.moveTo(tx, ty);\n this.staticCtx.lineTo(tx + ts / 2, ty + 0.866 * ts);\n this.staticCtx.lineTo(tx - ts / 2, ty + 0.866 * ts);\n this.staticCtx.lineTo(tx, ty);\n this.staticCtx.fill();\n this.staticCtx.stroke();\n }\n this.staticCtx.restore();\n };\n Game.prototype.preload = function () {\n return __awaiter(this, void 0, void 0, function () {\n return __generator(this, function (_a) {\n switch (_a.label) {\n case 0: return [4 /*yield*/, this.deck.preload()];\n case 1:\n _a.sent();\n return [4 /*yield*/, Promise.all(this.hands.map(function (h) { return h.preload(); }))];\n case 2:\n _a.sent();\n return [2 /*return*/];\n }\n });\n });\n };\n return Game;\n}());\n\n\n\n//# sourceURL=webpack:///./src/game.ts?"); +eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ Game: () => (/* binding */ Game)\n/* harmony export */ });\n/* harmony import */ var _deck__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./deck */ \"./src/deck.ts\");\n/* harmony import */ var _group__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./group */ \"./src/group.ts\");\n/* harmony import */ var _button__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./button */ \"./src/button.ts\");\nvar __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nvar __generator = (undefined && undefined.__generator) || function (thisArg, body) {\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === \"function\" ? Iterator : Object).prototype);\n return g.next = verb(0), g[\"throw\"] = verb(1), g[\"return\"] = verb(2), typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\n function verb(n) { return function (v) { return step([n, v]); }; }\n function step(op) {\n if (f) throw new TypeError(\"Generator is already executing.\");\n while (g && (g = 0, op[0] && (_ = 0)), _) try {\n if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\n if (y = 0, t) op = [op[0] & 2, t.value];\n switch (op[0]) {\n case 0: case 1: t = op; break;\n case 4: _.label++; return { value: op[1], done: false };\n case 5: _.label++; y = op[1]; op = [0]; continue;\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\n default:\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\n if (t[2]) _.ops.pop();\n _.trys.pop(); continue;\n }\n op = body.call(thisArg, _);\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\n }\n};\n\n\n\nvar Game = /** @class */ (function () {\n function Game(ctx, cv, staticCtx, staticCv, red) {\n if (red === void 0) { red = false; }\n // game values\n this.turn = 0;\n this.selectedTile = undefined;\n this.canCall = false;\n this.hasPicked = false;\n this.hasPlayed = false;\n this.lastPlayed = Date.now();\n // display parameter\n this.BG_RECT = { color: \"#007730\", x: 0, y: 0, w: 1050, h: 1050 };\n this.sizeHand = 0.7;\n this.sizeHiddenHand = 0.6;\n this.sizeDiscard = 0.6;\n this.ctx = ctx;\n this.cv = cv;\n this.staticCtx = staticCtx;\n this.staticCv = staticCv;\n this.deck = new _deck__WEBPACK_IMPORTED_MODULE_0__.Deck(red);\n this.hands = [\n this.deck.getRandomHand(),\n this.deck.getRandomHand(),\n this.deck.getRandomHand(),\n this.deck.getRandomHand()\n ];\n this.discards = [[], [], [], []];\n this.lastDiscard = undefined;\n this.groups = [[], [], [], []];\n this.hands[0].sort();\n this.pick(0);\n }\n Game.prototype.draw = function (mp) {\n // background\n this.staticCtx.clearRect(0, 0, this.cv.width, this.cv.height);\n this.staticCtx.fillStyle = this.BG_RECT.color;\n this.staticCtx.fillRect(this.BG_RECT.x, this.BG_RECT.y, this.BG_RECT.w, this.BG_RECT.h);\n this.getSelected(mp);\n this.drawGame();\n this.ctx.clearRect(0, 0, this.cv.width, this.cv.height);\n this.ctx.drawImage(this.staticCv, 0, 0);\n };\n Game.prototype.getDeck = function () {\n return this.deck;\n };\n Game.prototype.getHands = function () {\n return this.hands;\n };\n Game.prototype.click = function (mp) {\n var action = (0,_button__WEBPACK_IMPORTED_MODULE_2__.clickAction)(mp.x, mp.y, this.canDoAChii().length > 0, this.canDoAPon(), false, false, false);\n if (this.canCall && action !== -1) { // can call\n if (action === 0) { // pass\n this.canCall = false;\n if (this.turn === 3) {\n this.turn = 0;\n this.pick(0);\n }\n else {\n this.turn++;\n }\n this.hasPicked = true;\n this.hasPlayed = false;\n }\n else if (action == 2) { // pon\n this.pon(this.turn);\n }\n }\n else { // nothing unusual\n if (this.turn === 0 && this.selectedTile !== undefined) {\n this.discard(0, this.selectedTile);\n this.turn++;\n }\n }\n };\n Game.prototype.getSelected = function (mp) {\n var rect = this.cv.getBoundingClientRect();\n var x = 2.5 * 75 * 0.75;\n var y = 1050 - 250 * 0.6;\n var mouseX = mp.x - rect.left - x;\n var mouseY = mp.y - rect.top;\n var s = 83.9;\n var q = Math.floor(mouseX / (s * this.sizeHand));\n var r = mouseX - q * s * this.sizeHand;\n if (r <= (s - 3) * this.sizeHand &&\n q >= 0 &&\n q < this.hands[0].length() &&\n mouseY >= y &&\n mouseY <= y + 100 * this.sizeHand) {\n this.selectedTile = q;\n }\n else {\n this.selectedTile = undefined;\n }\n };\n ;\n Game.prototype.play = function () {\n if (this.turn !== 0) { // bot playing\n if (!this.hasPicked) { // begin of his turn\n this.lastPlayed = Date.now();\n this.pick(this.turn);\n this.hasPicked = true;\n }\n else if (!this.hasPlayed) { // middle of his turn\n if (Date.now() - this.lastPlayed > 700) {\n this.lastPlayed = Date.now();\n var n = Math.floor(this.hands[this.turn].length() * Math.random());\n this.discard(this.turn, n);\n this.hasPlayed = true;\n this.canCall = this.canDoAChii().length > 0 || this.canDoAPon();\n }\n }\n else if (!this.canCall) { // end of his turn\n if (this.turn === 3) {\n this.turn = 0;\n this.pick(0);\n }\n else {\n this.turn++;\n }\n this.hasPicked = false;\n this.hasPlayed = false;\n }\n }\n };\n Game.prototype.pick = function (player) {\n this.hands[player].push(this.deck.pop());\n this.hands[player].isolate = true;\n };\n Game.prototype.discard = function (player, n) {\n var tile = this.hands[player].eject(n);\n this.hands[player].sort();\n tile.setTilt();\n this.discards[player].push(tile);\n this.hands[player].isolate = false;\n this.hands[player].sort();\n this.lastDiscard = player;\n this.lastPlayed = Date.now();\n };\n Game.prototype.canDoAChii = function () {\n var chii = [];\n if (this.lastDiscard !== undefined &&\n this.lastDiscard === 3 &&\n this.turn === 3 &&\n this.discards[this.lastDiscard][this.discards[this.lastDiscard].length - 1].getFamily() < 4) {\n var t = this.discards[this.lastDiscard][this.discards[this.lastDiscard].length - 1];\n var h = this.hands[0];\n if (h.count(t.getFamily(), t.getValue() - 2) > 0 &&\n h.count(t.getFamily(), t.getValue() - 1) > 0) {\n chii.push(t.getValue() - 2);\n }\n else if (h.count(t.getFamily(), t.getValue() - 1) > 0 &&\n h.count(t.getFamily(), t.getValue() + 1) > 0) {\n chii.push(t.getValue() - 1);\n }\n else if (h.count(t.getFamily(), t.getValue() + 1) > 0 &&\n h.count(t.getFamily(), t.getValue() + 2) > 0) {\n chii.push(t.getValue());\n }\n }\n return chii;\n };\n Game.prototype.chii = function (minValue) {\n console.log(\"Chii !\\n\");\n };\n Game.prototype.canDoAPon = function () {\n if (this.lastDiscard !== undefined &&\n this.lastDiscard !== 0 &&\n this.turn !== 0) {\n var t = this.discards[this.lastDiscard][this.discards[this.lastDiscard].length - 1];\n return this.hands[0].count(t.getFamily(), t.getValue()) >= 2;\n }\n else {\n return false;\n }\n };\n Game.prototype.pon = function (p) {\n var t = this.discards[p].pop();\n this.lastDiscard = undefined;\n var t2 = this.hands[0].find(t.getFamily(), t.getValue());\n var t3 = this.hands[0].find(t.getFamily(), t.getValue());\n [t, t2, t3].forEach(function (t) { return t.setTilt(); });\n this.groups[0].push(new _group__WEBPACK_IMPORTED_MODULE_1__.Group([t, t2, t3], p));\n this.turn = 0;\n this.hasPicked = true;\n this.hasPlayed = false;\n };\n Game.prototype.drawGame = function () {\n // update game\n this.play();\n // hands\n this.drawHands();\n // groups\n this.drawGroups(0.6);\n // discards\n for (var i = 0; i < 4; i++) {\n this.drawDiscard(i, this.hands[0].get(this.selectedTile));\n }\n // called\n (0,_button__WEBPACK_IMPORTED_MODULE_2__.drawButtons)(this.staticCtx, this.canDoAChii().length > 0, this.canDoAPon(), false, false, false);\n };\n Game.prototype.drawHands = function () {\n var pi = 3.141592;\n this.hands[0].drawHand(this.staticCtx, 2.5 * 75 * 0.75, 1000 - 150 * this.sizeHand, 5 * this.sizeHand, 0.75, this.selectedTile, false, 0);\n this.hands[1].drawHand(this.staticCtx, 1000 - 150 * this.sizeHiddenHand, 1000 - 75 * 5 * this.sizeHiddenHand, 5 * this.sizeHiddenHand, this.sizeHiddenHand, undefined, true, -pi / 2);\n this.hands[2].drawHand(this.staticCtx, 1000 - 75 * 5 * this.sizeHiddenHand, 150 * this.sizeHiddenHand, 5 * this.sizeHiddenHand, this.sizeHiddenHand, undefined, true, -pi);\n this.hands[3].drawHand(this.staticCtx, 150 * this.sizeHiddenHand, 75 * 5 * this.sizeHiddenHand, 5 * this.sizeHiddenHand, this.sizeHiddenHand, undefined, true, pi / 2);\n };\n Game.prototype.drawGroups = function (size) {\n var os = 25;\n var pi = 3.141592;\n for (var p = 0; p < 4; p++) {\n var rotation = [0, -pi / 2, -pi, pi / 2][p];\n if (this.groups[p].length > 0) {\n for (var i = this.groups[p].length - 1; i >= 0; i--) {\n this.groups[p][i].drawGroup(this.staticCtx, 1050 - 240 - (260 + os) * size * i, 1050 - 62, 5, 0.6, rotation);\n }\n }\n }\n };\n Game.prototype.drawDiscard = function (p, highlitedTile) {\n var pi = 3.141592;\n this.staticCtx.save();\n this.staticCtx.translate(525, 525);\n this.staticCtx.rotate([0, -pi / 2, -pi, pi / 2][p]);\n var x = -this.sizeDiscard * 475 / 2;\n var y = this.sizeDiscard * (475 / 2 + 5);\n for (var i = 0; i < this.discards[p].length; i++) {\n var tile = this.discards[p][i];\n if (i < 12) {\n tile.drawTile(this.staticCtx, x + (i % 6) * 80 * this.sizeDiscard, y + Math.floor(i / 6) * 105 * this.sizeDiscard, this.sizeDiscard, false, 0, highlitedTile === null || highlitedTile === void 0 ? void 0 : highlitedTile.isEqual(tile.getFamily(), tile.getValue()));\n }\n else {\n tile.drawTile(this.staticCtx, x + (i - 12) * 80 * this.sizeDiscard, y + 2 * 105 * this.sizeDiscard, this.sizeDiscard, false, 0, highlitedTile === null || highlitedTile === void 0 ? void 0 : highlitedTile.isEqual(tile.getFamily(), tile.getValue()));\n }\n }\n if (this.lastDiscard === p) {\n var j = this.discards[p].length - 1;\n var tx = j < 12 ?\n x + (j % 6) * 80 * this.sizeDiscard :\n x + (j - 12) * 80 * this.sizeDiscard;\n var ty = j < 12 ?\n y + Math.floor(j / 6) * 105 * this.sizeDiscard :\n y + 2 * 105 * this.sizeDiscard;\n tx += 75 / 2 * this.sizeDiscard;\n ty += 115 * this.sizeDiscard;\n var ts = 10;\n this.staticCtx.fillStyle = \"#ff0000\";\n this.staticCtx.beginPath();\n this.staticCtx.moveTo(tx, ty);\n this.staticCtx.lineTo(tx + ts / 2, ty + 0.866 * ts);\n this.staticCtx.lineTo(tx - ts / 2, ty + 0.866 * ts);\n this.staticCtx.lineTo(tx, ty);\n this.staticCtx.fill();\n this.staticCtx.stroke();\n }\n this.staticCtx.restore();\n };\n Game.prototype.preload = function () {\n return __awaiter(this, void 0, void 0, function () {\n return __generator(this, function (_a) {\n switch (_a.label) {\n case 0: return [4 /*yield*/, this.deck.preload()];\n case 1:\n _a.sent();\n return [4 /*yield*/, Promise.all(this.hands.map(function (h) { return h.preload(); }))];\n case 2:\n _a.sent();\n return [2 /*return*/];\n }\n });\n });\n };\n return Game;\n}());\n\n\n\n//# sourceURL=webpack:///./src/game.ts?"); /***/ }), diff --git a/src/game.ts b/src/game.ts index c69c3d0..170c783 100644 --- a/src/game.ts +++ b/src/game.ts @@ -107,7 +107,7 @@ export class Game { } else { this.turn++; } - this.hasPicked = false; + this.hasPicked = true; this.hasPlayed = false; } else if (action == 2) { // pon this.pon(this.turn); @@ -152,6 +152,7 @@ export class Game { this.turn !== 0 ) { // bot playing if (!this.hasPicked) { // begin of his turn + this.lastPlayed = Date.now(); this.pick(this.turn); this.hasPicked = true; } else if (!this.hasPlayed) { // middle of his turn @@ -160,7 +161,7 @@ export class Game { let n = Math.floor(this.hands[this.turn].length() * Math.random()); this.discard(this.turn, n); this.hasPlayed = true; - this.canCall = this.canDoAPon(); + this.canCall = this.canDoAChii().length > 0 || this.canDoAPon(); } } else if (!this.canCall) { // end of his turn if (this.turn === 3) { @@ -191,33 +192,12 @@ export class Game { this.lastPlayed = Date.now(); } - private canDoAPon(): boolean { - if (this.lastDiscard !== undefined && this.lastDiscard !== 0) { - let t = this.discards[this.lastDiscard][this.discards[this.lastDiscard].length-1]; - return this.hands[0].count(t.getFamily(), t.getValue()) >= 2; - } else { - return false; - } - } - - private pon(p: number): void { - 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; - [t, t2, t3].forEach(t => t.setTilt()); - this.groups[0].push(new Group([t, t2, t3], p)); - - this.turn = 0; - this.hasPicked = true; - this.hasPlayed = false; - } - private canDoAChii(): Array { let chii = [] as Array; if ( this.lastDiscard !== undefined && this.lastDiscard === 3 && + this.turn === 3 && this.discards[this.lastDiscard][this.discards[this.lastDiscard].length-1].getFamily() < 4 ) { let t = this.discards[this.lastDiscard][this.discards[this.lastDiscard].length-1]; @@ -246,6 +226,32 @@ export class Game { console.log("Chii !\n"); } + private canDoAPon(): boolean { + if ( + this.lastDiscard !== undefined && + this.lastDiscard !== 0 && + this.turn !== 0 + ) { + let t = this.discards[this.lastDiscard][this.discards[this.lastDiscard].length-1]; + return this.hands[0].count(t.getFamily(), t.getValue()) >= 2; + } else { + return false; + } + } + + private pon(p: number): void { + 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; + [t, t2, t3].forEach(t => t.setTilt()); + this.groups[0].push(new Group([t, t2, t3], p)); + + this.turn = 0; + this.hasPicked = true; + this.hasPlayed = false; + } + private drawGame(): void { // update game this.play();