1
pull/1/head
wsy182 2024-11-30 17:08:43 +08:00
parent 349a2ff088
commit 4dbbd583b9
2 changed files with 77 additions and 7 deletions

View File

@ -1,6 +1,6 @@
def calculate_score(fan: int, base_score: int, is_self_draw: bool, is_dealer: bool) -> dict:
"""
根据番数和底分计算总
根据规则计算得
参数:
- fan: 总番数
@ -9,17 +9,15 @@ def calculate_score(fan: int, base_score: int, is_self_draw: bool, is_dealer: bo
- is_dealer: 是否为庄家
返回:
- scores: 一个字典包含所有玩家的得分
- "winner": 胜利者的得分正数
- "loser": 输家们的得分负数
- scores: 字典包含赢家得分和输家扣分
"""
# 计算总分 = 底分 * (2 ** 番数)
# 翻倍计算总分
total_score = base_score * (2 ** fan)
if is_self_draw:
# 自摸,其他三家平摊
if is_dealer:
# 庄家自摸,三家平摊且输家每人付总分
# 庄家自摸:每家付总分
loser_score = -total_score
winner_score = total_score * 3
return {
@ -27,7 +25,7 @@ def calculate_score(fan: int, base_score: int, is_self_draw: bool, is_dealer: bo
"loser": [loser_score] * 3
}
else:
# 闲家自摸庄家付双倍,其他两家付单倍
# 闲家自摸庄家付双倍,其他两家付单倍
dealer_loss = -total_score * 2
other_loss = -total_score
winner_score = total_score * 4

72
tests/test_scoring.py Normal file
View File

@ -0,0 +1,72 @@
from src.engine.scoring import calculate_score
def test_dealer_self_draw():
"""
测试用例 1: 庄家自摸总番数 3底分 10
"""
fan = 3
base_score = 10
is_self_draw = True
is_dealer = True
scores = calculate_score(fan, base_score, is_self_draw, is_dealer)
expected_scores = {"winner": 240, "loser": [-80, -80, -80]}
assert scores == expected_scores, f"庄家自摸测试失败: {scores} != {expected_scores}"
def test_non_dealer_point_win():
"""
测试用例 2: 闲家点炮总番数 2底分 10
"""
fan = 2
base_score = 10
is_self_draw = False
is_dealer = False
scores = calculate_score(fan, base_score, is_self_draw, is_dealer)
expected_scores = {"winner": 40, "loser": [-40, 0, 0]}
assert scores == expected_scores, f"闲家点炮测试失败: {scores} != {expected_scores}"
def test_non_dealer_self_draw():
"""
测试用例 3: 闲家自摸总番数 4底分 10
"""
fan = 4
base_score = 10
is_self_draw = True
is_dealer = False
scores = calculate_score(fan, base_score, is_self_draw, is_dealer)
expected_scores = {"winner": 160, "loser": [-80, -40, -40]}
assert scores == expected_scores, f"闲家自摸测试失败: {scores} != {expected_scores}"
def test_dealer_point_win():
"""
测试用例 4: 庄家点炮总番数 1底分 5
"""
fan = 1
base_score = 5
is_self_draw = False
is_dealer = True
scores = calculate_score(fan, base_score, is_self_draw, is_dealer)
expected_scores = {"winner": 10, "loser": [-10, 0, 0]}
assert scores == expected_scores, f"庄家点炮测试失败: {scores} != {expected_scores}"
if __name__ == "__main__":
# 单独执行测试
test_dealer_self_draw()
print("测试 1: 庄家自摸通过!")
test_non_dealer_point_win()
print("测试 2: 闲家点炮通过!")
test_non_dealer_self_draw()
print("测试 3: 闲家自摸通过!")
test_dealer_point_win()
print("测试 4: 庄家点炮通过!")