Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python | Pokémon Training Game
Next article icon

Python is a multipurpose language and one can do literally anything with it. Python can also be used for game development. Let’s create a simple FLAMES game without using any external game libraries like PyGame.

FLAMESis a popular game named after the acronym: Friends, Lovers, Affectionate, Marriage, Enemies, Sibling. This game does not accurately predict whether or not an individual is right for you, but it can be fun to play this with your friends.

There are two steps in this game:

Example : 

Input :   player1 name : AJAY          player 2 name : PRIYAOutput : Relationship status : Friends

Explanation:In above given two names A and Y are common letters which are occurring one time(common count) in both names so we are removing these letters from both names. Now count the total letters that are left here it is 5. Now start removing letters one by one from FLAMES using the count we got and the letter which lasts the process is the result.

Counting is done in anti-clockwise circular fashion.

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. 

So, the relationship is F i.e. Friends .

Approach: Take two names as input then remove the common characters with their respective common occurrences. For removing purpose we create a user-defined remove_match_char function with two arguments as list1 and list2 which stores list of characters of two players name respectively and return list of concatenated list(list1 + "*" flagst2) and flag value which we store in ret_list variable.After removing all the common characters, count the total no. of remaining characters then create a result list with FLAMES acronym i.e ["Friends", "Love", "Affection", "Marriage", "Enemy", "Siblings"]. Now start removing word one by one until list does not contain only one word, using the total count which we got. the word which remains in the last, is the result.

Below is the implementation:

Python3
# function for removing common characters# with their respective occurrencesdefremove_match_char(list1,list2):foriinrange(len(list1)):forjinrange(len(list2)):# if common character is found# then remove that character# and return list of concatenated# list with True Flagiflist1[i]==list2[j]:c=list1[i]# remove character from the listlist1.remove(c)list2.remove(c)# concatenation of two list elements with *# * is act as border mark herelist3=list1+["*"]+list2# return the concatenated list with True flagreturn[list3,True]# no common characters is found# return the concatenated list with False flaglist3=list1+["*"]+list2return[list3,False]# Driver codeif__name__=="__main__":# take first namep1=input("Player 1 name : ")# converted all letters into lower casep1=p1.lower()# replace any space with empty stringp1.replace(" ","")# make a list of letters or charactersp1_list=list(p1)# take 2nd namep2=input("Player 2 name : ")p2=p2.lower()p2.replace(" ","")p2_list=list(p2)# taking a flag as True initiallyproceed=True# keep calling remove_match_char function# until common characters is found or# keep looping until proceed flag is Truewhileproceed:# function calling and store return valueret_list=remove_match_char(p1_list,p2_list)# take out concatenated list from return listcon_list=ret_list[0]# take out flag value from return listproceed=ret_list[1]# find the index of "*" / border markstar_index=con_list.index("*")# list slicing perform# all characters before * store in p1_listp1_list=con_list[:star_index]# all characters after * store in p2_listp2_list=con_list[star_index+1:]# count total remaining characterscount=len(p1_list)+len(p2_list)# list of FLAMES acronymresult=["Friends","Love","Affection","Marriage","Enemy","Siblings"]# keep looping until only one item# is not remaining in the result listwhilelen(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.ifsplit_index>=0:# list slicingright=result[split_index+1:]left=result[:split_index]# list concatenationresult=right+leftelse:result=result[:len(result)-1]# print final resultprint("Relationship status :",result[0])

Output: 

Player 1 name : ANKITPlayer 2 name : DEEPIKARelationship status : Marriage

Code Explanation:

  1. The code starts by taking the first name, player 1 name.
  2. It converts all of the letters into lower case and replaces any space with an empty string.
  3. Next, it makes a list of all of the letters in player 1’s name.
  4. The code then takes the second name, player 2 name.
  5. It converts all of the letters into lower case and replaces any space with an empty string.
  6. Next, it creates a list of characters from both players’ names.
  7. The code starts by setting the proceed flag to True .
  8. Then it calls the remove_match_char function on each list.
  9. The remove_match_char function looks for common characters between both lists and removes them.
  10. If no common characters are found, then proceed is set to False and the loop ends.
  11. However, if common characters are found, then ret_list[0] stores the index of "*" in it (the border mark), ret_list[1] stores the flag value (True ), and star_index stores the index of where that character was found in p1_list or p2_list (depending on which list was used).
  12. So after removing all of the common characters from each list, p1_list and p2_list
  13. The code firstly takes in two player names as input.
  14. Next, all of the letters in each name are converted to lower case.
  15. Finally, any spaces are replaced with an empty string.
  16. A list of these strings is then created.
  17. Next, the remove_match_char function is called once for each list.
  18. This function looks for a common character between the two lists and removes it.
  19. If no common characters are found, the return value is [list1, False] and the proceed flag is set to True .
  20. If a common character is found, the return value is [list3, True] and the proceed flag is set to False .
  21. Finally, the concatenated list is returned with either [list3, True].

Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp