搜索数组以打印正确的语句

Searching array to print correct statement

此代码适用于用户输入网格大小的游戏,网格的第一行中除 1 外均填充 0。然后用户输入坐标,一次一个(与大小相同)。如果坐标或它周围的8个空格中有1,则1变为0。如果没有命中,则1向下移动一行。尝试另一个坐标,依此类推。如果在游戏结束时没有 1,则 "stormtroopers" 获胜。但是,如果至少有 1 个到达最后一行,则 "redshirts" 获胜。基本上,如果所有坐标都被取入,并且任何 1 仍然存在,则红衫军也被取入。我的问题是确定谁获胜。我的最后陈述没有正常工作;我的程序总是说冲锋队赢了,我已经尝试了很多东西,但我不知道如何让它工作(第 107-119 行)。那里的 for 语句可能不需要,我只是试了一下,看看他们是否会修复它。

#include <iostream>
using namespace std;

int ** createField(int N);
int playGame(int ** arr, int N);

int main()
{
    int N;

    cout << "Enter the number of Redshirts: " << endl;
    cin >> N;

    int ** arr = createField(N);
    **createField(N);

    playGame(arr, N);
}

int ** createField(int N)
{
    int ** arr = new int *[N];
    for (int i = 0; i<N; i++)
        arr[i] = new int[N];            //creating array of user size

    for (int i = 0; i < N; i++)           // fills array with 0's except for first row
    {
        for (int j = 0; j < N; j++)
        {
            arr[i][j] = 0;
            arr[1][j] = 1;
        }
    }

    return arr;
}

int playGame(int ** arr, int N)    
{
    int x, y;
    cout << "Enter the coordinates of the " << N << " shots: " << endl;

    for (int i = 0; i < N; i++)
    {
        cin >> x >> y;
        x = x - 1;                                  // starts grid at 1 vs 0
        y = y - 1;
        if (x >= N || y >= N)
        {
            cout << "The coordinate is out of bounds." << endl;
            return 0;
        }
        else
        {
            if (arr[x][y] == 1)                     // searches each position for a redshirt (1)
                arr[x][y] = 0;
            if ((x + 1) != N)                       // these if statements check that surrounding spaces are not out of bounds
            {
                if (arr[x + 1][y] == 1)
                    arr[x + 1][y] = 0;
            }
            if ((x - 1) >= 0)
            {
                if (arr[x - 1][y] == 1)
                    arr[x - 1][y] = 0;
            }
            if ((y + 1) != N)
            {
                if (arr[x][y + 1] == 1)
                    arr[x][y + 1] = 0;
            }
            if ((y - 1) >= 0)
            {
                if (arr[x][y - 1] == 1)
                    arr[x][y - 1] = 0;
            }
            if ((x - 1) >= 0 && (y - 1) >= 0)
            {
                if (arr[x - 1][y - 1] == 1)
                    arr[x - 1][y - 1] = 0;
            }
            if ((x - 1) >= 0 && (y + 1) != N)
            {
                if (arr[x - 1][y + 1] == 1)
                    arr[x - 1][y + 1] = 0;
            }
            if ((x + 1) != N && (y + 1) != N)
            {
                if (arr[x + 1][y + 1] == 1)
                    arr[x + 1][y + 1] = 0;
            }
            if ((y - 1) >= 0 && (x + 1) != N)
            {
                if (arr[x + 1][y - 1] == 1)
                    arr[x + 1][y - 1] = 0;
            }
        }
            for (int y = 0; y < N; y++)
            {
                if (arr[x][y] == 1)            // searches for any redshirts left
                {
                    arr[x + 1][y] = 1;         // moves remaining reshirts down one space
                    arr[x][y] = 0;
                }
            }
    }
    for (int x = 0; x < N; x++)
    {
        for (int y = 0; y < N; y++)
        {
            if (arr[x][y] == 1)                                    // if redshirts reached end, they win
            {
                cout << "Redshirts win. " << endl;
                return 1;
            }
            else
            {
                cout << "Stormtroopers win." << endl;
                return 2;
            }
        }
    }
}

在你的 PlayGame 函数的最后,会发生在 arr[0][0],如果它是 1,它表示 Redshirts win,否则 Stormtroopers win,你的代码没有机会检查其他网格。您应该将 Stormtrooper 获胜消息移到嵌套 for 循环之外。

for (int x = 0; x < N; x++)
{
    for (int y = 0; y < N; y++)
    {
        if (arr[x][y] == 1) // if redshirts reached end, they win
        {
            cout << "Redshirts win. " << endl;
            return 1;
        }
    }
}
cout << "Stormtroopers win." << endl;
return 2;