gametree: fix player

This commit is contained in:
2025-11-06 17:41:59 +08:00
parent 61923407af
commit b4afdb06c4
7 changed files with 236 additions and 171 deletions

67
test/p_creat.py Normal file
View File

@@ -0,0 +1,67 @@
import sys
from pathlib import Path
import pytest
from gametree import Game, PlayerId, Street, ActionType
def make_players(n, stack):
return [(PlayerId(i, f"p{i}"), stack) for i in range(n)]
def test_player_states():
sb = 5
bb = 10
d_idx = 1
n_player = 4
stack = 500
players_init = make_players(n_player, stack)
g = Game(players_init=players_init, small_blind=sb, big_blind=bb, dealer_idx=d_idx)
assert g.current_street == Street.PREFLOP
sb_idx = (d_idx + 1) % n_player
bb_idx = (d_idx + 2) % n_player
next_idx = (d_idx + 3) % n_player
# init_bet
assert g.get_sb_amt == sb
assert g.get_bb_amt == bb
assert g.get_sb_idx == sb_idx
assert g.get_bb_idx == bb_idx
assert g.get_next_act_idx == next_idx
# init_players
players = g.get_active_players
assert len(players) == n_player
for idx, p in enumerate(players):
assert p.hand_cards is not None and len(p.hand_cards) == 2
if idx == sb_idx:
assert p.stack == stack - sb
assert p.committed == sb
elif idx == bb_idx:
assert p.stack == stack - bb
assert p.committed == bb
else:
assert p.stack == stack
assert p.committed == 0
# init_deck
deck = getattr(g, 'deck_manager', None)
rem_cards = getattr(deck, 'remaining_cards', None)
assert rem_cards is not None
assert rem_cards() == 28 # 36-8
# first_player
legal_actions = g.legal_actions(g.players[next_idx].pid)
assert legal_actions is not None and len(legal_actions) == 4
assert set(legal_actions) == {ActionType.FOLD, ActionType.CALL,
ActionType.RAISE, ActionType.ALLIN}
# n_players = len(players_init)
# if n_players < 2:
# expected_sb = None
# expected_bb = None
# elif n_players == 2:
# expected_sb = d_idx
# expected_bb = (d_idx + 1) % n_players
# else:
# expected_sb = (d_idx + 1) % len(players_init)
# expected_bb = (d_idx + 2) % len(players_init)
# assert g.get_sb_idx == expected_sb
# assert g.get_bb_idx == expected_bb

View File

@@ -1,64 +0,0 @@
{
"player_names": {
"0": "a",
"1": "b",
"2": "c",
"3": "d",
"4": "e"
},
"game_state": {
"players_init": [
[
0,
500
],
[
1,
500
],
[
2,
500
],
[
3,
500
],
[
4,
500
]
],
"dealer_idx": 0,
"small_blind": 5,
"big_blind": 10,
"current_street": "PREFLOP",
"all_actions": [
{
"type": "CALL",
"actor": 3,
"amount": null
},
{
"type": "CALL",
"actor": 4,
"amount": null
},
{
"type": "CALL",
"actor": 0,
"amount": null
},
{
"type": "ALL_IN",
"actor": 1,
"amount": null
},
{
"type": "ALL_IN",
"actor": 2,
"amount": null
}
]
}
}

View File

@@ -15,6 +15,7 @@ from gametree.model import act_fold, act_call, act_check, act_bet, act_raise, ac
GAME_FILE = "pg.json"
def create_file_idx():
Path("data").mkdir(exist_ok=True)
files = list(Path("data").glob("pg_*.json"))
game_id = len(files) + 1
return f"data/pg_{game_id:03d}.json"
@@ -64,7 +65,7 @@ def display_game_status(game: Game, player_names: Dict[int,str], show_cards_for:
marks.append("->")
if pstate.folded:
marks.append("F")
if pstate.all_in:
if pstate.allin:
marks.append("A")
info = f"{i}:{name} stack={pstate.stack} bet={pstate.committed} [{' '.join(marks)}]"
if show_cards_for == "all" or show_cards_for == name:
@@ -144,8 +145,8 @@ def display_player_turn():
elif a == ActionType.RAISE:
min_raise, max_raise = GAME.get_raise_bounds(pid)
print(f" - raise (min: {min_raise}, max: {max_raise})")
elif a == ActionType.ALL_IN:
print(f" - all_in (amount: {GAME.get_allin_amt(pid)})")
elif a == ActionType.ALLIN:
print(f" - allin (amount: {GAME.get_allin_amt(pid)})")
print("----------------------------------------------------")
@@ -200,7 +201,7 @@ def main():
return
input = shlex.split(args)
if len(input) < 2:
print("act <player_name> <fold|call|check|bet|raise|all_in> [amount]")
print("act <player_name> <fold|call|check|bet|raise|allin> [amount]")
return
pname = input[0]
action = input[1]
@@ -213,7 +214,7 @@ def main():
legal = GAME.legal_actions(pid)
if action.lower() == 'allin':
atype = ActionType.ALL_IN
atype = ActionType.ALLIN
elif action.lower() in ('fold', 'call', 'check', 'bet', 'raise'):
atype = ActionType[action.upper()]
else:
@@ -273,7 +274,7 @@ def main():
def cmd_help(args):
print("可用命令:")
print(" set <small>/<big> <p1> [p2 ...] [--stack N] ")
print(" act <player> <fold|call|check|bet|raise|all_in> [amount]")
print(" act <player> <fold|call|check|bet|raise|allin> [amount]")
print(" status [player|all] ")
print(" save ")
print(" reset ")