Gangadhar B Gangadhar B - 6 months ago 26
Java Question

flames game logic using java giving error result?

Here I have created a flames game logic it is final string length is correct(eg:4 for two strings 'raja' and 'rani') based on length I need to show 'f' or 'l' or 'a' or 'm' or 'e' or 's'.
I have written logic based on length that character is visible but that is not my concern. For length 4 result should be 'e'(in flames for length 4 first 'm' should remove then 'l' then 'f' then 'a' then 's' finally i need 'e' as output.Can anybody tell me idea. This is my code.

public static void main(String[] args) {
String name1 = "raja";
String name2 = "rani";
String s1 = name1;
String s2 = name2;
for (int i = 0; i < name1.length(); i++) {
for (int j = 0; j < name2.length(); j++) {
if (name1.charAt(i) == name2.charAt(j)) {
name1 = name1.replaceFirst(String.valueOf(name1.charAt(i)), "#");
name2 = name2.replaceFirst(String.valueOf(name2.charAt(j)), "#");
}
}
}
String result = name1 + name2;
result = result.replaceAll("#", "");
int resultLength = result.length();
String baseInput = "flames";
char relationIs = 0;
int temp = 0;
if (resultLength > 0) {
temp = resultLength % baseInput.length();
}
if (temp == 0 && resultLength >= 6) {
relationIs = 's';
} else {
int count = temp - 1;
if (count >= 0) {
relationIs = baseInput.charAt(count);
System.out.println("Relation Betw " + s1 + " and " + s2 + " is:");
}
}
switch (relationIs) {
case 'f':
System.out.println("friendship");
break;
case 'l':
System.out.println("Lovers");
break;
case 'a':
System.out.println("Affection");
break;
case 'm':
System.out.println("Marriage");
break;
case 'e':
System.out.println("Enemity");
break;
case 's':
System.out.println("Siblings");
break;
default:
System.out.println("FLAME Test works only for different names");
break;
}
}


Logic follows this order:only forward direction remove 4 character.

if length=4
step 0:flames ('f' as 1)
step 1:flaes (here 'e' as 1)
step 2:faes (here 'a' as 1)
step 3:aes (here 'a' as 1)
step 4:es (here 'e' as 1)
step 5:e //output.


Your help will be appreciated.

Answer

Hope this is what you need. I am here striking the character of the word flames until i am getting one character. Once i get the character that is what the result of flames.

 if (resultLength > 0) {
    while (baseInput.length() !=1)
        {
           System.out.println(baseInput);
           int tmpLen = resultLength % baseInput.length(); //finding char position to strike
           if(tmpLen != 0)
           {
               temp = baseInput.substring(tmpLen) + baseInput.substring(0, tmpLen-1); //Append part start from next char to strike and first charater to char before strike.
           }
           else
           {
               temp = baseInput.substring(0, baseInput.length()-1); //If mod result zero we can strike last letter easily
           }
           baseInput = temp; //Assign the temp to baseinput for next iteration.
        }
        relationIs = baseInput.charAt(0);
        System.out.println(relationIs);
 }

Link : http://ideone.com/Fqgcc1