If / Else语句在2d数组中移动的麻烦

时间:2017-02-16 07:57:50

标签: c++ arrays if-statement combinatorics

制作骑士之旅的版本。该程序的行为与我认为给出我的代码的方式不同。我有几个不同的奇怪的差异。虽然我为它设置了参数,但“骑士”已经超出了界限。还有更多的动作比应该给我的while循环。混淆i值,j值和while循环中的数字,你会得到几个奇怪的结果。我现在将值设置为32只是为了看看会发生什么。这是代码和结果:

#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个动作,为什么要离开?

1 个答案:

答案 0 :(得分:1)

我认为问题很少。

首先,你一个接一个地检查允许的移动

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

这意味着在检查if循环之前,多个while语句都可以为真,因此您超过了32的限制。每次移动后都可以使用continue语句到循环的开始并进行检查,或者你必须在每次移动后进行比较,然后从循环中进行break

然后你在几个地方缺少括号{

然后正如第一篇中的felix指出的那样,如果你应该n=j+1而不是n=j-2

您可能需要考虑轻微重写代码以便于阅读。例如,你可以创建一个试图移动的函数,并返回true,它是成功的。

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,但这会改变移动的顺序。

与您的代码相比,将会有更多的比较,但更容易看到发生了什么。