2017-02-16 7 views
-1

ナイトツアーのバージョンで作業しています。プログラムは私のコードを与えてくれたと思うように行動していません。私はいくつかの異なる奇妙な差別を得ています。私はそれに対してパラメターを設定していますが、 'ナイト'は限界を迎えています。私のwhileループを与えなければならないよりも多くの動きがあります。 i値、j値、whileループの数値を混乱させると、いくつかの奇妙な結果が得られます。私は32に値を設定しましたが、何が起こるかを見るだけです。ここでは、コードと結果は次のとおりです。If/Elseステートメントが2次元配列を移動する際の問題

#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が移動すると、なぜボードをオフに行くされているがありますか?

+2

デバッガを使用する方法と、変数とその値を監視しながら、デバッガを使用してコードを段階的に実行する方法を学習する時間です。 –

+0

私はもう一度、デバッグのスキルを学ぶ必要があると思います。ビジュアルデバッガを使用するか、さらに多くのcout文を追加して変数の値を出力することができます。 –

+0

ループの最初の段階で、 'n = j-2;'の代わりに 'n = j + 1;'を使いたいかもしれません。 – felix

答えて

1

問題はほとんどないと思います。

まずあなたは、したがって、あなたがcontinueステートメントを使用することができます32の制限を超え、これはwhileループが確認されるまで、複数のif文が真であることを意味許さ移動に他の

while (starting point<32){ 
    if (not over right board){ 
    } 
    if (not over left board){ 
    } 
    .... 
} 

後のいずれかにチェックマークを付け各移動の後にループの先頭に移動し、チェックを行うか、毎回の移動後に比較を実行してから、ループからbreakを実行する必要があります。

次に、いくつかの場所に角括弧{がありません。

次に、n=j+1ではないn=j-2を持っている必要がある場合最初にフェリックスによって指摘したように。

読みやすくするためにコードを少し書き直すことを検討してください。たとえば、移動を行い、成功したことを返す関数を作成することができます。

bool tryToMove(int stepI, int stepJ){ 
    int n = i+stepI; 
    int m = j+stepJ; 
    if (n>=0 && n<8 && m>=0 && m<8 && openSpace(n,m)){ 
    i = n; 
    j = m; 
    board[i][j] = startingPoint; 
    return true; 
    } 
    return false; 
} 

、あなたは

if (tryToMove(-2,1)){ 
    if (startingPoint == 32) break; 
} 
if (tryToMove(-2,-1)){ 
    if (startingPoint == 32) break; 
} 
... 

またはその代わりにcontinueを使用するようにそれを使用することができますが、それは動きの順序を変更します。

あなたのコードよりも比較が少ないでしょうが、何が起こっているかを見るのがはるかに簡単です。