📜  实现火焰游戏的程序

📅  最后修改于: 2022-05-13 01:57:06.872000             🧑  作者: Mango

实现火焰游戏的程序

火焰是一个流行的游戏,以首字母缩写命名:朋友,恋人,深情,婚姻,敌人,兄弟姐妹。该游戏无法准确预测某个人是否适合您,但与您的朋友一起玩会很有趣。
这个游戏有两个步骤:

  • 取这两个名字。
  • 删除常见字符及其各自的常见情况。
  • 获取剩余字符数。
  • 以 FLAMES 字母为 [“F”, “L”, “A”, “M”, “E”, “S”]
  • 使用我们得到的计数开始删除字母。
  • 最后一个过程的字母是结果。

例子 :

Input: Player1 = AJAY, Player2 = PRIYA
Output: Friends

说明:在上面给定的两个名称 A 和 Y 是两个名称中出现一次(共同计数)的常见字母,因此我们从两个名称中删除这些字母。现在计算剩下的字母总数,它是 5。现在开始使用我们得到的计数从 FLAMES 中一个一个地删除字母,持续该过程的字母就是结果。
计数以逆时针循环方式进行。

方法:需要三个计数器,两个用于初始化为零的名称,一个用于初始化为 5 的火焰。使用三个字符串,两个用于名称,一个用于已经存储了 FLAMES 的位置。这里程序首先计算名字中的字母数量,然后计算第二名字中的字母数量,然后在使用 strlen 将它们存储到整数变量中之后,为每个名字运行一个 for 循环以计算常见字母。然后通过使用嵌套的 if-else 来取消每个名称中的字母,该名称由字符串表示。再次重复 for 循环以继续此过程。因此,计数器旋转并指向 FLAMES 中的每个字母。随着字母被取消,循环再次运行。然后对于每个字母,使用 If else 语句,打印最后一个字母对应的结果。
下面是实现:

C++
// C++ program to implement FLAMES game
#include 
using namespace std;
 
// Function to find out the flames result
void flame(char* a, char* b)
{
    int i, j, k, l = 1, n, m, sc = 0, tc, rc = 0, fc = 5;
    char q[25], w[25], c;
    char f[] = "flames";
 
    strcpy(q, a);
    strcpy(w, b);
    n = strlen(a);
    m = strlen(b);
    tc = n + m;
 
    for (i = 0; i < n; i++)
    {
        c = a[i];
        for (j = 0; j < m; j++)
        {
            if (c == b[j])
            {
                a[i] = -1;
                b[j] = -1;
                sc = sc + 2;
                break;
            }
        }
    }
 
    rc = tc - sc;
 
    for (i = 0;; i++)
    {
        if (l == (rc))
        {
            for (k = i; f[k] != '\0'; k++)
            {
                f[k] = f[k + 1];
            }
            f[k + 1] = '\0';
            fc = fc - 1;
            i = i - 1;
            l = 0;
        }
        if (i == fc)
        {
            i = -1;
        }
        if (fc == 0)
        {
            break;
        }
        l++;
    }
 
    // Print the results
    if (f[0] == 'e')
        cout << q <<" is ENEMY to " << w;
    else if (f[0] == 'f')
        cout << q <<" is FRIEND to "<


C
// C program to implement FLAMES game
 
#include 
#include 
 
// Function to find out the flames result
void flame(char* a, char* b)
{
    int i, j, k, l = 1, n, m, sc = 0, tc, rc = 0, fc = 5;
    char q[25], w[25], c;
    char f[] = "flames";
 
    strcpy(q, a);
    strcpy(w, b);
    n = strlen(a);
    m = strlen(b);
    tc = n + m;
 
    for (i = 0; i < n; i++) {
        c = a[i];
        for (j = 0; j < m; j++) {
            if (c == b[j]) {
                a[i] = -1;
                b[j] = -1;
                sc = sc + 2;
                break;
            }
        }
    }
 
    rc = tc - sc;
 
    for (i = 0;; i++) {
        if (l == (rc)) {
            for (k = i; f[k] != '\0'; k++) {
                f[k] = f[k + 1];
            }
            f[k + 1] = '\0';
            fc = fc - 1;
            i = i - 1;
            l = 0;
        }
        if (i == fc) {
            i = -1;
        }
        if (fc == 0) {
            break;
        }
        l++;
    }
 
    // Print the results
    if (f[0] == 'e')
        printf("%s is ENEMY to %s ", q, w);
    else if (f[0] == 'f')
        printf("%s is FRIEND to %s ", q, w);
    else if (f[0] == 'm')
        printf("%s is going to MARRY %s", q, w);
    else if (f[0] == 'l')
        printf("%s is in LOVE with %s ", q, w);
    else if (f[0] == 'a')
        printf("%s has more AFFECTION on %s ", q, w);
    else
        printf("%s and %s are SISTERS/BROTHERS ", q, w);
}
 
// Driver code
int main()
{
 
    char a[] = "AJAY";
    char b[] = "PRIYA";
 
    flame(a, b);
}


Python3
# Python3 program to implement FLAMES game
 
# Function to find out the flames result
def flame(a, b):
    l, sc = 1, 0
    rc, fc = 0, 5
    f = "flames"
    f = [i for i in f]
    q = "".join(a)
    w = "".join(b)
     
    # print(q, w)
    n = len(a)
    m = len(b)
    tc = n + m
    for i in range(n):
        c = a[i]
        for j in range(m):
            if (c == b[j]):
                a[i] = -1
                b[j] = -1
                sc = sc + 2
                break
 
    rc = tc - sc
    i = 0
 
    while (i):
        if (l == (rc)):
            for k in range(i,len(f)):
                f[k] = f[k + 1]
            f[k + 1] = '\0'
            fc = fc - 1
            i = i - 1
            l = 0
        if (i == fc):
            i = -1
        if (fc == 0):
            break
        l += 1
        i += 1
 
    # Print the results
    if (f[0] == 'e'):
        print(q, "is ENEMY to", w)
    elif (f[0] == 'f'):
        print(q, "is FRIEND to", w)
    elif (f[0] == 'm'):
        print(q, "is going to MARRY", w)
    elif (f[0] == 'l'):
        print(q, "is in LOVE with", w)
    elif (f[0] == 'a'):
        print(q, "has more AFFECTION on", w)
    else:
        print(q, "and", w, "are SISTERS/BROTHERS ")
 
# Driver code
a = "AJAY"
b = "PRIYA"
a = [i for i in a]
b = [j for j in b]
 
# print(a,b)
flame(a, b)
 
# This code is contributed by Mohit Kumar


Javascript


输出:
AJAY is FRIEND to PRIYA