【本地国标麻将环境源码】botzone

【本地国标麻将环境源码】botzone

botzone-local

/botzone

/envs

/botzone

/chinesestandardmahjong.py

from collections import defaultdict

from copy import deepcopy

import random

from botzone import *

from botzone.online.viewer.chinesestandardmahjong import ChineseStandardMahjongTextViewer

try:

from MahjongGB import MahjongFanCalculator

except:

print('MahjongGB library required! Please visit https://github.com/ailab-pku/PyMahjongGB for more information.')

raise

class ChineseStandardMahjongEnv(Env):

'''

Description:

Mahjong using Chinese Standard Rule.

Rule: https://wiki.botzone.org.cn/index.php?title=Chinese-Standard-Mahjong/en

Decision order: Four players act simutaneously in each turn.

Initdata: {

srand: random seed, integer

quan: prevalent wind, 0..3 for east/south/west/north

walltiles: a string represents initial tile wall, each tile split by space

}

Request and response:

See https://wiki.botzone.org.cn/index.php?title=Chinese-Standard-Mahjong/en#Bot_Input_and_Output

Episode Termination:

Some player makes Mahjong or a draw when tile wall runs out.

Score:

Basic score = 8;

Win by self drawn:

winner is (Basic score + Fan points) * 3;

loser is -(Basic score + Fan points);

Win by ignition:

winner is Basic score * 3 + Fan points;

loser of ignition: -(Basic score + Fan points);

other losers: -Basic score.

Draw:

0 for all.

'''

metadata = {'render.modes': ['ansi']}

def __init__(self, duplicate = True):

# Initialize configurations, possible viewers and state

self.agents = None

self.round = None

self.closed = False

self.initdata = {}

self._seed = None

self.display = []

self.viewer = None

self.duplicate = duplicate

@property

def player_num(self):

return 4

def reset(self, initdata = None):

if self.closed:

raise AlreadyClosed()

if self.agents is None:

raise AgentsNeeded()

# Initialize each agent

for agent in self.agents:

agent.reset()

# Initialize state for new episode

self.round = 0

if not initdata: initdata = {}

if 'srand' in initdata:

self.seed(initdata['srand'])

if self._seed:

random.seed(self._seed)

if 'quan' in initdata:

self.prevalentWind = initdata['quan']

else:

self.prevalentWind = random.randint(0, 3)

if 'walltiles' in initdata:

self.tileWall = initdata['walltiles'].split()

assert len(self.tileWall) == 136 if self.duplicate else 144

else:

self.tileWall = []

for j in range(4):

for i in range(1, 10):

self.tileWall.append('W' + str(i))

self.tileWall.append('B' + str(i))

self.tileWall.append('T' + str(i))

for i in range(1, 5):

self.tileWall.append('F' + str(i))

for i in range(1, 4):

self.tileWall.append('J' + str(i))

if not self.duplicate:

for i in range(1, 9):

self.tileWall.append('H' + str(i))

random.shuffle(self.tileWall)

self.initdata = dict(quan = self.prevalentWind, walltiles = self.tileWall)

self.display = [dict(quan = self.prevalentWind, action = 'INIT')]

if self.viewer: self.viewer.reset(self.initdata)

if self.duplicate:

self.tileWall = [self.tileWall[i * 34 : (i + 1) * 34] for i in range(4)]

self.shownTiles = defaultdict(int)

self.state = -1

self.requests = ['0 %d %d' % (seatWind, self.prevalentWind) for seatWind in range(4)]

GPT-4o

Poe

以下是逐行代码解析,用中文解释每一段代码的功能和意义:

导入库和模块

python

运行复制

from collections import defaultdict

from copy import deepcopy

import random

from collections import defaultdict: 从 Python 的 collections 模块导入 defaultdict,用于创建字典对象,其默认值类型可以指定,方便处理字典中未初始化的键。

from copy import deepcopy: 从 copy 模块导入 deepcopy,用于深拷贝对象,避免修改原对象。

import random: 导入 Python 内置的随机数模块,用于生成随机数或打乱列表。

python

运行复制

from botzone import *

from botzone.online.viewer.chinesestandardmahjong import ChineseStandardMahjongTextViewer

from botzone import *: 从 botzone 模块导入所有内容。botzone 是 Botzone 平台的专用模块,用于实现游戏环境。

from botzone.online.viewer.chinesestandardmahjong import ChineseStandardMahjongTextViewer: 从 botzone 的子模块导入 ChineseStandardMahjongTextViewer,这是一个用于显示国标麻将游戏状态的文本查看器。

python

运行复制

try:

from MahjongGB import MahjongFanCalculator

except:

print('MahjongGB library required! Please visit https://github.com/ailab-pku/PyMahjongGB for more information.')

raise

尝试导入 MahjongFanCalculator:

try 块尝试从 MahjongGB 模块导入 MahjongFanCalculator,这是一个用于计算国标麻将番数的库。

如果导入失败,抛出异常并输出错误信息,提示用户需要安装 MahjongGB 库以运行代码。

定义国标麻将游戏环境类

python

运行复制

class ChineseStandardMahjongEnv(Env):

定义一个继承自 Env 的类 ChineseStandardMahjongEnv,用于实现国标麻将的游戏环境。

类的文档说明

python

运行复制

'''

Description:

Mahjong using Chinese Standard Rule.

Rule: https://wiki.botzone.org.cn/index.php?title=Chinese-Standard-Mahjong/en

Decision order: Four players act simutaneously in each turn.

...

'''

文档注释:描述了类的功能、规则来源以及游戏的基本流程:

规则

相关风暴