鱼C论坛

 找回密码
 立即注册
查看: 1055|回复: 13

[已解决]用python写一个石头剪刀布的游戏

[复制链接]
发表于 2024-4-8 09:05:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
用python写一个石头剪刀布的游戏
最佳答案
2024-8-3 13:05:00
面向对象:
  1. class Player:
  2.     def game2(self):
  3.         score = 0# 积分
  4.         times = 0# 局数
  5.         import random
  6.         print('平局+0分,败局-1分,胜局+1分')
  7.         print('3局过后谁会赢呢?')
  8.         while times < 3:
  9.             computer = random.randint(1, 3)

  10.             n = input('1代表石头,2代表剪刀,3代表布 出一个吧:')

  11.             try:
  12.                 n = int(n)

  13.             except ValueError as e:
  14.                 print('您输入的不是1~3中的整数!')
  15.                 continue

  16.             right = (1, 2, 3)
  17.             while n not in right:
  18.                 if n not in right:
  19.                     print('您输入的不是1~3中的整数!')
  20.                     n = int(input('1代表石头,2代表剪刀,3代表布 出一个吧:'))
  21.                 continue

  22.             try:
  23.                 n = int(n)

  24.             except ValueError as a:
  25.                 print('您输入的不是1~3中的整数!')
  26.                 continue
  27.             else:
  28.                 n = int(n)

  29.             if computer == 1:
  30.                 if n == 1:
  31.                     times += 1
  32.                     score += 0
  33.                     print('程序和你都出石头,平局+0分','当前第%d局,当前积分%d'%(times,score),sep='\n')

  34.                 if n == 2:
  35.                     times += 1
  36.                     score -= 1
  37.                     print('程序出石头,你出的是剪刀,败局-1分','当前第%d局,当前积分%d'%(times,score),sep='\n')

  38.                 if n == 3:
  39.                     times += 1
  40.                     score += 1
  41.                     print('程序出石头,你出的是布,胜局+1分', '当前第%d局,当前积分%d' % (times, score), sep='\n')


  42.             elif computer == 2:
  43.                 if n == 1:
  44.                     times += 1
  45.                     score += 1
  46.                     print('程序出剪刀,你出石头,胜局+1分', '当前第%d局,当前积分%d' % (times, score), sep='\n')

  47.                 if n == 2:
  48.                     times += 1
  49.                     score += 0
  50.                     print('程序和你都出剪刀,平局+0分', '当前第%d局,当前积分%d' % (times, score), sep='\n')

  51.                 if n == 3:
  52.                     times += 1
  53.                     score -= 1
  54.                     print('程序出剪刀,你出的是布,败局-1分', '当前第%d局,当前积分%d' % (times, score), sep='\n')


  55.             else:
  56.                 if n == 1:
  57.                     times += 1
  58.                     score -= 1
  59.                     print('程序出布,你出石头,败局-1分', '当前第%d局,当前积分%d' % (times, score), sep='\n')

  60.                 if n == 2:
  61.                     times += 1
  62.                     score += 1
  63.                     print('程序出布,你出剪刀,胜局+1分', '当前第%d局,当前积分%d' % (times, score), sep='\n')

  64.                 if n == 3:
  65.                     times += 1
  66.                     score += 0
  67.                     print('程序和你都出布,平局+0分', '当前第%d局,当前积分%d' % (times, score), sep='\n')

  68.             if times < 3:
  69.                 print('开始下一局')
  70.         if score >= 1:
  71.             print('\n','你赢了!')
  72.         elif score == 0:
  73.             print('\n','你与程序平手')
  74.         else:
  75.             print('\n','你输了!真给人类丢脸')


  76. player = Player()
  77. player.game2()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-4-8 09:05:26 | 显示全部楼层
好的,下面是一个简单的石头剪刀布游戏的Python代码:

  1. import random

  2. def game():
  3.     choices = ['石头', '剪刀', '布']
  4.     computer_choice = random.choice(choices)
  5.     user_choice = input("请输入你的选择(石头/剪刀/布):")

  6.     if user_choice not in choices:
  7.         print("输入无效,请重新输入")
  8.         return

  9.     print("你选择了:" + user_choice)
  10.     print("计算机选择了:" + computer_choice)

  11.     if user_choice == computer_choice:
  12.         print("平局!")
  13.     elif (user_choice == '石头' and computer_choice == '剪刀') or (user_choice == '剪刀' and computer_choice == '布') or (user_choice == '布' and computer_choice == '石头'):
  14.         print("恭喜你,你赢了!")
  15.     else:
  16.         print("很遗憾,你输了。")

  17. game()
复制代码


在这个代码中,我们首先定义了一个`game`函数来表示游戏的逻辑。在每一局游戏开始时,计算机会随机选择石头、剪刀或布,并等待用户输入他们的选择。然后根据用户和计算机的选择进行比较,并输出胜负结果。

希望这个简单的代码能帮助你入门Python编程,享受游戏的乐趣!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-4-8 09:46:54 | 显示全部楼层
  1. import random   
  2. man_choice = input("请输入你的选择(石头/剪刀/布):")
  3. if man_choice == "石头":
  4.     man=1
  5. elif man_choice == "剪刀":
  6.     man=2
  7. elif man_choice == "布":
  8.     man=3
  9. choices = ['石头', '剪刀', '布']
  10. computer_choice=random.choice(choices)
  11. for choice in computer_choice:
  12.     if choice == "石头":
  13.         computer=1
  14.     elif choice == "剪刀":
  15.         computer=2
  16.     elif choice == "布":
  17.         computer=3
  18. print("电脑出的是:",computer_choice)
  19.       
  20. ##石头1 剪刀2 布3
  21. #12 13 21 23 31 32
  22. if computer==1 and man==2:
  23.     print("电脑赢")
  24. elif computer==1 and man==3:
  25.     print("我胜")
  26. elif computer==2 and man==1:
  27.     print("我胜")
  28. elif computer==2 and man==3:
  29.     print("电脑赢")
  30. elif computer==3 and man==1:
  31.     print("电脑赢")
  32. elif computer==3 and man==2:
  33.     print("我胜")
  34. else:
  35.     print("平局")
复制代码

哪里错了?要怎么改?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-4-8 09:47:13 | 显示全部楼层
chen` 发表于 2024-04-08 09:46
import random   
man_choice = input("请输入你的 ...


代码中有几个问题需要修正:
1. 在定义变量`choices`和`computer_choice`时,没有为`choices`赋值一个选项列表。你可以将它设置为`choices = ["石头", "剪刀", "布"]`。
2. 在for循环中,你不需要对`computer_choice`进行迭代,因为它只是一个字符串。你只需要根据`computer_choice`的值来确定`computer`的值即可。
3. 在判断胜负时,你需要考虑到平局的情况。可以先判断是否平局,再判断其他胜负情况。

以下是修正后的代码:

import random

choices = ["石头", "剪刀", "布"]
man_choice = input("请输入你的选择(石头/剪刀/布):")

if man_choice == "石头":
    man = 1
elif man_choice == "剪刀":
    man = 2
elif man_choice == "布":
    man = 3

computer_choice = random.choice(choices)
if computer_choice == "石头":
    computer = 1
elif computer_choice == "剪刀":
    computer = 2
elif computer_choice == "布":
    computer = 3

print("电脑出的是:", computer_choice)

if computer == man:
    print("平局")
elif (computer == 1 and man == 2) or (computer == 2 and man == 3) or (computer == 3 and man == 1):
    print("电脑赢")
else:
    print("我胜")

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-3 13:05:00 | 显示全部楼层    本楼为最佳答案   
面向对象:
  1. class Player:
  2.     def game2(self):
  3.         score = 0# 积分
  4.         times = 0# 局数
  5.         import random
  6.         print('平局+0分,败局-1分,胜局+1分')
  7.         print('3局过后谁会赢呢?')
  8.         while times < 3:
  9.             computer = random.randint(1, 3)

  10.             n = input('1代表石头,2代表剪刀,3代表布 出一个吧:')

  11.             try:
  12.                 n = int(n)

  13.             except ValueError as e:
  14.                 print('您输入的不是1~3中的整数!')
  15.                 continue

  16.             right = (1, 2, 3)
  17.             while n not in right:
  18.                 if n not in right:
  19.                     print('您输入的不是1~3中的整数!')
  20.                     n = int(input('1代表石头,2代表剪刀,3代表布 出一个吧:'))
  21.                 continue

  22.             try:
  23.                 n = int(n)

  24.             except ValueError as a:
  25.                 print('您输入的不是1~3中的整数!')
  26.                 continue
  27.             else:
  28.                 n = int(n)

  29.             if computer == 1:
  30.                 if n == 1:
  31.                     times += 1
  32.                     score += 0
  33.                     print('程序和你都出石头,平局+0分','当前第%d局,当前积分%d'%(times,score),sep='\n')

  34.                 if n == 2:
  35.                     times += 1
  36.                     score -= 1
  37.                     print('程序出石头,你出的是剪刀,败局-1分','当前第%d局,当前积分%d'%(times,score),sep='\n')

  38.                 if n == 3:
  39.                     times += 1
  40.                     score += 1
  41.                     print('程序出石头,你出的是布,胜局+1分', '当前第%d局,当前积分%d' % (times, score), sep='\n')


  42.             elif computer == 2:
  43.                 if n == 1:
  44.                     times += 1
  45.                     score += 1
  46.                     print('程序出剪刀,你出石头,胜局+1分', '当前第%d局,当前积分%d' % (times, score), sep='\n')

  47.                 if n == 2:
  48.                     times += 1
  49.                     score += 0
  50.                     print('程序和你都出剪刀,平局+0分', '当前第%d局,当前积分%d' % (times, score), sep='\n')

  51.                 if n == 3:
  52.                     times += 1
  53.                     score -= 1
  54.                     print('程序出剪刀,你出的是布,败局-1分', '当前第%d局,当前积分%d' % (times, score), sep='\n')


  55.             else:
  56.                 if n == 1:
  57.                     times += 1
  58.                     score -= 1
  59.                     print('程序出布,你出石头,败局-1分', '当前第%d局,当前积分%d' % (times, score), sep='\n')

  60.                 if n == 2:
  61.                     times += 1
  62.                     score += 1
  63.                     print('程序出布,你出剪刀,胜局+1分', '当前第%d局,当前积分%d' % (times, score), sep='\n')

  64.                 if n == 3:
  65.                     times += 1
  66.                     score += 0
  67.                     print('程序和你都出布,平局+0分', '当前第%d局,当前积分%d' % (times, score), sep='\n')

  68.             if times < 3:
  69.                 print('开始下一局')
  70.         if score >= 1:
  71.             print('\n','你赢了!')
  72.         elif score == 0:
  73.             print('\n','你与程序平手')
  74.         else:
  75.             print('\n','你输了!真给人类丢脸')


  76. player = Player()
  77. player.game2()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-7 17:48:48 | 显示全部楼层

大佬,膜拜一下,带小弟么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-7 18:21:56 | 显示全部楼层
Stubborn 发表于 2024-8-7 17:48
大佬,膜拜一下,带小弟么

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-8 13:16:49 | 显示全部楼层

加个联系方式,请教下五子棋的局面评估函数怎么写
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-8 15:16:49 | 显示全部楼层
Stubborn 发表于 2024-8-8 13:16
加个联系方式,请教下五子棋的局面评估函数怎么写

这个可不简单
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-8 15:34:27 | 显示全部楼层

你可以的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-8 15:34:37 | 显示全部楼层
本帖最后由 Stubborn 于 2024-8-8 15:40 编辑

  1. import random

  2. class Player:
  3.    
  4.     @staticmethod
  5.     def game2():
  6.         print('平局+0分,败局-1分,胜局+1分。\n3局过后谁会赢呢?')
  7.         time,score = 3,0
  8.         while True and time:
  9.             
  10.             
  11.             try:
  12.                 n = int(input('1代表石头,2代表剪刀,3代表布 出一个吧:'))
  13.                 if 0 > n or n > 3:
  14.                     continue
  15.             except:
  16.                 print('您输入的不是1~3中的整数!')
  17.                 continue

  18.             computer = random.randint(1, 3)
  19.             def compare(n, c):
  20.                 if [n, c] in ([1, 2], [2, 3], [3, 1]):
  21.                     return 1
  22.                 if [n, c] in ([1, 1], [2, 2], [3, 3]):
  23.                     return 0
  24.                 return -1

  25.             r = compare(n, computer)
  26.             rl = ['石头','剪刀','布']
  27.             el = ['平局','你赢了','你输了']
  28.             print('程序出{},你出的是{},{}{}分, 当前第{}局'.format(rl[n-1],rl[computer-1],el[r], r, abs(time-3)) )
  29.             time -= 1
  30.             score+= r

  31.             print('开始下一局')
  32.         

  33. Player.game2()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-8 15:51:15 | 显示全部楼层

我还真有一个五子棋案例。。。花钱买的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-8 15:57:41 | 显示全部楼层

你发个求助帖,我把五子棋算法给你
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-8 15:59:31 | 显示全部楼层
本帖最后由 Stubborn 于 2024-8-8 16:01 编辑
某一个“天” 发表于 2024-8-8 15:57
你发个求助帖,我把五子棋算法给你


,发了求助铁子
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-20 01:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表