一,作业背景
本次作业要求 2-3 人一组,使用代码自动生成工具完成结对编
程,我们小组选择开发 “猜投硬币小游戏”,并记录协作过程。
二,工具选择
我们使用的代码生成工具是 ChatGPT(GPT-4),它支持自然语
言需求输入,能快速生成代码并实时优化功能,适配小组协作
的高效需求。
三,三人分工
张子卓 :负责梳理需求、向工具提交功能描述,对接代码生
成环节
陈旭 :负责代码调试、测试异常场景(如输入无效选项、边
界情况)
四,开发过程
1,需求梳理与工具输入
张子卓牵头整理游戏需求,统一向 ChatGPT 提交:“请用
Python 写一个猜投硬币小游戏:玩家通过输入选择正面或反面
电脑随机生成正面或反面结果判断猜对与否并显示结果统计并
显示玩家猜对次数、猜错次数游戏结束后支持重新开始处理无
效输入的异常提示”
2,代码生成与初步运行
ChatGPT 返回初始代码后,小组在 Python 环境中测试基础功

能,确认核心逻辑正常运行。
3,分工优化与调试
陈旭测试发现,输入非指定选项时提示不够清晰,补充需求让
工具优化提示语。优化整理员提出优化,添加表情符号和趣味
化提示,提升游戏交互的生动性。
import random
class CoinTossGame:
def __init__(self):
self.choices = {
'1': '正面',
'2': '反面'
}
self.stats = {
'correct': 0,
'wrong': 0
}
def display_rules(self):
print("🪙 欢迎来到猜投硬币小游戏!")
print("=" * 40)

print("游戏规则:")
print(" 选择正面或反面,猜对即获胜")
print(" 电脑将随机抛出硬币,揭晓结果")
print("=" * 40)
def get_user_choice(self):
while True:
print("\n 请选择:")
print(" 1 - 🪙正面 ")
print(" 2 - 反面 ⚫")
user_input = input("请输入你的选择(1/2):").strip()
if user_input in self.choices:
return self.choices[user_input]
else:
print("❌ 输入无效!请输入 1(正面)或 2(反面)~")
def get_computer_result(self):
return random.choice(['正面', '反面'])
def determine_result(self, user_choice, computer_result):
if user_choice == computer_result:

return "correct"
else:
return "wrong"
def display_result(self, user_choice, computer_result, result):
print(f"\n 你的猜测:{user_choice} {'🪙' if user_choice=='正面' else ' '}")⚫
print(f"硬币结果:{computer_result} {'🪙' if computer_result=='正面' else
' '}")⚫
if result == "correct":
print("🎉 恭喜你猜对啦!运气爆棚~")
self.stats['correct'] += 1
else:
print("😯 猜错啦,下次一定中!")
self.stats['wrong'] += 1
def display_stats(self):
print(f"\n📊 当前战绩:")
print(f" 猜对:{self.stats['correct']} ")🏆
print(f" 猜错:{self.stats['wrong']} ")❌
total = self.stats['correct'] + self.stats['wrong']
if total > 0:

correct_rate = (self.stats['correct'] / total) * 100
print(f" 正确率:{correct_rate:.1f}% ")⭐
def play_round(self):
user_choice = self.get_user_choice()
computer_result = self.get_computer_result()
result = self.determine_result(user_choice, computer_result)
self.display_result(user_choice, computer_result, result)
self.display_stats()
def reset_stats(self):
self.stats = {'correct': 0, 'wrong': 0}
print("\n🔄 战绩已重置!重新开始挑战~")
def main():
game = CoinTossGame()
game.display_rules()
while True:
game.play_round()
while True:

continue_choice = input("\n🔄 是否继续游戏?(y-继续, n-重新开始, q-
退出):").strip().lower()
if continue_choice in ['y', 'n', 'q']:
break
print("❌ 请输入 y, n 或 q~")
if continue_choice == 'q':
print("\n👋 游戏结束,下次再来挑战运气吧!")
break
elif continue_choice == 'n':
game.reset_stats()
print("-" * 50)
if __name__ == "__main__":
main()

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐