修复益智游戏

Fixing Puzzle Game

我是 C++ 和一般编程的新手,这是我的第一个 'project'。很抱歉,如果这不是一个适用的问题,请告诉我,我会立即删除它。

我想构建一个益智游戏,但用户不是滑动方块,而是交换与“0”相邻的数字,其中“0”是空白方块。

这是我目前的代码:

#include <iostream>
#include <ctime>
#include <algorithm>

using namespace std;

int grid[]= {1,2,3,4,5,6,7,8,0};

void shuffle()
{
    random_shuffle(&grid[0],&grid[9]);
}
void print_puzzle()
{
    shuffle();
    int a=grid[0];
    int b=grid[1];
    int c=grid[2];
    int d=grid[3];
    int e=grid[4];
    int f=grid[5];
    int g=grid[6];
    int h=grid[7];
    int i=grid[8];

    cout<<endl;
    cout<<"     "<<a<<"  "<<b<<"  "<<c<<endl;
    cout<<"     "<<d<<"  "<<e<<"  "<<f<<endl;
    cout<<"     "<<g<<"  "<<h<<"  "<<i<<endl;
}

bool check()
{
    if (grid[0]==1&&grid[1]==2&&grid[2]==3&&grid[3]==4&&grid[4]==5&&grid[6]==7&&grid[7]==8&&grid[8]==0)
    {
        return true;
    }
else
   {
        return false;
   }

}


void swap_puzzle()
{
    print_puzzle();

    int temp=0;

    cout<<"Enter the number adjacent to '0' that you want to be swapped with '0'."<<endl;
    cin>> temp;

    while(check==false)
    {
        for(int i=0; i<9; i++)
        {
            if(grid[i]==temp)
            {
                int pos_num=i;

                for(int j=0; j<9; j++)
                {

                    if(grid[j]==0)
                    {
                        int pos_0=j;
                        swap(grid[pos_num], grid[pos_0]);
                        break;
                    }

                }

            }

        }
        print_puzzle();
    }

}
int main()
{
    srand ( time(NULL) );
    cout<<"The Rules:"<<endl;
    cout<<"There will be a shuffled 3x3 grid with numbers from 1-8, with one blank space denoted by '0'. You can swap '0' only with adjacent numbers. The goal of this game is to rearrange the numbers in the grid in ascending order."<<endl;
    cout<<endl;
    cout<<"The Shuffled Grid: "<<endl;
    swap_puzzle();
    return 0;
}

将输入用户的数字与“0”交换后,网格只是随机播放而不是像我预期的那样交换它们。我试图通过将代码分解成更小的函数来解决这个问题,但这没有帮助。

如有任何建议,我们将不胜感激。

在你的 print_puzzle 函数中你有这一行

shuffle();

这意味着每次你去打印你的拼图时,它都会洗牌。你不想要那个。

还有这一行:

while(check==false)

应该是

while(check()==false)

check本身是指向函数的指针,而不是对函数的调用。

您还想将对新号码的请求移至循环内进行交换,否则您的循环将永远持续下去。

您的代码有几个问题。

  • swap_puzzle() 函数的末尾,在 while 循环内调用 print_puzzle()print_puzzle 呼叫 shuffle()。这就是为什么一切都被洗牌而不是只有交换操作的原因。要解决此问题,您必须分离这两个功能:打印是一回事,洗牌是另一回事。这些都不应该触发另一个。或者也许您可以使用 shuffle 函数在最后调用 print one,以显示新状态;但是 "print" 函数除了打印之外不应该做任何事情。

  • 然后,在您的 check() 函数中您忘记了 grid[5]==6。实际上这可能并不重要,因为如果所有其他人都在正确的位置,那么 grid[5] 应该自动是正确的。无论如何,对于它的成本,我一定会添加它。

  • 然后,您的 swap_puzzle() 出现问题,您调用交换然后中断。你有 2 个嵌套的 for 循环,你必须从两个循环中中断。现在,你正在从内部中断(使用 j 的那个),这意味着程序将在 i 的下一次迭代中恢复。那么,作为先前交换的结果,交换条件有可能再次得到满足。所以用户想走一步,结果走到了2。准确的说,第二步会反第一步,这当然不是我们想要的。要解决这个问题,您可以添加一个名为 swapped 的 bool 变量,并在 while 循环开始时将其设置为 false;当你到达休息点时,在休息之前你将它设置为真。然后,在内循环结束后,立即检查 swapped 的值:如果为真,则必须再次中断。

  • 由于您的程序现在是结构化的,因此您只显示 cout 并使用 cin 读取输入一次。您必须将这两个函数都放在 while 循环中,以便每次移动时它们都是 运行。

我做了一些小改动,效果很好。

  • 开始游戏可能需要洗牌一次。 在 swap_puzzle() 中调用 shuffle()(但打印时不要乱序)
  • cin>> temp; 放入 while 循环中,以便用户可以输入直到解开谜题
  • 也打破内部循环(我为此添加了一些变量)
  • 最终调用 check(),如果为真则中断。那就是解决了。

--

#include <iostream>
#include <ctime>
#include <algorithm>

using namespace std;

int grid[]= {1,2,3,4,5,6,7,8,0};

void shuffle()
{
    random_shuffle(&grid[0],&grid[9]);
}
void print_puzzle()
{
 //   shuffle();
    int a=grid[0];
    int b=grid[1];
    int c=grid[2];
    int d=grid[3];
    int e=grid[4];
    int f=grid[5];
    int g=grid[6];
    int h=grid[7];
    int i=grid[8];

    cout<<endl;
    cout<<"     "<<a<<"  "<<b<<"  "<<c<<endl;
    cout<<"     "<<d<<"  "<<e<<"  "<<f<<endl;
    cout<<"     "<<g<<"  "<<h<<"  "<<i<<endl;
}

bool check()
{
    if (grid[0]==1&&grid[1]==2&&grid[2]==3&&grid[3]==4&&grid[4]==5&&grid[6]==7&&grid[7]==8&&grid[8]==0)
    {
        return true;
    }
else
   {
        return false;
   }

}


void swap_puzzle()
{
    shuffle();
    print_puzzle();

    int temp=0;
    bool swaped = false;
    while(1)
    {
        swaped = false;
        cout<<"Enter the number adjacent to '0' that you want to be swapped with '0'."<<endl;
        cin>> temp;
        for(int i=0; i<9; i++)
        {
            if(grid[i]==temp)
            {
                int pos_num=i;

                for(int j=0; j<9; j++)
                {

                    if(grid[j]==0)
                    {
                        int pos_0=j;
                        swap(grid[pos_num], grid[pos_0]);
                        swaped = true;
                        break;
                    }

                }
            }
                if(swaped) break;

        }
        print_puzzle();
        if(check()==true) break;
    }

}
int main()
{
    srand ( time(NULL) );
    cout<<"The Rules:"<<endl;
    cout<<"There will be a shuffled 3x3 grid with numbers from 1-8, with one blank space denoted by '0'. You can swap '0' only with adjacent numbers. The goal of this game is to rearrange the numbers in the grid in ascending order."<<endl;
    cout<<endl;
    cout<<"The Shuffled Grid: "<<endl;
    swap_puzzle();
    return 0;
}