骑士团索引和移动算法错误



此程序的目标是让骑士在胸板周围移动,并且只接触每个点一次。

默认情况下,每个点都被初始化并设置为零。当骑士移动时,骑士接触的每个点都应该与达到该点所需的移动次数相对应。

然而,我遇到了不少问题

1) 我的骑士在棋盘上移动,要么离开多维棋盘阵列的边界,要么操纵

2) 我的布尔函数MoveOnBoard&MoveHasNotBeenMade是指如果下一个可能的移动在现有的行和列之间,以及如果要移动到的点的值为0(意味着它还没有移动到)。然而,这两种情况似乎被忽视了。

我该如何解决这个问题

提前谢谢!

这是下面的代码

using namespace std;
#include <iostream>
#include <array>
void DefinedMoveSet();
void RenderBoard();
void MoveKnight(int& moveChoice, int& numberOfMovesMade);
void PossibleMoves();
bool MoveOnBoard(int& moveChoice);
bool MoveHasNotBeenMade(int& moveChoice);
// Two single dimenisional arrays to store move positions for the Knight
// Arrays have yet to be assigned values
int vertical[8], horizontal[8];
int currentRow = 4, currentColumn = 3;
// Initializing an array with the dimension 8 * 8
int chestBoard[8][8] = { 0 };
int main()
{
DefinedMoveSet();
PossibleMoves();
RenderBoard();
cin.ignore();
return 0;
}
void RenderBoard()
{
// The outer loop goes through each row until it reaches 8
for (int boardRow = 0; boardRow < 8; boardRow++)
{
// The inner loop takes in the specific row 
for (int boardColumn = 0; boardColumn < 8; boardColumn++)
{
// Then iterates through the columns of that row until it reaches 8
// Each index is seperated by a tab escape key shortcut
cout << chestBoard[boardRow][boardColumn] << "t";
}
// Back to the inner array a new line is printed for the next row
cout << "n";
}
}
void DefinedMoveSet()
{
// Values for the horizontal array at each index
horizontal[0] = 2;
horizontal[1] = 1;
horizontal[2] = -1;
horizontal[3] = -2;
horizontal[4] = -2;
horizontal[5] = -1;
horizontal[6] = 1;
horizontal[7] = 2;
// Values for the vertical array at each index
vertical[0] = -1;
vertical[1] = -2;
vertical[2] = -2;
vertical[3] = -1;
vertical[4] = 1;
vertical[5] = 2;
vertical[6] = 2;
vertical[7] = 1;
}
bool MoveOnBoard(int& moveChoice)
{
int futureRow = currentRow + vertical[moveChoice];
int futureColumn = currentColumn + horizontal[moveChoice];
if ((0 < futureRow) && (0 < futureColumn) && (futureRow < 8) && (futureColumn < 8))
return true;
}
bool MoveHasNotBeenMade(int& moveChoice)
{
int futureRow = currentRow + vertical[moveChoice];
int futureColumn = currentColumn + horizontal[moveChoice];
if (chestBoard[futureRow][futureColumn] == 0)
return true;
}
void PossibleMoves()
{
bool movesStillExist = true;
int numberOfMovesMade = 1;
while (numberOfMovesMade < 65 && movesStillExist)
{
for (int i = 0; i < 8; i++)
{
if (i == 8)
movesStillExist = false;
if (MoveOnBoard(i) && MoveHasNotBeenMade(i))
{
numberOfMovesMade++;
MoveKnight(i, numberOfMovesMade);
}
}
}
}
void MoveKnight(int &moveChoice, int &numberOfMovesMade)  
{
// Takes in the int moveNumber as a parameter
// MoveNumber(or case) must be between 0 and 7
// if there is not a case for the value then the knight will not move
//chestBoard[currentRow][currentColumn] = numberOfMovesMade;
currentRow += vertical[moveChoice];
currentColumn += horizontal[moveChoice];
chestBoard[currentRow][currentColumn] = numberOfMovesMade;
}

在MoveOnBoardand和MoveHasNotBeenMade中而不是中

if(...)
return true;

应该是

if(...)
return true;
return false;

如果condition==false,则函数返回not void reach end without return语句。

根据我收到的评论中的建议,我能够修复索引问题以及布尔函数的返回值。

我的主要问题是移动后没有突破上一个循环

通过此if语句轻松解决

if (MoveOnBoard(i) && MoveHasNotBeenMade(i))
{
MoveKnight(i);
break;
}

我试图通过告诉编译器来实现这一点

if (i == 8)
movesStillExist = false;

正如@Azuth所指出的,这个条件永远不会得到满足,因为在该索引处不存在移动。

因此,出于我的目的,我将条件更改为

if (i == 7)
movesStillExist = false;

同样对于指数问题,我的逻辑有点偏离

if (((0 <= futureRow) && (0 <= futureColumn)) && ((futureRow < 8) && (futureColumn < 8)))
return true; // if the future row and column are in bounds then return true
return false; // else the default is false

此外,我的代码对于c++来说并不理想。

有这么多全局变量而没有足够的注释。

请理解,需要使用单个和多维数组,因为这对我的c++课程来说是一个挑战。

bool MoveOnBoard(int& moveChoice)
{
int futureRow = currentRow + vertical[moveChoice];
int futureColumn = currentColumn + horizontal[moveChoice];
if (((0 <= futureRow) && (0 <= futureColumn)) && ((futureRow < 8) && (futureColumn < 8)))
return true;
return false;
}
bool MoveHasNotBeenMade(int& moveChoice)
{
int futureRow = currentRow + vertical[moveChoice];
int futureColumn = currentColumn + horizontal[moveChoice];
if (chestBoard[futureRow][futureColumn] == 0)
return true;
return false;
}
void PossibleMoves()
{
bool movesStillExist = true;
while (numberOfMovesMade < 65 && movesStillExist)
{
for (int i = 0; i < 8; i++)
{
if (MoveOnBoard(i) && MoveHasNotBeenMade(i))
{
MoveKnight(i);
break;
}
if (i == 7)
movesStillExist = false;
}
}
}
void MoveKnight(int &moveChoice)
{
// Takes in the int moveNumber as a parameter
// MoveNumber(or case) must be between 0 and 7
// if there is not a case for the value then the knight will not move
chestBoard[currentRow][currentColumn] = numberOfMovesMade;
numberOfMovesMade++;
currentRow += vertical[moveChoice];
currentColumn += horizontal[moveChoice];
chestBoard[currentRow][currentColumn] = numberOfMovesMade;
}

最新更新