Program to Implement FLAMES Game in Java
Friends, Lovers, Affectionate, Marriage, Enemies, and Sibling are all represented by the acronym FLAMES, which also serves as the name of a well-known game. Although, it can be entertaining to play with your friends, this game does not effectively determine whether a person is suited for you. So, we are going to learn how to create a program to find what will be the relation between the two names. So, in this article, we are going to know how to write a program for flames game and we also get a brief understanding how the game works.
How does the FLAMES game work?
Following are the sequence of steps by which we can find the relation between two names.
- Select two names.
- Eliminate the characters that frequently appear along with those instances.
- Count the number of characters still available.
- Think of FLAMES as the letter’s "F", "L", "A", "M", "E," and "S."
- Using the count, we just received, start eliminating letters.
- The final letter in the process is the outcome.
Approach for building the program
Two counts are needed for names with initial values of 0, and one counter is needed for flames with initial values of 5. Two strings are used for names, and one is used to keep FLAMES, totaling three strings. Here, the computer calculated the first name's letter count before calculating the second name's letter count. Then, one for loop is executed for each name to count the frequent letters after saving them using strlen into integer variables. After that, each name, which is represented by a string, has its letters removed using stacked if-else statements. To carry out this operation further, the for loop is repeated. Each letter of FLAMES is pointed to because of the counter rotating.
Implementation
Two names will be accepted as input, after which we will delete any common characters that appear often in both names. We will create a user-defined function that will hold a list of characters that correspond to the names of two players and return a list of concatenated lists to eliminate.Once we have deleted all the common characters, the result list will comprise acronyms for "Friends," "Lovers," "Affectionate," "Marriage," "Enemies," and "Siblings." We'll next begin removing words from the list one at a time until there is only one left using the count we received. The term that remains on the list will be the result.
Example:
Input: Player1 is AJAY, while Player2 is PRIYA.
Results: Friends.
We are deleting the letters A and Y from the two names in the example above since they are both instances of common letters that only appear once in each name (common count). The number of letters remaining is now 5, so let's count them all. Using the count, we just received, start deleting letters from FLAMES one at a time. The letter that survives the procedure is the outcome.
- Circularly, counterclockwise motion is used for counting.
- FLAMES counting starts from F, and since E is at the fifth count, we erase E and begin counting again from S.
- FLAMS
- M is on the fifth count, so we take Mout of the equation and start counting from S.
- We delete S and start counting with F as FLAS S is on the fifth count.
- FLA
- L is on the fifth count; therefore, we take L out of the equation and start counting from A.
- The fifth count for FA A necessitates A's elimination. This is the final response since there is just one letter left.
- F
- Now, F or friends describes the relationship.
Program to implement flames using Java
import java.io.*;
import java.util.*;
public class Flames
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
StringBuffer s1=new StringBuffer(sc.next());
StringBuffer s2=new StringBuffer(sc.next());
char c,d,e='\0';
for(int i=0;i<s1.length();i++)
{
c=s1.charAt(i);
for(int j=0;j<s2.length();j++)
{
d=s2.charAt(j);
if(c==d)
{
s1.deleteCharAt(i);
s2.deleteCharAt(j);
i--;
j--;
break;
}
}
}
s1=s1.append(s2);
int k=6,l=0;
String s3="FLAMES";
while(k>1)
{
for(int m=1;m<s1.length();m++)
{
if(s3.charAt(l)!='X')
{
l++;
if(l==6)
l=0;
}
else
{
l++;
if(l==6)
l=0;
m--;
}
}
while(s3.charAt(l)=='X')
{
l++;
if(l==6)
l=0;
}
s3=s3.substring(0,l)+"X"+s3.substring(l+1);
k--;
}
for(int i=0;i<s3.length();i++)
{
if(s3.charAt(i)!='X')
e=s3.charAt(i);
}
switch(e)
{
case 'F':System.out.println("Friends");
break;
case 'L':System.out.println("Love");
break;
case 'A':System.out.println("Affection");
break;
case 'M':System.out.println("Marriage");
break;
case 'E':System.out.println("Enemy");
break;
case 'S':System.out.println("Sister");
break;
default :break;
}
}
}
Output:
Love