C++ - 国际象棋主教移动代码错误
C++ - Chess Bishop MoveCode Error?
所以我正在做一个国际象棋游戏,但是我无法让主教棋子正确移动。
这是我的棋盘:
string board[8][8] = {
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "B" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"},
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"} };
这是绘制板的绘图功能。
void Draw()
{
for( int i = 0; i < 8; i++ )
{
for( int j = 0; j < 8; j++ )
std::cout << board[ i ][ j ] << ' ';
std::cout << 'n';
}
cout<<"n";
}
主教运动守则到目前为止。
if (board[x][y] == "B")
{ //Highlight the users chosen piece
board[x][y] = " 33[0;31mB 33[0m";
//Now showing available moves the chosen bishop can move to
for(int counter=1; counter <=7; counter++) {
if(board[x+counter][y+counter] == "_") { //if there is an empty space, then place X to show peice can move there
board[x+counter][y+counter] = "X";
}
else { //if cannot move their ,then break
break;
}
}
}
这是我的问题。它显示了棋子能够在板上的某些位置移动到用户的 X 空间。但是,当该部分位于数组的某些位置时,例如它在电路板代码中的位置。它重叠并在电路板的不同侧显示 X,而不是在没有"_"可用时停止绘制 X。
您需要
检查x + counter
和y + counter
是否属于董事会。
if (board[x][y] == "B")
{ //Highlight the users chosen piece
board[x][y] = " 33[0;31mB 33[0m";
//Now showing available moves the chosen bishop can move to
for(int counter = 1; (x + counter) <= 7 && (y + counter) <= 7; counter++){
if(board[x + counter][y + counter] == "_") {
//if there is an empty space, then place X to show peice can move there
board[x + counter][y + counter] = "X";
}
else { //if cannot move their ,then break
break;
}
}
}
当然,这只标志着一个方向,而主教实际上可以向四个方向移动。
此外,这不会检查路径之间是否有任何部分。
要考虑所有四个方向,您可以创建一个方向矩阵,该矩阵存储每个方向的 x 和 y 变化。
// 4 directions in which bishop can move
int dx[4] = {-1, -1, 1, 1};
int dy[4] = {-1, 1, -1, 1};
if (board[x][y] == "B")
{
// for each direction
for(int dir = 0; dir < 4; dir++) {
// the bishop can move to a maximum of 7 squares
for(int counter = 1; counter < 8; counter++) {
// calculate where the bishop will be
// after moving "counter" number of squares
int new_x, new_y;
new_x = x + dx[dir] * counter;
new_y = y + dy[dir] * counter;
// check if the square lies within the board
if(new_x >= 0 && new_x < 8 && new_y >= 0 && new_y < 8) {
// if there is an empty space, then place X to show peice can move there
if(board[cur_x][cur_y] == "_") {
board[cur_x][cur_y] = "X";
}
// if there is any other piece in between, the bishop can't move further
else {
break;
}
}
// break if the square is outside the board
else {
break;
}
}
}
}
相关文章:
- 可以使用移动语义更改或改进此C++代码吗?
- 是否可以避免在以下代码中复制/移动构造函数的需要?
- 为什么在我的代码中调用复制构造函数而不是移动构造函数?
- C++具有移动和复制构造函数的类中的代码重复
- 初始化不可移动对象数组:为什么这样的代码无法在 GCC 上编译?
- 将代码移动到函数中时,DLL "myApp.exe"中找不到序号错误0xC0000138
- 在将其尺寸较大的向量移动到容量较小的向量之前,是否可以通过使用Reserve()来提高代码性能
- 将代码移动到 Opencv cv::P arallelLoopBody
- 移动返回对象的构造函数会中断C++98 代码?
- 从旧的 C 样式指针移动到C++智能指针,代码几乎没有变化
- 源引擎 - 移动代码:什么是"wishspeed"?
- 如何在c++中将代码片段移动到不同的文件中
- 使用代码移动光标并打开文件夹
- C++ - 国际象棋主教移动代码错误
- 我可以将C++代码(或用C++编写的库)与混合移动应用程序代码混合使用吗
- 该代码调用副本或移动构造函数
- 标志以将IPDB和IOBJ文件从链接时间代码生成优化(LTCG)移动
- C++如何从主系统移动代码
- 防止GCC在基准代码中移动代码
- 移动代码c++