📜  使用Python的策划游戏

📅  最后修改于: 2022-05-13 01:55:49.128000             🧑  作者: Mango

使用Python的策划游戏

鉴于当代人对游戏的了解及其高度要求的技术,许多人都渴望追求进一步开发和推进游戏的想法。最终,每个人都从头开始。 Mastermind 是一款由两名玩家玩的古老的密码破解游戏。游戏可以追溯到 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 库的同时进一步开发它,将使它更像真正的交易,更不用说更多的参与了!