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.
FLAMES is 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:
- Take the two names.
- Remove the common characters with their respective common occurrences.
- Get the count of the characters that are left .
- Take FLAMES letters as [“F”, “L”, “A”, “M”, “E”, “S”]
- Start removing letter using the count we got.
- The letter which last the process is the result.
Example :
Input : player1 name : AJAY
player 2 name : PRIYA
Output : 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.
F
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
def remove_match_char(list1, list2):
for i in range ( len (list1)):
for j in range ( len (list2)):
if list1[i] = = list2[j]:
c = list1[i]
list1.remove(c)
list2.remove(c)
list3 = list1 + [ "*" ] + list2
return [list3, True ]
list3 = list1 + [ "*" ] + list2
return [list3, False ]
if __name__ = = "__main__" :
p1 = input ( "Player 1 name : " )
p1 = p1.lower()
p1.replace( " " , "")
p1_list = list (p1)
p2 = input ( "Player 2 name : " )
p2 = p2.lower()
p2.replace( " " , "")
p2_list = list (p2)
proceed = True
while proceed:
ret_list = remove_match_char(p1_list, p2_list)
con_list = ret_list[ 0 ]
proceed = ret_list[ 1 ]
star_index = con_list.index( "*" )
p1_list = con_list[: star_index]
p2_list = con_list[star_index + 1 :]
count = len (p1_list) + len (p2_list)
result = [ "Friends" , "Love" , "Affection" , "Marriage" , "Enemy" , "Siblings" ]
while len (result) > 1 :
split_index = (count % len (result) - 1 )
if split_index > = 0 :
right = result[split_index + 1 :]
left = result[: split_index]
result = right + left
else :
result = result[: len (result) - 1 ]
print ( "Relationship status :" , result[ 0 ])
|
Output:
Player 1 name : ANKIT
Player 2 name : DEEPIKA
Relationship status : Marriage
Code Explanation:
- The code starts by taking the first name, player 1 name.
- It converts all of the letters into lower case and replaces any space with an empty string.
- Next, it makes a list of all of the letters in player 1’s name.
- The code then takes the second name, player 2 name.
- It converts all of the letters into lower case and replaces any space with an empty string.
- Next, it creates a list of characters from both players’ names.
- The code starts by setting the proceed flag to True .
- Then it calls the remove_match_char function on each list.
- The remove_match_char function looks for common characters between both lists and removes them.
- If no common characters are found, then proceed is set to False and the loop ends.
- 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).
- So after removing all of the common characters from each list, p1_list and p2_list
- The code firstly takes in two player names as input.
- Next, all of the letters in each name are converted to lower case.
- Finally, any spaces are replaced with an empty string.
- A list of these strings is then created.
- Next, the remove_match_char function is called once for each list.
- This function looks for a common character between the two lists and removes it.
- If no common characters are found, the return value is [list1, False] and the proceed flag is set to True .
- If a common character is found, the return value is [list3, True] and the proceed flag is set to False .
- Finally, the concatenated list is returned with either [list3, True].
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 Dec, 2022
Like Article
Save Article