parent
1d6593a8f2
commit
e54a8c5f00
|
|
@ -9,9 +9,9 @@ def setup_logging():
|
||||||
# 清除所有现有日志处理器,防止重复配置
|
# 清除所有现有日志处理器,防止重复配置
|
||||||
logger.remove()
|
logger.remove()
|
||||||
|
|
||||||
# 配置日志文件
|
# 配置斗地主日志文件
|
||||||
logger.add(
|
logger.add(
|
||||||
os.path.join(log_dir, "chengdu_mj_engine.log"),
|
os.path.join(log_dir, "doudizhu_engine.log"), # 斗地主的日志文件
|
||||||
rotation="10 MB",
|
rotation="10 MB",
|
||||||
level="DEBUG",
|
level="DEBUG",
|
||||||
format="{time:YYYY-MM-DD HH:mm:ss.SSS} {level} {message}",
|
format="{time:YYYY-MM-DD HH:mm:ss.SSS} {level} {message}",
|
||||||
|
|
@ -19,7 +19,17 @@ def setup_logging():
|
||||||
diagnose=True # 启用变量诊断信息
|
diagnose=True # 启用变量诊断信息
|
||||||
)
|
)
|
||||||
|
|
||||||
# 配置控制台日志(可选)
|
# 配置麻将日志文件(如果需要)
|
||||||
|
logger.add(
|
||||||
|
os.path.join(log_dir, "chengdu_mj_engine.log"),
|
||||||
|
rotation="10 MB",
|
||||||
|
level="DEBUG",
|
||||||
|
format="{time:YYYY-MM-DD HH:mm:ss.SSS} {level} {message}",
|
||||||
|
backtrace=True,
|
||||||
|
diagnose=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
# 配置控制台日志
|
||||||
logger.add(
|
logger.add(
|
||||||
lambda msg: print(msg),
|
lambda msg: print(msg),
|
||||||
level="DEBUG",
|
level="DEBUG",
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -2,7 +2,7 @@ from stable_baselines3 import PPO
|
||||||
from src.environment.dizhu_env import DouDiZhuEnv # 导入斗地主环境
|
from src.environment.dizhu_env import DouDiZhuEnv # 导入斗地主环境
|
||||||
import torch
|
import torch
|
||||||
from configs.log_config import setup_logging
|
from configs.log_config import setup_logging
|
||||||
from loguru import logger # 添加 logger
|
from loguru import logger # 使用日志工具
|
||||||
|
|
||||||
def train_dizhu_model():
|
def train_dizhu_model():
|
||||||
# 创建 DouDiZhuEnv 环境实例
|
# 创建 DouDiZhuEnv 环境实例
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from loguru import logger
|
||||||
from src.engine.dizhu.player_state import PlayerState
|
from src.engine.dizhu.player_state import PlayerState
|
||||||
from src.engine.dizhu.deck import Deck
|
from src.engine.dizhu.deck import Deck
|
||||||
|
|
||||||
class DiZhuEngine:
|
class DiZhuEngine:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.deck = Deck() # 牌堆
|
self.deck = Deck() # 牌堆
|
||||||
|
|
@ -28,11 +30,19 @@ class DiZhuEngine:
|
||||||
self.current_player_index = 0
|
self.current_player_index = 0
|
||||||
self.game_over = False
|
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):
|
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):
|
def step(self, action):
|
||||||
"""
|
"""
|
||||||
|
|
@ -41,30 +51,36 @@ class DiZhuEngine:
|
||||||
"""
|
"""
|
||||||
current_player = self.get_current_player()
|
current_player = self.get_current_player()
|
||||||
|
|
||||||
|
logger.info(f"玩家 {self.current_player_index + 1} 的动作: {action}")
|
||||||
|
|
||||||
if action == "pass":
|
if action == "pass":
|
||||||
# 过牌作为单独的历史记录
|
# 过牌作为单独的历史记录
|
||||||
current_player.history.append([])
|
current_player.history.append([])
|
||||||
|
logger.info(f"玩家 {self.current_player_index + 1} 选择过牌")
|
||||||
else:
|
else:
|
||||||
# 确保动作始终为列表
|
# 确保动作始终为列表
|
||||||
if not isinstance(action, list):
|
if not isinstance(action, list):
|
||||||
action = [action]
|
action = [action]
|
||||||
# 检查动作合法性
|
# 检查动作合法性
|
||||||
if not all(card in current_player.hand_cards for card in 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}")
|
raise ValueError(f"玩家手牌不足以完成此次出牌: {action}")
|
||||||
# 移除出牌
|
# 移除出牌
|
||||||
for card in action:
|
for card in action:
|
||||||
current_player.hand_cards.remove(card)
|
current_player.hand_cards.remove(card)
|
||||||
# 记录动作
|
# 记录动作
|
||||||
current_player.history.append(action)
|
current_player.history.append(action)
|
||||||
|
logger.info(f"玩家 {self.current_player_index + 1} 出牌: {action}")
|
||||||
|
|
||||||
# 检查游戏是否结束
|
# 检查游戏是否结束
|
||||||
if not current_player.hand_cards:
|
if not current_player.hand_cards:
|
||||||
self.game_over = True
|
self.game_over = True
|
||||||
|
logger.info(f"游戏结束!玩家 {self.current_player_index + 1} ({current_player.role}) 获胜")
|
||||||
return f"{current_player.role} 胜利!"
|
return f"{current_player.role} 胜利!"
|
||||||
|
|
||||||
# 切换到下一个玩家
|
# 切换到下一个玩家
|
||||||
self.current_player_index = (self.current_player_index + 1) % 3
|
self.current_player_index = (self.current_player_index + 1) % 3
|
||||||
|
logger.info(f"切换到玩家 {self.current_player_index + 1}")
|
||||||
|
|
||||||
def get_game_state(self):
|
def get_game_state(self):
|
||||||
"""
|
"""
|
||||||
|
|
@ -83,4 +99,11 @@ class DiZhuEngine:
|
||||||
"current_player_index": self.current_player_index,
|
"current_player_index": self.current_player_index,
|
||||||
"game_over": self.game_over,
|
"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
|
return state
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue