首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >跳跃游戏中嵌套for循环的问题

跳跃游戏中嵌套for循环的问题
EN

Stack Overflow用户
提问于 2019-04-25 05:07:18
回答 1查看 47关注 0票数 0

我需要帮助创建一个嵌套的for循环。这是用于“跳跃游戏”的,基本上有一个数组,里面有一些数字。每个数组位置的数字都有一定的次数可以跳到末尾。例如:[2 3 1 1 4 0],所以array[0] = 2,它可以跳到2或1位,array[1] = 3,可以跳到3,2或1位。例如:[3 2 1 0 4 0],所以你基本上永远不能到达数组中的位置。

我已经做了一段时间了,还是想不出循环的方法。readFromFile基本上只是创建数组大小。现在我有了尺寸,我需要检查我粘在上面的每个数字。

代码语言:javascript
运行
复制
void readFromFile(int list[], int& size, ifstream& infile)
{
    infile >> list[size];

    while (!infile.eof()) // Loops until end of file is reached
    {
        size++;
        infile >> list[size];
    }

    infile.close();
    return;

}

bool getToLastIndex(int list[], int size)
{
    for(i=0; i< size;i++)
    {
        if(list[i] == 0)
            break;
        j=i+list[i]; //Resetting it for a new jump or section on array?
        i=j;

        for(int j=0; j <= list[i]; j++)
        {
            list[j] == return true;
        }
    }
}

我希望能够使用getToLastIndex(list,size);

代码语言:javascript
运行
复制
if(getToLastIndex() == true)
{
    cout << "Mario FTW" << endl; // Got to the end
}
else
    cout << "This bridge has the workings of bowser all over it >:( " << endl; //Couldnt make it to the end

作业是以马里奥为主题的。我没有选择输出

EN

回答 1

Stack Overflow用户

发布于 2019-04-25 05:25:54

尝试以下操作:

代码语言:javascript
运行
复制
void readFromFile(int list[], int &size, ifstream& infile)
{
    size = 0;
    if(!infile.is_open())
        return;

    while(!infile.eof())
    {
        infile >> list[size++];
    }
    infile.close();
}

bool isTheLastReachable(int list[], int index, int size)
{
    if(index < size)
    {
        if(index)
        {
            for(int i = 0; i < list[index]; ++i)
                if(isTheLastReachable(list, index + i, size))
                    return true;

            return false;
        }
        else
            return false;
    }
    return true;
}


bool getToLastIndex(int list[], int size)
{
    return isTheLastReachable(list, 0, size);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55838557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档