用python语言写一个猜拳游戏


题目:用基础python知识,写一个猜拳游戏.系统要求每次可以免费玩10次,有计分功能,赢一次得一分,平不得分,输一次扣一分.另外如果玩家输入的不是剪刀石头布这三种的话就会提示你重新输入,但不得超过三次机会,否则自动退出系统.
import random
a = "石头"
b = "剪刀"
c = "布"
count=0
score = 0
count1=0
while count<10:
    gesture1 = random.choice([a,b,c])
    gesture = input("请输入您的选择(只允许输入石头,剪刀,布):")
    if gesture not in [a,b,c]:
            print("你输入有误,请重新输入,你还有%d次机会输错!" % (2-count1))
            count1+=1
            if count1>=3:
                break
    else:
        print(gesture1)
        if (gesture == a and gesture1 == b) or (gesture == b and gesture1 == c) or (gesture == c and gesture1 == a):
                print("你赢了")
                score+=1
        elif gesture == gesture1:
                print("这是一个平局")
                score+=0
        else:
                print("你输了")
                score-=1
        count += 1
        print("你还可以免费玩%d次" % (10 - count))
        print("你当前得分为%d" % score)

if score>=1:
    print("你真是个天才")
elif score<0:
    print("你这个蠢货")

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,我可以提供一个简单的猜拳智能游戏的示例代码,基于 TensorFlow 和 Python 实现。以下是代码和注释: ```python import tensorflow as tf import random # 定义猜拳手势对应的数字 rock = 0 paper = 1 scissors = 2 # 定义手势名称,用于展示结果 gesture_names = ['Rock', 'Paper', 'Scissors'] # 定义训练数据,手势对应的数字 training_data = [ [rock, rock], [rock, paper], [rock, scissors], [paper, rock], [paper, paper], [paper, scissors], [scissors, rock], [scissors, paper], [scissors, scissors] ] # 定义训练目标,0 表示平局,1 表示胜利,-1 表示失败 target_data = [ 0, -1, 1, 1, 0, -1, -1, 1, 0 ] # 定义神经网络模型 model = tf.keras.Sequential([ tf.keras.layers.Dense(16, input_shape=(2,), activation='relu'), tf.keras.layers.Dense(3, activation='softmax') ]) # 编译模型,使用交叉熵作为损失函数,使用 Adam 优化器 model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # 将训练数据和目标数据转换为张量 training_data = tf.constant(training_data) target_data = tf.constant(target_data) # 将目标数据转换为 one-hot 编码 target_data = tf.one_hot(target_data + 1, depth=3) # 训练模型 model.fit(training_data, target_data, epochs=1000) # 循环进行游戏 while True: # 让用户输入猜拳手势 user_gesture = input('Enter your gesture (0 for rock, 1 for paper, 2 for scissors): ') # 将用户输入转换为数字 user_gesture = int(user_gesture) # 计算模型的预测结果 prediction = model.predict([[user_gesture, random.randint(0, 2)]]) # 获取预测结果中概率最大的手势 computer_gesture = tf.argmax(prediction, axis=1).numpy()[0] # 打印出用户和计算机的手势 print('User:', gesture_names[user_gesture]) print('Computer:', gesture_names[computer_gesture]) # 判断胜负 if user_gesture == computer_gesture: print('Draw!') elif (user_gesture == rock and computer_gesture == scissors) or (user_gesture == paper and computer_gesture == rock) or (user_gesture == scissors and computer_gesture == paper): print('You win!') else: print('You lose!') ``` 这个程序使用了 TensorFlow 实现了一个简单的神经网络模型,用于预测计算机出拳的手势。程序的执行流程如下: 1. 定义猜拳手势对应的数字,以及手势名称。 2. 定义训练数据和目标数据,用于训练神经网络模型。 3. 定义神经网络模型,并编译模型。 4. 训练神经网络模型。 5. 循环进行游戏,让用户输入猜拳手势,计算机根据神经网络模型预测出拳手势,判断胜负,并输出结果。 注意,这个示例代码只是一个非常简单的猜拳游戏实现,可能存在不足和改进空间,仅供参考。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小番茄 bhl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值