Compare commits

...

2 Commits

Author SHA1 Message Date
d8a333978b 1
1
2024-11-30 13:28:59 +08:00
3d22b695e6 Create actions.py 2024-11-30 13:28:56 +08:00
4 changed files with 49 additions and 0 deletions

13
src/engine/actions.py Normal file
View File

@@ -0,0 +1,13 @@
def draw_tile(state):
if state.remaining_tiles == 0:
raise ValueError("牌堆已空")
tile = state.deck.pop(0)
state.remaining_tiles -= 1
state.hands[state.current_player][tile] += 1
return tile
def discard_tile(state, tile):
if state.hands[state.current_player][tile] == 0:
raise ValueError("玩家没有这张牌")
state.hands[state.current_player][tile] -= 1
state.discards[state.current_player].append(tile)

16
src/engine/game_state.py Normal file
View File

@@ -0,0 +1,16 @@
class ChengduMahjongState:
def __init__(self):
# 每个玩家的手牌
self.hands = [[0] * 108 for _ in range(4)] # 每个玩家108张牌的计数
# 每个玩家的打出的牌
self.discards = [[] for _ in range(4)] # 每个玩家的弃牌列表
# 每个玩家的明牌(碰、杠)
self.melds = [[] for _ in range(4)]
# 剩余的牌堆
self.deck = list(range(108)) # 0-107 表示108张牌
# 当前玩家索引
self.current_player = 0
# 剩余牌数量
self.remaining_tiles = 108
# 胜利玩家列表
self.winners = []

13
src/engine/scoring.py Normal file
View File

@@ -0,0 +1,13 @@
def can_win(hand):
# 判断是否满足胡牌条件
...
def calculate_fan(hand, melds, is_self_draw, is_cleared):
fan = 1 # 基本胡
if is_cleared:
fan += 2 # 清一色
if len(melds) >= 2:
fan += len(melds)
if is_self_draw:
fan += 1
return fan

7
src/engine/utils.py Normal file
View File

@@ -0,0 +1,7 @@
def get_suit(tile_index):
"""
根据牌的索引返回花色。
条:索引 0-35索引 36-71索引 72-107
"""
suits = ["", "", ""]
return suits[tile_index // 36]