我试图实现一个搜索多维数组的函数,找出其中是否有值,然后移动该函数。我的搜索功能
bool search(int value, int values [][d], int n)
{
bool In = false;
//d is an it that is the maximum length and height
//e.g 3x3 or 4x4 as in the image below
for(int row=0; row<d; row++)
{
for(int col=0; col<d; col++)
{
if(values[row][col] == value)
{
//checking if this loop is executed
printf("EXECUTED!! :) \n");
In=true;
}
printf("Row:%i & Col%i: %i \n",row,col,values[row][col]);
}
}
//Usleep for debugging purpouses
// as another function clears the screen
usleep(50000000);
if(In==true){return true;}
if(In==false){return false;}
}
这是打印的内容,这对于打印上面的 4x4 框很奇怪,我使用了相同的数组,并且搜索功能无论如何都不会改变数组。这是我的“移动”功能
bool move(int tile)
{
if(search(tile,board,d))
{
printf("please execute this code pretty please clang\n");
return true;
}
else
{
printf("NOO\n");
return false;
}
}
这是首先初始化变量的函数
void init(void)
{
bool even;
if((d & 1) == 0)
{
even = true;
}
else
{
even = false;
}
int value = d*d - 1;
for(int row =0; row<d; row++)
{
for(int col=0; col<d; col++)
{
board[row][col]=value;
value--;
}
}
//for this game to work if d is even the values of the third
// and second last arrays must be switched
if(even==true)
{
int temp = board[d-1][d-2];
board [d-1][d-2] = board[d-1][d-3];
board [d-1][d-3] = temp;
}
}
编辑:这是完整代码的 pastebin http://pastebin.com/yS8DDEqZ 注意 Cs50 是一个自定义库,由 im 类实现,它定义了一个字符串,一个辅助函数,用于获取用户输入 GetInt() 等。