以下是Python代码实现:
import random
定义游戏规则
rules =
初始化游戏数据
rounds = 5
game_count = 0
win_count = {‘rock’: 0, ‘scissors’: 0, ‘paper’: 0}
lose_count = {‘rock’: 0, ‘scissors’: 0, ‘paper’: 0}
draw_count = {‘rock’: 0, ‘scissors’: 0, ‘paper’: 0}
history = []
开始游戏
print(‘Welcome to the Rock-Paper-Scissors game!’)
for i in range(rounds):
print(f’
Round {i+1}:’)
while True:
# 玩家出拳
player_choice = input(‘Please choose rock, paper or scissors: ‘)
if player_choice in rules:
break
else:
print(‘Invalid input, please try again.’)
# 电脑出拳
computer_choice = random.choice(list(rules.keys()))
# 判断胜负
if rules[player_choice] == computer_choice:
print(f’You win! {player_choice} beats {computer_choice}.’)
win_count[player_choice] += 1
lose_count[computer_choice] += 1
history.append(player_choice)
break
elif rules[computer_choice] == player_choice:
print(f’You lose! {computer_choice} beats {player_choice}.’)
lose_count[player_choice] += 1
win_count[computer_choice] += 1
history.append(player_choice)
break
else:
print(f’Draw! Both choose {player_choice}.’)
draw_count[player_choice] += 1
draw_count[computer_choice] += 1
history.append(player_choice)
game_count += 1
输出游戏结果
print(’
Game over!’)
print(f’Total game count: {game_count}’)
print(f’Win count: {win_count}’)
print(f’Lose count: {lose_count}’)
print(f’Draw count: {draw_count}’)
统计胜率
win_rate = {‘rock’: 0, ‘scissors’: 0, ‘paper’: 0}
for key in win_count:
win_rate[key] = win_count[key] / game_count
print(f’Win rate: {win_rate}’)