11
1
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import numpy as np
|
||||
from loguru import logger
|
||||
from src.engine.dizhu.player_state import PlayerState
|
||||
from src.engine.dizhu.deck import Deck
|
||||
|
||||
class DiZhuEngine:
|
||||
def __init__(self):
|
||||
self.deck = Deck() # 牌堆
|
||||
@@ -28,11 +30,19 @@ class DiZhuEngine:
|
||||
self.current_player_index = 0
|
||||
self.game_over = False
|
||||
|
||||
# 日志输出
|
||||
logger.info("游戏初始化完成")
|
||||
logger.info(f"地主牌: {self.landlord_cards}")
|
||||
for i, player in enumerate(self.players):
|
||||
logger.info(f"玩家 {i + 1} ({player.role}) 手牌: {player.hand_cards}")
|
||||
|
||||
def get_current_player(self):
|
||||
"""
|
||||
获取当前玩家对象
|
||||
"""
|
||||
return self.players[self.current_player_index]
|
||||
current_player = self.players[self.current_player_index]
|
||||
logger.info(f"当前玩家: 玩家 {self.current_player_index + 1} ({current_player.role})")
|
||||
return current_player
|
||||
|
||||
def step(self, action):
|
||||
"""
|
||||
@@ -41,30 +51,36 @@ class DiZhuEngine:
|
||||
"""
|
||||
current_player = self.get_current_player()
|
||||
|
||||
logger.info(f"玩家 {self.current_player_index + 1} 的动作: {action}")
|
||||
|
||||
if action == "pass":
|
||||
# 过牌作为单独的历史记录
|
||||
current_player.history.append([])
|
||||
logger.info(f"玩家 {self.current_player_index + 1} 选择过牌")
|
||||
else:
|
||||
# 确保动作始终为列表
|
||||
if not isinstance(action, list):
|
||||
action = [action]
|
||||
# 检查动作合法性
|
||||
if not all(card in current_player.hand_cards for card in action):
|
||||
logger.error(f"玩家 {self.current_player_index + 1} 的动作非法: {action}")
|
||||
raise ValueError(f"玩家手牌不足以完成此次出牌: {action}")
|
||||
# 移除出牌
|
||||
for card in action:
|
||||
current_player.hand_cards.remove(card)
|
||||
# 记录动作
|
||||
current_player.history.append(action)
|
||||
logger.info(f"玩家 {self.current_player_index + 1} 出牌: {action}")
|
||||
|
||||
# 检查游戏是否结束
|
||||
if not current_player.hand_cards:
|
||||
self.game_over = True
|
||||
logger.info(f"游戏结束!玩家 {self.current_player_index + 1} ({current_player.role}) 获胜")
|
||||
return f"{current_player.role} 胜利!"
|
||||
|
||||
# 切换到下一个玩家
|
||||
self.current_player_index = (self.current_player_index + 1) % 3
|
||||
|
||||
logger.info(f"切换到玩家 {self.current_player_index + 1}")
|
||||
|
||||
def get_game_state(self):
|
||||
"""
|
||||
@@ -83,4 +99,11 @@ class DiZhuEngine:
|
||||
"current_player_index": self.current_player_index,
|
||||
"game_over": self.game_over,
|
||||
}
|
||||
logger.info("当前游戏状态: ")
|
||||
logger.info(f"地主牌: {self.landlord_cards}")
|
||||
for i, player in enumerate(self.players):
|
||||
logger.info(f"玩家 {i + 1} ({player.role}) 手牌: {player.hand_cards}")
|
||||
logger.info(f"玩家 {i + 1} 出牌历史: {player.history}")
|
||||
logger.info(f"当前玩家索引: {self.current_player_index}")
|
||||
logger.info(f"游戏是否结束: {self.game_over}")
|
||||
return state
|
||||
|
||||
Reference in New Issue
Block a user