Python|实现简单火焰游戏的程序
Python是一种多用途语言,几乎可以用它做任何事情。 Python也可用于游戏开发。让我们在不使用任何外部游戏库(如 PyGame)的情况下创建一个简单的 FLAMES 游戏。
火焰是一个流行的游戏,以首字母缩写命名:朋友,恋人,深情,婚姻,敌人,兄弟姐妹。该游戏无法准确预测某个人是否适合您,但与您的朋友一起玩会很有趣。
这个游戏有两个步骤:
- 取这两个名字。
- 删除常见字符及其各自的常见情况。
- 获取剩余字符数。
- 以 FLAMES 字母为 [“F”, “L”, “A”, “M”, “E”, “S”]
- 使用我们得到的计数开始删除字母。
- 最后一个过程的字母是结果。
例子 :
Input : player1 name : AJAY
player 2 name : PRIYA
Output : Relationship status : Friends
说明:在上面给定的两个名称 A 和 Y 是常见的字母,它们在两个名称中都出现一次(共同计数),因此我们从两个名称中删除这些字母。现在计算剩下的字母总数是 5。现在开始使用我们得到的计数从 FLAMES 中一个一个地删除字母,持续该过程的字母就是结果。
计数以逆时针循环方式进行。
FLAMES
counting is start from F, E is at 5th count so we remove E and start counting again but a this time start from S.
FLAMS
M is at 5th count so we remove M and counting start from S.
FLAS
S is at 5th count so we remove S and counting start from F.
FLA
L is at 5th count so we remove L and counting start from A.
FA
A is at 5th count so we remove A. now we have only one letter is remaining so this is the final answer.
F
So, the relationship is F i.e. Friends .
方法:将两个名称作为输入,然后删除它们各自常见的常见字符。为了删除的目的,我们创建了用户定义的 remove_match_char函数,它有两个参数作为 list1 和 list2 分别存储两个玩家名称的字符列表并返回连接列表的列表(list1 + “*” flagst2)和我们存储在 ret_list 变量中的标志值.去掉所有常见字符后,统计总数。剩下的字符然后创建一个带有 FLAMES 首字母缩略词的结果列表,即 [“朋友”、“爱”、“感情”、“婚姻”、“敌人”、“兄弟姐妹”]。现在开始一个一个地删除单词,直到列表不只包含一个单词,使用我们得到的总数。留在最后的词是结果。
下面是实现:
Python3
# function for removing common characters
# with their respective occurrences
def remove_match_char(list1, list2):
for i in range(len(list1)) :
for j in range(len(list2)) :
# if common character is found
# then remove that character
# and return list of concatenated
# list with True Flag
if list1[i] == list2[j] :
c = list1[i]
# remove character from the list
list1.remove(c)
list2.remove(c)
# concatenation of two list elements with *
# * is act as border mark here
list3 = list1 + ["*"] + list2
# return the concatenated list with True flag
return [list3, True]
# no common characters is found
# return the concatenated list with False flag
list3 = list1 + ["*"] + list2
return [list3, False]
# Driver code
if __name__ == "__main__" :
# take first name
p1 = input("Player 1 name : ")
# converted all letters into lower case
p1 = p1.lower()
# replace any space with empty string
p1.replace(" ", "")
# make a list of letters or characters
p1_list = list(p1)
# take 2nd name
p2 = input("Player 2 name : ")
p2 = p2.lower()
p2.replace(" ", "")
p2_list = list(p2)
# taking a flag as True initially
proceed = True
# keep calling remove_match_char function
# until common characters is found or
# keep looping until proceed flag is True
while proceed :
# function calling and store return value
ret_list = remove_match_char(p1_list, p2_list)
# take out concatenated list from return list
con_list = ret_list[0]
# take out flag value from return list
proceed = ret_list[1]
# find the index of "*" / border mark
star_index = con_list.index("*")
# list slicing perform
# all characters before * store in p1_list
p1_list = con_list[ : star_index]
# all characters after * store in p2_list
p2_list = con_list[star_index + 1 : ]
# count total remaining characters
count = len(p1_list) + len(p2_list)
# list of FLAMES acronym
result = ["Friends", "Love", "Affection", "Marriage", "Enemy", "Siblings"]
# keep looping until only one item
# is not remaining in the result list
while len(result) > 1 :
# store that index value from
# where we have to perform slicing.
split_index = (count % len(result) - 1)
# this steps is done for performing
# anticlock-wise circular fashion counting.
if split_index >= 0 :
# list slicing
right = result[split_index + 1 : ]
left = result[ : split_index]
# list concatenation
result = right + left
else :
result = result[ : len(result) - 1]
# print final result
print("Relationship status :", result[0])
输出:
Player 1 name : ANKIT
Player 2 name : DEEPIKA
Relationship status : Marriage