Compare commits

...

2 Commits

Author SHA1 Message Date
42a6320ce8 Update test_scoring.py 2024-11-30 17:35:56 +08:00
c041963b97 Update scoring.py 2024-11-30 17:35:53 +08:00
2 changed files with 31 additions and 104 deletions

View File

@@ -1,52 +1,32 @@
def calculate_score(fan: int, base_score: int, is_self_draw: bool, is_dealer: bool) -> dict:
def calculate_score(fan: int, base_score: int, is_self_draw: bool) -> dict:
"""
根据规则计算得分。
根据成都麻将规则计算得分(不区分庄家和闲家)
参数:
- fan: 总番数。
- base_score: 底分。
- is_self_draw: 是否为自摸。
- is_dealer: 是否为庄家。
返回:
- scores: 字典,包含赢家得分和输家扣分。
"""
# 翻倍计算基础
total_score = base_score * (2 ** fan)
# 计算总
multiplier = 2 ** fan # 根据番数计算倍率
total_score = base_score * multiplier
if is_self_draw:
# 自摸,其他三家平摊
if is_dealer:
# 庄家自摸:每家付单倍总
per_loser_score = -total_score
winner_score = total_score * 3 # 三家支付的总和
return {
"winner": winner_score,
"loser": [per_loser_score] * 3
}
else:
# 闲家自摸:庄家付双倍,其他两家付单倍
dealer_loss = -total_score * 2
other_loss = -total_score
winner_score = -dealer_loss + 2 * -other_loss # 庄家和两闲家支付总和
return {
"winner": winner_score,
"loser": [dealer_loss, other_loss, other_loss]
}
# 自摸三家平摊分数
per_loser_score = -total_score
winner_score = total_score * 3 # 总赢家得
return {
"winner": winner_score,
"loser": [per_loser_score] * 3
}
else:
# 点炮点炮者独付
# 点炮点炮者独付
loser_score = -total_score
winner_score = total_score
if is_dealer:
# 庄家点炮
return {
"winner": winner_score,
"loser": [loser_score, 0, 0]
}
else:
# 闲家点炮
return {
"winner": winner_score,
"loser": [loser_score, 0, 0]
}
return {
"winner": winner_score,
"loser": [loser_score, 0, 0]
}

View File

@@ -1,72 +1,19 @@
import pytest
from src.engine.scoring import calculate_score
@pytest.mark.parametrize("fan, is_self_draw, base_score, expected_scores", [
# 测试用例 1: 自摸,总番数 3
(3, True, 5, {"winner": 120, "loser": [-40, -40, -40]}),
def test_dealer_self_draw():
"""
测试用例 1: 庄家自摸,总番数 3底分 10
"""
fan = 3
base_score = 10
is_self_draw = True
is_dealer = True
# 测试用例 2: 点炮,总番数 2
(2, False, 5, {"winner": 20, "loser": [-20, 0, 0]}),
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}"
# 测试用例 3: 自摸,总番数 4
(4, True, 5, {"winner": 240, "loser": [-80, -80, -80]}),
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: 庄家点炮通过!")
# 测试用例 4: 点炮,总番数 1
(1, False, 5, {"winner": 10, "loser": [-10, 0, 0]}),
])
def test_calculate_score(fan, is_self_draw, base_score, expected_scores):
scores = calculate_score(fan, base_score, is_self_draw)
assert scores == expected_scores, f"测试失败: {scores} != {expected_scores}"