FLAMES game using Java

FLAMES game using Java
In this post we are going to see Java code for flames game. This is one of the commonly asked interview question. Before we go to the code, Let us see how are we going to solve this.

Step 1:
After getting the names, We are going to use StringBuilder to compare the names and replace the common matching characters into “0”.

Step 2:
After replacing with “0” we are going to remove the character “0” and count the length of both strings to get the value.

Step 3:
After getting the number, We are going to use the formula – x%flamesLength, i.e.
x = length of both strings after striking out matching characters
FlamesLength = Length of the flames as it get decremented on each iteration (As we strike out the characters one by one on each iteration)

int  y = x % flames.length() ;

There is one constraint here to make a note, When you strike characters in flames, on each iteration the counting starts from position ‘0’ in string. But as per flames game, for example – if the character “a” gets strike out the counting should start from the next character “m”. But on iterations the counting starts from initial position. To overcome this constraint we are going to use substring. (you can understand it from code)

Step 4:
After strikings when we get the last character, We are using the switch case to find the alphabet and give the meaning of that

Now let us see the code for Flames game,





Leave a Reply

Your email address will not be published. Required fields are marked *