我正在为我的Uni项目制作国际象棋游戏。到目前为止,我只研究了if
/ else
,for
/ while
/ goto
,1D / 2D数组,指针和函数。我只能在代码中使用这些东西。
我已经写了一个代码,发生的问题在这里:
当我将用户的输入作为排名地址时,我试图将它们存储在数组中。我尝试使用8个变量,但是经过3次移动后,第一个ade移动将重置。
此外,我一直收到此错误:
错误2错误LNK1120:1个未解决的外部C:\ Users \ Laptop \ Desktop \ FinalChess \ Debug \ FinalChess.exe 1 1 FinalChess
和
错误1错误LNK2019:函数_main C:\ Users中引用的未解析的外部符号“ int __cdecl gameboard(int,int,int,int,int,int,int,int,int)”(?gameboard @@ YAHHHHHHHHH @ Z) \ Laptop \ Desktop \ FinalChess \ FinalChess \ Source.obj FinalChess
代码如下:
#include <iostream>
#include <string>
using namespace std;
const int BL = 19;
string boardBorder[BL][BL];
//string *rankFile[8][8];
string swapper;
char sp = ' ';
int gameboard( int, int, int, int, int, int, int, int);
void pieceplace()
{
for (int ROW = 0; ROW < BL; ROW++)
{
for (int COL = 0; COL < BL; COL++)
{
if (ROW == 4 && COL>0 && COL<18 && COL % 2 == 0)
boardBorder[ROW][COL] = 'S';
if (ROW == 14 && COL>0 && COL < 18 && COL % 2 == 0)
boardBorder[ROW][COL] = 's';
if (ROW == 2 && COL == 2 || ROW == 2 && COL == 16)
boardBorder[ROW][COL] = 'R';
if (ROW == 16 && COL == 2 || ROW == 16 && COL == 16)
boardBorder[ROW][COL] = 'r';
if (ROW == 2 && COL == 4 || ROW == 2 && COL == 14)
boardBorder[ROW][COL] = 'H';
if (ROW == 16 && COL == 14 || ROW == 16 && COL == 4)
boardBorder[ROW][COL] = 'h';
if (ROW == 2 && COL == 6 || ROW == 2 && COL == 12)
boardBorder[ROW][COL] = 'B';
if (ROW == 16 && COL == 12 || ROW == 16 && COL == 6)
boardBorder[ROW][COL] = 'b';
boardBorder[2][10] = 'K';
boardBorder[16][10] = 'k';
boardBorder[2][8] = 'Q';
boardBorder[16][8] = 'q';
}
}
}
int moves = 1;
void main()
{
int from[197], from2[197], to[197], to2[197], f[197], f2[197], t[197], t2[197];
pieceplace();
gameboard(from[197], from2[197], to[197], to2[197], f[197], f2[197], t[197], t2[197]);
pieceplace();
/*for (int i = 1; i <= 8; i++)
{
for (int j = 1; j <= 8; j++)
{
rankFile[i][j] = &boardBorder[i * 2][j * 2];
}
}*/
for (; moves <= 1972; moves++)
{
cout << "\t\tPlayer white's turn : ";
cout << "Please enter current rank-to-rank address : ";
cin >> from[moves] >> from2[moves];
cout << "\n\t\t\t\t\tPlease enter a new rank-to-rank address : ";
cin >> to[moves] >> to2[moves];
boardBorder[to[moves] * 2][to2[moves] * 2] = boardBorder[from[moves] * 2][from2[moves] * 2];
boardBorder[from[moves] * 2][from2[moves] * 2] = " ";
system("CLS");
gameboard(from[197], from2[197], to[197], to2[197], f[197], f2[197], t[197], t2[197]);
moves++;
cout << "\t\tPlayer black's turn : ";
cout << "Please enter current rank-to-rank address : ";
cin >> f[moves] >> f2[moves];
cout << "\n\t\t\t\t\tPlease enter a new rank-to-rank address : ";
cin >> t[moves] >> t2[moves];
boardBorder[t[moves] * 2][t2[moves] * 2] = boardBorder[f[moves] * 2][f2[moves] * 2];
boardBorder[f[moves] * 2][f2[moves] * 2] = " ";
system("CLS");
gameboard(from[197], from2[197], to[197], to2[197], f[197], f2[197], t[197], t2[197]);
}
cout << "\n\n\n";
}
int gameboard(int from[197], int from2[197], int to[197], int to2[197], int f[197], int f2[197], int t[197], int t2[197])
{
system("color F0");
for (int ROW = 0; ROW < BL; ROW++)
{
for (int COL = 0; COL < BL; COL++)
{
if (ROW >= 1 && ROW <= 17 && COL == 1 || ROW >= 1 && ROW <= 17 && COL == 17)
{
boardBorder[ROW][COL] = "|";
}
else if (ROW >= 1 && ROW % 2 == 0 && COL >= 2 && COL % 2 != 0 && ROW <= 17 && COL <= 16)
{
boardBorder[ROW][COL] = "|";
}
else if (ROW >= 1 && ROW % 2 != 0 && COL >= 2 && COL % 2 != 0 && ROW <= 17 && COL <= 16)
{
boardBorder[ROW][COL] = "+";
}
else if (ROW >= 1 && ROW % 2 != 0 && COL >= 2 && COL % 2 == 0 && ROW <= 17 && COL <= 16)
{
boardBorder[ROW][COL] = "-";
}
else
boardBorder[ROW][COL] = " ";
}
}
int siders = 1, side = 1, upper = 1, up = 1;
int mov = 1;
pieceplace();
boardBorder[to[mov] * 2][to2[mov] * 2] = boardBorder[from[mov] * 2][from2[mov] * 2];
boardBorder[from[mov] * 2][from2[mov] * 2] = " ";
mov++;
boardBorder[t[mov] * 2][t2[mov] * 2] = boardBorder[f[mov] * 2][f2[mov] * 2];
boardBorder[from[mov] * 2][from2[mov] * 2] = " ";
for (int ROW = 0; ROW < BL; ROW++)
{
for (int COL = 0; COL < BL; COL++)
{
if (ROW == 0 && COL == 1 || ROW == 18 && COL == 1)
{
cout << " ";
}
if (ROW >= 2 && ROW <= 16 && ROW % 2 == 0 && COL == 1)
{
cout << siders++;
}
if (ROW >= 2 && ROW <= 16 && ROW % 2 == 0 && COL == 18)
{
cout << side++;
}
if (ROW == 0 && COL >= 2 && COL <= 18 && COL % 2 == 0)
{
if (upper < 9)
cout << "\b" << upper++;
}
if (ROW == 18 && COL >= 2 && COL <= 18 && COL % 2 == 0)
{
if (up < 9)
cout << "\b" << up++;
}
if (COL == 0)
cout << endl;
if (COL == 1 && ROW >= 1 && ROW <= 17 && ROW % 2 == 0)
{
cout << boardBorder[ROW][COL];
}
else
{
cout << " " << boardBorder[ROW][COL];
}
}
}
cout << "\n\n\t\t\t\bBoth Players must use the rank-to-rank address to choose or move a piece.\n\n";
return 0;
}
答案 0 :(得分:1)
您已经声明了一个以int
作为参数的函数:
int gameboard( int, int, int, int, int, int, int, int);
您已经定义了第二个函数,该函数采用int
s的数组:
int gameboard(int from[197], int from2[197], int to[197], int to2[197], int f[197], int f2[197], int t[197], int t2[197])
在main
中,您试图调用需要int
s的函数
gameboard(from[197], from2[197], to[197], to2[197], f[197], f2[197], t[197], t2[197]);
(方括号中的数字是下标,而不是大小。)
您尚未真正定义带有int
参数的函数版本,而尝试调用它会导致链接器错误。
此外,您正在访问有效范围之外的数组元素。例如,from[197]
引用了一个由197个元素组成的数组的第198个元素。