visit
using System;
class FlamesGame
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to the Flames game!");
Console.Write("Enter your name: ");
string yourName = Console.ReadLine();
Console.Write("Enter your partner's name: ");
string partnerName = Console.ReadLine();
string flamesResult = CalculateFlames(yourName, partnerName);
Console.WriteLine("Your Flames result is: " + flamesResult);
}
static string CalculateFlames(string yourName, string partnerName)
{
string flames = "FLAMES";
int count = yourName.Length + partnerName.Length;
for (int i = 0; i < yourName.Length; i++)
{
for (int j = 0; j < partnerName.Length; j++)
{
if (yourName[i] == partnerName[j])
{
count -= 2;
yourName = yourName.Remove(i, 1);
partnerName = partnerName.Remove(j, 1);
i--;
break;
}
}
}
int index = 0;
for (int i = 1; i <= 5; i++)
{
index = (index + count) % flames.Length;
if (index == 0)
{
index = flames.Length;
}
flames = flames.Remove(index - 1, 1);
}
return flames;
}
}
We start by displaying a welcome message to the user and prompting them to enter their name and their partner’s name using the Console.ReadLine()
method.
Console.WriteLine("Welcome to the Flames game!");
Console.Write("Enter your name: ");
string yourName = Console.ReadLine();
Console.Write("Enter your partner's name: ");
string partnerName = Console.ReadLine();
Next, we pass the user’s names to the CalculateFlames()
method, which performs the actual calculation of the result.
string flamesResult = CalculateFlames(yourName, partnerName);
The CalculateFlames()
method first removes all the standard letters from the two names by iterating through the letters in each name and removing the letters that appear in both names. It also keeps a count of the remaining letters.
static string CalculateFlames(string yourName, string partnerName)
{
string flames = "FLAMES";
int count = yourName.Length + partnerName.Length;
for (int i = 0; i < yourName.Length; i++)
{
for (int j = 0; j < partnerName.Length; j++)
{
if (yourName[i] == partnerName[j])
{
count -= 2;
yourName = yourName.Remove(i, 1);
partnerName = partnerName.Remove(j, 1);
i--;
break;
}
}
}
int index = 0;
for (int i = 1; i <= 5; i++)
{
index = (index + count) % flames.Length;
if (index == 0)
{
index = flames.Length;
}
flames = flames.Remove(index - 1, 1);
}
return flames;
Console.WriteLine("Your Flames result is: " + flamesResult);
Also published here.