石头剪刀布小游戏开发

文章目录

学校:郑州升达经贸管理学院
班级:23级计科本5班

小组成员:

202305050543 亓博研

202305050520 刘棕义

202305050528 孙焯

一、作业背景

本次作业要求2-3人一组,使用代码自动生成工具完成结对编程,我们3人小组选择开发"石头剪刀布小游戏",并记录协作过程。

二、工具选择

我们使用的代码生成工具是 C h a t G P T ( G P T − 4 ) ChatGPT(GPT-4) ChatGPT(GPT−4),它支持自然语言需求输入,能快速生成代码并实时优化功能,适配小组协作的高效需求。

三、三人分工

刘棕义:负责梳理需求、向工具提交功能描述,对接代码生成环节

孙焯:负责代码调试、测试异常场景(如输入无效选项、边界情况)

亓博研:负责优化交互体验、整理演示截图、撰写博客内容

四、开发过程

1.需求梳理与工具输入

刘棕义牵头整理游戏需求,统一向ChatGPT提交:

"请用Python写一个石头剪刀布小游戏:

玩家通过输入选择石头、剪刀或布

电脑随机选择石头、剪刀或布

判断胜负并显示结果(赢、输、平局)

统计并显示玩家获胜次数、失败次数、平局次数

游戏结束后支持重新开始

处理无效输入的异常提示"

2.代码生成与初步运行

ChatGPT返回初始代码后,小组在Python环境中测试基础功能,确认核心逻辑正常运行。

3.分工优化与调试

孙焯测试发现:输入非数字或不在有效范围内的选项时,提示语可以更明确,于是补充需求让工具添加相应的提示

亓博研提出优化:优化交互语气,添加表情符号和更生动的提示语,让游戏体验更轻松有趣

五、代码展示

python 复制代码
import random

class RockPaperScissorsGame:
    def __init__(self):
        self.choices = {
            '1': '石头',
            '2': '剪刀', 
            '3': '布'
        }
        self.win_cases = {
            '石头': '剪刀',  # 石头赢剪刀
            '剪刀': '布',    # 剪刀赢布  
            '布': '石头'     # 布赢石头
        }
        self.stats = {
            'wins': 0,
            'losses': 0,
            'draws': 0
        }
    
    def display_rules(self):
        print("🎮 欢迎来到石头剪刀布游戏!")
        print("=" * 40)
        print("游戏规则:")
        print("  石头(1) → 剪刀(2)")
        print("  剪刀(2) → 布(3)") 
        print("  布(3) → 石头(1)")
        print("=" * 40)
    
    def get_user_choice(self):
        while True:
            print("\n请选择:")
            print("  1 - 石头 ✊")
            print("  2 - 剪刀 ✌️") 
            print("  3 - 布 ✋")
            
            user_input = input("请输入你的选择(1/2/3):").strip()
            
            if user_input in self.choices:
                return user_input, self.choices[user_input]
            else:
                print("❌ 输入无效!请输入1、2或3~")
    
    def get_computer_choice(self):
        choice_num = random.choice(['1', '2', '3'])
        return choice_num, self.choices[choice_num]
    
    def determine_winner(self, user_choice, computer_choice):
        if user_choice == computer_choice:
            return "draw"
        elif self.win_cases[user_choice] == computer_choice:
            return "win"
        else:
            return "lose"
    
    def display_result(self, user_choice, computer_choice, result):
        print(f"\n你的选择:{user_choice} {'✊' if user_choice=='石头' else '✌️' if user_choice=='剪刀' else '✋'}")
        print(f"电脑选择:{computer_choice} {'✊' if computer_choice=='石头' else '✌️' if computer_choice=='剪刀' else '✋'}")
        
        if result == "win":
            print("🎉 恭喜你赢了!太棒了!")
            self.stats['wins'] += 1
        elif result == "lose":
            print("💻 电脑赢了,再接再厉!")
            self.stats['losses'] += 1
        else:
            print("🤝 平局!心有灵犀呀~")
            self.stats['draws'] += 1
    
    def display_stats(self):
        print(f"\n📊 当前战绩:")
        print(f"  胜场:{self.stats['wins']} 🏆")
        print(f"  负场:{self.stats['losses']} 📉") 
        print(f"  平局:{self.stats['draws']} 🤝")
        total = self.stats['wins'] + self.stats['losses'] + self.stats['draws']
        if total > 0:
            win_rate = (self.stats['wins'] / total) * 100
            print(f"  胜率:{win_rate:.1f}% ⭐")
    
    def play_round(self):
        user_num, user_choice = self.get_user_choice()
        computer_num, computer_choice = self.get_computer_choice()
        
        result = self.determine_winner(user_choice, computer_choice)
        self.display_result(user_choice, computer_choice, result)
        self.display_stats()
    
    def reset_stats(self):
        self.stats = {'wins': 0, 'losses': 0, 'draws': 0}
        print("\n🔄 战绩已重置!重新开始记录~")

def main():
    game = RockPaperScissorsGame()
    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)![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/39af3888edd840f09e009751f92e4402.png)


if __name__ == "__main__":
    main()

代码调试

相关推荐
程序员爱钓鱼6 小时前
Python 综合项目实战:学生成绩管理系统(命令行版)
后端·python·ipython
Brsentibi6 小时前
基于python代码自动生成关于建筑安全检测的报告
python·microsoft
程序员爱钓鱼6 小时前
REST API 与前后端交互:让应用真正跑起来
后端·python·ipython
gCode Teacher 格码致知8 小时前
Python基础教学:Python的openpyxl和python-docx模块结合Excel和Word模板进行数据写入-由Deepseek产生
python·excel
Destiny_where10 小时前
Agent平台-RAGFlow(2)-源码安装
python·ai
molunnnn10 小时前
第四章 Agent的几种经典范式
开发语言·python
linuxxx11011 小时前
django测试缓存命令的解读
python·缓存·django
毕设源码-邱学长13 小时前
【开题答辩全过程】以 基于Python的Bilibili平台数据分析与可视化实现为例,包含答辩的问题和答案
开发语言·python·数据分析