Add CPU opponents and a table type selector (cash only / friends / CPU)

- Create screen: choose キャッシュオンリー (real cards, chips only), 友人戦
  (server deals and settles) or CPU戦 (友人戦 plus 1–8 CPU players); replaces
  the card toggle. The lobby shows each table's type
- Poker AI written from scratch (server/ai.js, no libraries): fast 7-card
  evaluator (checked against bestHand), Monte Carlo equity, and decisions from
  hand rank, pot odds, raise count, opponents and a per-CPU style (aggression,
  bluff rate); continuation bets via the last aggressor. It never sees other
  players' hole cards
- CPU seats: think time of 0.7–1.8s (faster when no human is left in the hand),
  automatic rebuy, never the host, no new hand unless a human is playing,
  re-think after pause
- Play UI: robot avatars and a "考えています…" status for CPU turns
- Tests for the evaluator, equity, legality of AI actions and CPU tables

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
agomizi1cmandClaude Opus 5.5 committed 2026-10-17 16:55:25 +09:00
1 parent 66907345ff
commit 0d3a054f0d
12 files changed
+550 -46

No files matched your search

+84 -8
View File
@@ -4,6 +4,14 @@ const crypto = require('crypto');
const config = require('./config');
const Blinds = require('../public/assets/blinds');
const { Hand } = require('./hand');
const ai = require('./ai');
// テーブルの種類
// chips : キャッシュオンリー。カードは実物で配り、サイトはチップだけを管理する
// friends: 友人戦。サーバがカードを配り、ショーダウンも自動で判定する
// cpu : CPU戦。友人戦に CPU プレイヤーが加わる
const MODES = ['chips', 'friends', 'cpu'];
const CPU_NAMES = ['ハル', 'ソラ', 'リク', 'ミオ', 'レン', 'ユイ', 'カイ', 'ナギ'];
class Table {
constructor(opts) {
@@ -14,14 +22,18 @@ class Table {
this.baseBigBlind = opts.bigBlind;
this.timerEnabled = opts.blindTimerEnabled;
this.intervalMin = opts.blindIntervalMin;
// カード機能: サーバがカードを配り、ショーダウンの勝敗も自動で判定する(オフなら実物のカードで遊ぶ)
this.cardsEnabled = opts.cardsEnabled === true;
this.mode = MODES.includes(opts.mode) ? opts.mode : 'chips';
// カード機能: サーバがカードを配り、ショーダウンの勝敗も自動で判定する(キャッシュオンリーは実物のカード)
this.cardsEnabled = this.mode !== 'chips';
this.hostToken = opts.hostToken || null;
this.maxSeats = config.MAX_SEATS;
// { token, name, stack, buyIns, sittingOut, left }
// { token, name, stack, buyIns, sittingOut, left, bot, style }
// left: ハンド中に退席した人。ハンドが終わるまで席を予約しておく
// bot : CPU プレイヤー(style は思考の性格)
this.seats = new Array(this.maxSeats).fill(null);
this.botTurn = null; // { key, at }: CPU が行動する時刻
if (this.mode === 'cpu') this.seatBots(opts.cpuCount);
this.hand = null;
this.started = false;
this.paused = false;
@@ -38,6 +50,28 @@ class Table {
this.emptySince = null;
}
// CPU は席1から順に座らせる(席0は作成者)
seatBots(count) {
const n = Math.max(1, Math.min(this.maxSeats - 1, Number.isInteger(count) ? count : 1));
const names = CPU_NAMES.slice();
for (let i = names.length - 1; i > 0; i--) {
const j = crypto.randomInt(i + 1);
[names[i], names[j]] = [names[j], names[i]];
}
for (let k = 0; k < n; k++) {
this.seats[k + 1] = {
token: crypto.randomBytes(16).toString('hex'), // ブラウザには渡さない
name: names[k],
stack: this.startingStack,
buyIns: 1,
sittingOut: false,
left: false,
bot: true,
style: ai.randomStyle(),
};
}
}
get intervalMs() {
return this.intervalMin * 60 * 1000;
}
@@ -74,8 +108,10 @@ class Table {
.filter((i) => i >= 0);
}
// CPU だけでハンドを続けても意味がないので、人間が1人は参加している必要がある
canDeal() {
return this.started && !this.paused && this.eligibleSeats().length >= 2;
const eligible = this.eligibleSeats();
return this.started && !this.paused && eligible.length >= 2 && eligible.some((i) => !this.seats[i].bot);
}
// 着席した席番号を返す。満席なら -1(観戦)
@@ -110,7 +146,7 @@ class Table {
this.seats[i] = null;
}
if (this.hostToken === token) {
const next = this.seats.find((s) => s && !s.left);
const next = this.seats.find((s) => s && !s.left && !s.bot);
this.hostToken = next ? next.token : null;
}
return true;
@@ -130,6 +166,13 @@ class Table {
startNewHand() {
this.removeLeftSeats();
// チップがなくなった CPU は自動でリバイ
for (const s of this.seats) {
if (s && s.bot && s.stack === 0) {
s.stack = this.startingStack;
s.buyIns++;
}
}
const eligible = this.eligibleSeats();
this.buttonSeat =
this.buttonSeat == null
@@ -143,6 +186,7 @@ class Table {
const { sb, bb } = this.blinds;
this.nextHandAt = null;
this.runoutAt = null;
this.botTurn = null;
this.hand = new Hand({
number: this.handNumber,
seats: this.seats,
@@ -217,6 +261,7 @@ class Table {
// 止めていた時間だけ次のハンドの開始・場札をめくる時刻を後ろにずらす
if (this.nextHandAt != null) this.nextHandAt += now - this.pausedAt;
if (this.runoutAt != null) this.runoutAt += now - this.pausedAt;
this.botTurn = null; // CPU は再開してから考え直す
if (this.timerEnabled) this.levelEndsAt = now + this.levelRemainingMs;
this.paused = false;
this.pausedAt = null;
@@ -231,7 +276,33 @@ class Table {
return null;
}
// 時間で進む処理(ブラインドタイマー・オールイン後の場札・次のハンドの開始)
// CPU の考える時間。人間がハンドに残っていなければ早めに進める
botDelay() {
const h = this.hand;
const humanLive = h.live().some((p) => !this.seats[p.seat].bot && !this.seats[p.seat].left);
if (!humanLive) return config.BOT_FAST_MS;
return config.BOT_THINK_MIN_MS + crypto.randomInt(config.BOT_THINK_MAX_MS - config.BOT_THINK_MIN_MS + 1);
}
// 手番の CPU が考える時間を過ぎていたら行動する。行動したら true
botStep(now) {
const h = this.hand;
if (!h || h.finished || h.toAct == null || !this.seats[h.toAct].bot) return false;
const key = `${h.number}:${h.actions}`;
if (!this.botTurn || this.botTurn.key !== key) {
this.botTurn = { key, at: now + this.botDelay() };
return false;
}
if (now < this.botTurn.at) return false;
this.botTurn = null;
const seat = h.toAct;
const d = ai.decide(h, seat, { style: this.seats[seat].style, iterations: config.BOT_ITERATIONS });
// 万一判断が不正でも止まらないよう、チェック/フォールドにする
if (!d || h.act(seat, d.type, d.amount)) h.autoAct();
return true;
}
// 時間で進む処理(ブラインドタイマー・CPU の行動・オールイン後の場札・次のハンドの開始)
// 状態が変わったら true
tick(now) {
let changed = false;
@@ -242,6 +313,8 @@ class Table {
}
if (this.paused) return changed;
if (this.botStep(now)) changed = true;
const h = this.hand;
if (h && h.runout) {
if (this.runoutAt == null) this.runoutAt = now + config.RUNOUT_STEP_MS;
@@ -292,6 +365,7 @@ class Table {
timerEnabled: this.timerEnabled,
intervalMin: this.intervalMin,
cardsEnabled: this.cardsEnabled,
mode: this.mode,
createdAt: this.createdAt,
};
}
@@ -313,6 +387,7 @@ class Table {
startingStack: this.startingStack,
maxSeats: this.maxSeats,
cardsEnabled: this.cardsEnabled,
mode: this.mode,
level: this.level,
smallBlind: sb,
bigBlind: bb,
@@ -336,7 +411,8 @@ class Table {
seat: i,
name: s.name,
stack: s.stack,
connected: connectedTokens.has(s.token),
connected: Boolean(s.bot) || connectedTokens.has(s.token),
bot: Boolean(s.bot),
sittingOut: s.sittingOut,
...(hv && hv.players[i] ? { inHand: true, ...hv.players[i] } : { inHand: false }),
}
@@ -402,4 +478,4 @@ class TableManager {
}
}
module.exports = { TableManager, Table };
module.exports = { TableManager, Table, MODES };