Работа над версией тура рыцаря. Программа не действует так, как я думаю, это дало бы мой код. Я получаю несколько разных странных расхождений. «Рыцарь» выходит за пределы, хотя я настраивал параметры для этого. Есть также больше движений, чем нужно дать мой цикл while. Помешайте значение i, значение j и число в цикле while, и вы получите несколько странных результатов. У меня есть значение, установленное на 32, чтобы просто увидеть, что произойдет. Вот код и результат:Проблема с If/Else Statement для перемещения по 2d массиву
#include <iostream>
int board[8][8];
void printBoard();
bool openSpace(int i,int j);
int startingPoint=0, m , n, i , j;
using namespace std;
int main()
{
i=4;
j=2;
cout<<"Move: "<<startingPoint<<" Coordinates: "<<i<<","<<j<<endl;
std::fill_n(&board[0][0], sizeof(board)/sizeof(**board), -1);//set all points to -1
//Set the starting point to zero
board[i][j] = startingPoint;//plot the starting point
while(startingPoint<32)//for the first 32 points
{
if (i-2>0)
{
if(j+1<8)
{
m=i-2;
n=j-2;
if (openSpace(m,n))
{
startingPoint++;
i=i-2;
j=j+1;
board[i][j]=startingPoint;
cout<<"Move: "<<startingPoint<<" Coordinates: "<<i<<","<<j<<endl;
}
}
if(j-1>0)
m=i-2;
n=j-1;
if (openSpace(m,n))
{
startingPoint++;
i=i-2;
j=j-1;
board[i][j]=startingPoint;
cout<<"Move: "<<startingPoint<<" Coordinates: "<<i<<","<<j<<endl;
}
}
if (i-1>0)
{
if(j+2<8)
{
m=i-1;
n=j+2;
if (openSpace(m,n))
{
startingPoint++;
i=i-1;
j=j+2;
board[i][j]=startingPoint;
cout<<"Move: "<<startingPoint<<" Coordinates: "<<i<<","<<j<<endl;
}
}
if(j-2>0)
m=i-1;
n=j-2;
if (openSpace(m,n))
{
startingPoint++;
i=i-1;
j=j-2;
board[i][j]=startingPoint;
cout<<"Move: "<<startingPoint<<" Coordinates: "<<i<<","<<j<<endl;
}
}
if (i+1<8)
{
if(j+2<8)
{
m=i+1;
n=j+2;
if (openSpace(m,n))
{
startingPoint++;
i=i+1;
j=j+2;
board[i][j]=startingPoint;
cout<<"Move: "<<startingPoint<<" Coordinates: "<<i<<","<<j<<endl;
}
}
if(j-2>0)
m=i+1;
n=j-2;
if (openSpace(m,n))
{
startingPoint++;
i=i+1;
j=j-2;
board[i][j]=startingPoint;
cout<<"Move: "<<startingPoint<<" Coordinates: "<<i<<","<<j<<endl;
}
}
if (i+2<8){
if(j+1<8)
{
m=i+2;
n=j+1;
if (openSpace(m,n))
{
startingPoint++;
i=i+2;
j=j+1;
board[i][j]=startingPoint;
cout<<"Move: "<<startingPoint<<" Coordinates: "<<i<<","<<j<<endl;
}
}
if(j-1>0)
m=i+2;
n=j-1;
if (openSpace(m,n))
{
startingPoint++;
i=i+2;
j=j-1;
board[i][j]=startingPoint;
cout<<"Move: "<<startingPoint<<" Coordinates: "<<i<<","<<j<<endl;
}
}else{
cout<<"I don't understand"<<endl;
}
}//end while
printBoard();
}
void printBoard()
{
//We need to loop through each row one by one
for (int row=0; row<8; row++)
{
//now loop through each column in the row
for(int column=0; column<8; column++)
{
cout<<board[row][column]<<" ";
}
cout<<endl;
}
cout<<endl;
}
//Check for open surrounding spaces
bool openSpace(int m,int n){
if (board[m][n] == -1)
return true;
else
return false;
}
Результат:
Move: 0 Coordinates: 4,2
Move: 1 Coordinates: 2,3
Move: 2 Coordinates: 0,2
Move: 3 Coordinates: 1,4
Move: 4 Coordinates: 2,2
Move: 5 Coordinates: 4,3
Move: 6 Coordinates: 6,2
Move: 7 Coordinates: 4,3
Move: 8 Coordinates: 3,5
Move: 9 Coordinates: 4,7
Move: 10 Coordinates: 5,5
Move: 11 Coordinates: 7,6
Move: 12 Coordinates: 5,7
Move: 13 Coordinates: 3,6
Move: 14 Coordinates: 2,4
Move: 15 Coordinates: 3,2
Move: 16 Coordinates: 5,3
Move: 17 Coordinates: 7,2
Move: 18 Coordinates: 5,3
Move: 19 Coordinates: 4,5
Move: 20 Coordinates: 3,3
Move: 21 Coordinates: 4,1
Move: 22 Coordinates: 6,0
Move: 23 Coordinates: 4,-1
Move: 24 Coordinates: 3,1
Move: 25 Coordinates: 2,-1
Move: 26 Coordinates: 3,-3
Move: 27 Coordinates: 5,-2
Move: 28 Coordinates: 4,0
Move: 29 Coordinates: 5,2
Move: 30 Coordinates: 6,0
I don't understand
Move: 31 Coordinates: 7,-2
I don't understand
Move: 32 Coordinates: 5,-1
Move: 33 Coordinates: 6,1
I don't understand
-1 -1 2 -1 -1 -1 -1 -1
-1 -1 -1 -1 3 -1 -1 25
-1 -1 4 1 14 26 -1 -1
-1 24 15 20 -1 8 13 23
28 21 0 7 -1 19 27 32
-1 -1 29 18 -1 10 -1 12
30 33 6 -1 -1 -1 31 -1
-1 -1 17 -1 -1 -1 11 -1
Process returned 0 (0x0) execution time : 0.172 s
Press any key to continue.
Так что да, почему там 34 двигается, почему идущие с доски?
Время, чтобы узнать, как использовать отладчик и как его использовать, чтобы выполнить свой код, строко за строкой, контролируя переменные и их значения. –
Во-вторых, вам нужно изучить некоторые навыки отладки. Вы можете использовать визуальный отладчик или добавить дополнительные инструкции cout для печати значений переменных. –
Вы можете использовать 'n = j + 1;' вместо 'n = j-2;' в самом начале цикла. – felix