热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用Python策划游戏

使用Python策划游戏原文:https://www.gee

使用 Python 策划游戏

原文:https://www.geeksforgeeks.org/mastermind-game-using-python/

鉴于当代人对游戏及其高要求技术的了解,许多人渴望进一步发展和推进游戏。最终,每个人都要从头开始。摄魂师是一款由两个玩家玩的破密码老游戏。这个游戏可以追溯到 19 世纪,可以用纸和铅笔玩。

先决条件:
Python 中的随机数

游戏规则

两个玩家互相进行游戏;让我们假设玩家 1 和玩家 2。


  • 玩家 1 通过设置多位数先玩。

  • 玩家 2 现在尝试第一次猜数字。

  • 如果玩家 2 第一次尝试成功(尽管几率极小),他就赢得了游戏,并加冕为摄魂师!如果不是,那么玩家 1 通过显示玩家 2 得到的数字或数字来提示。

  • 游戏继续进行,直到玩家 2 最终能够完全猜出数字。

  • 现在玩家 2 开始设置数字,玩家 1 扮演猜数字的角色。

  • 如果玩家 1 能够在比玩家 2 更少的尝试次数内猜出数字,则玩家 1 赢得游戏并加冕为摄魂师。

  • 如果没有,那么玩家 2 赢得游戏。

  • 然而,真正的游戏已经证明了美学,因为数字是由彩色编码的按钮表示的。

例如:
输入:

Player 1, set the number: 5672
Player 2, guess the number: 1472

输出:

Not quite the number. You did get 2 digits correct.
X X 7 2
Enter your next choice of numbers:

我们将不使用任何Pygame库来帮助我们获得额外的图形,因此将只处理框架和概念。此外,我们将与计算机对战,也就是说,计算机将生成要猜测的数字。

以下是上述想法的实现。

import random
# the .randrange() function generates a
# random number within the specified range.
num = random.randrange(1000, 10000)  
n = int(input("Guess the 4 digit number:"))
# condition to test equality of the
# guess made. Program terminates if true.
if (n == num):  
    print("Great! You guessed the number in just 1 try! You're a Mastermind!")
else:
    # ctr variable initialized. It will keep count of 
    # the number of tries the Player takes to guess the number.
    ctr = 0  
    # while loop repeats as long as the 
    # Player fails to guess the number correctly.
    while (n != num):  
        # variable increments every time the loop
        # is executed, giving an idea of how many
        # guesses were made.
        ctr += 1  
        count = 0
        # explicit type conversion of an integer to
        # a string in order to ease extraction of digits
        n = str(n)  
        # explicit type conversion of a string to an integer
        num = str(num)  
        # correct[] list stores digits which are correct
        correct = ['X']*4  
        # for loop runs 4 times since the number has 4 digits.
        for i in range(0, 4): 
             # checking for equality of digits
            if (n[i] == num[i]):  
                # number of digits guessed correctly increments
                count += 1  
                # hence, the digit is stored in correct[].
                correct[i] = n[i]  
            else:
                continue
        # when not all the digits are guessed correctly.
        if (count <4) and (count != 0):  
            print("Not quite the number. But you did get ", count, " digit(s) correct!")
            print("Also these numbers in your input were correct.")
            for k in correct:
                print(k, end=' ')
            print('\n')
            print('\n')
            n = int(input("Enter your next choice of numbers: "))
        # when none of the digits are guessed correctly.
        elif (count == 0):  
            print("None of the numbers in your input match.")
            n = int(input("Enter your next choice of numbers: "))
    # condition for equality.
    if n == num:  
        print("You've become a Mastermind!")
        print("It took you only", ctr, "tries.")

让我们假设计算机设定的数字是 1564

输出:

Guess the 4 digit number: 1564
Great! You guessed the number in just 1 try! You're a Mastermind!

如果这个数字一次都猜不到。

输出:

Guess the 4 digit number: 2164
Not quite the number. But you did get 2 digit(s) correct!
Also these numbers in your input were correct.
X X 6 4
Enter your next choice of numbers: 3564
Not quite the number. But you did get 2 digit(s) correct!
Also these numbers in your input were correct.
X 5 6 4
Enter your next choice of numbers: 1564
You've become a Mastermind.
It took you only 3 tries.

您可以通过增加输入的位数或不透露输入中哪些数字被正确放置来增加游戏难度。
这已经在下面的代码中解释过了。

import random
#the .randrange() function generates
# a random number within the specified range.
num = random.randrange(1000,10000) 
n = int(input("Guess the 4 digit number:"))
# condition to test equality of the 
# guess made. Program terminates if true.
if(n == num):             
     print("Great! You guessed the number in just 1 try! You're a Mastermind!")
else:
     # ctr variable initialized. It will keep count of 
     # the number of tries the Player takes to guess the number.
     ctr = 0    
     # while loop repeats as long as the Player
     # fails to guess the number correctly.
     while(n!=num):
          # variable increments every time the loop 
          # is executed, giving an idea of how many 
          # guesses were made.
          ctr += 1             
          count = 0
          # explicit type conversion of an integer to 
          # a string in order to ease extraction of digits
          n = str(n) 
          # explicit type conversion of a string to an integer                                 
          num = str(num)
          # correct[] list stores digits which are correct 
          correct=[]        
          # for loop runs 4 times since the number has 4 digits.     
          for i in range(0,4): 
              # checking for equality of digits
              if(n[i] == num[i]): 
                  # number of digits guessed correctly increments
                  count += 1    
                  # hence, the digit is stored in correct[].
                  correct.append(n[i])     
              else:
                  continue
          # when not all the digits are guessed correctly.
          if (count <4) and (count != 0):     
              print("Not quite the number. But you did get ",count," digit(s) correct!")
              print("Also these numbers in your input were correct.")
              for k in correct:
                  print(k, end=' ')
              print('\n')
              print('\n')
              n = int(input("Enter your next choice of numbers: "))
          # when none of the digits are guessed correctly.
          elif(count == 0):         
              print("None of the numbers in your input match.")
              n=int(input("Enter your next choice of numbers: ")) 
     if n==num:                
         print("You've become a Mastermind!")
         print("It took you only",ctr,"tries.")

假设计算机设定的数字是 54876。

输出:

Guess the 5 digit number: 38476
Not quite the number. But you did get 2 digit(s) correct!
Enter your next choice of numbers: 41876
Not quite the number. But you did get 4 digit(s) correct!
Enter the next choice of numbers: 54876
Great you've become a Mastermind!
It took you only 3 tries!

修改这段代码的整个范围是巨大的。这里的想法是了解这个概念是什么。像这种依赖类似基本代码的游戏还有很多。

通过利用这段代码,进一步开发它,同时结合 Pygame 的库,将使它更像真正的交易,更不用说涉及更多了!


推荐阅读
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 【shell】网络处理:判断IP是否在网段、两个ip是否同网段、IP地址范围、网段包含关系
    本文介绍了使用shell脚本判断IP是否在同一网段、判断IP地址是否在某个范围内、计算IP地址范围、判断网段之间的包含关系的方法和原理。通过对IP和掩码进行与计算,可以判断两个IP是否在同一网段。同时,还提供了一段用于验证IP地址的正则表达式和判断特殊IP地址的方法。 ... [详细]
  • 学习Java异常处理之throws之抛出并捕获异常(9)
    任务描述本关任务:在main方法之外创建任意一个方法接收给定的两个字符串,把第二个字符串的长度减1生成一个整数值,输出第一个字符串长度是 ... [详细]
  • iOS超签签名服务器搭建及其优劣势
    本文介绍了搭建iOS超签签名服务器的原因和优势,包括不掉签、用户可以直接安装不需要信任、体验好等。同时也提到了超签的劣势,即一个证书只能安装100个,成本较高。文章还详细介绍了超签的实现原理,包括用户请求服务器安装mobileconfig文件、服务器调用苹果接口添加udid等步骤。最后,还提到了生成mobileconfig文件和导出AppleWorldwideDeveloperRelationsCertificationAuthority证书的方法。 ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • 图像因存在错误而无法显示 ... [详细]
  • Android自定义控件绘图篇之Paint函数大汇总
    本文介绍了Android自定义控件绘图篇中的Paint函数大汇总,包括重置画笔、设置颜色、设置透明度、设置样式、设置宽度、设置抗锯齿等功能。通过学习这些函数,可以更好地掌握Paint的用法。 ... [详细]
author-avatar
手机用户2502870457
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有