私は C++ を学んでおり、C# について十分に理解していますが、これまでこの問題に遭遇したことはありません。チェスの駒を架空の盤 (列挙された配列) に配置し、最初に駒が配置されている正方形を割り当てる単純なプログラムを使用すると、座標を求められ、プログラムはその正方形にあるものを返します。正しいピースが表示されますが、非デバッグ モードでは常にクラッシュし、Visual Studio のデバッグでバッファ オーバーランが表示されます。かなり短いので、すべてのコードを示します。
#include <iostream>
#include <string>
using namespace std;
int main() {
enum Chessboard {
Blank,
Pawn,
Rook,
Knight,
Bishop,
King,
Queen
};
Chessboard board[8][8] = { Blank };
for (int x = 1; x < 8; x++)
{
board[1][x] = Pawn;
board[8][x] = Pawn;
}
board[7][0] = Rook;
board[7][1] = Knight;
board[7][2] = Bishop;
board[7][3] = King;
board[7][4] = Queen;
board[7][5] = Bishop;
board[7][6] = Knight;
board[7][7] = Rook;
board[0][0] = Rook;
board[0][1] = Knight;
board[0][2] = Bishop;
board[0][4] = King;
board[0][3] = Queen;
board[0][5] = Bishop;
board[0][6] = Knight;
board[0][7] = Rook;
int X = 0;
int Y = 0;
bool Error = false;
cout << "Enter the coordinates of a square on a chessboard to see what is on there at the start of the game (1 number at a time)" << endl;
do {
cin >> X;
X--;
Error = false;
if (X < 0 || X > 7)
{
cout << "That's not on the board" << endl;
Error = true;
}
} while (Error = false);
do {
cin >> Y;
Y--;
Error = false;
if (Y < 0 || Y > 7)
{
cout << "That's not on the board" << endl;
Error = true;
}
} while (Error = false);
string Name = "";
Chessboard Piece = board[X][Y];
switch (Piece)
{
case Blank: Name = "nothing";
break;
case Pawn: Name = "a Pawn";
break;
case Rook: Name = "a Rook";
break;
case Knight: Name = "a Knight";
break;
case Bishop: Name = "a Bishop";
break;
case King: Name = "a King";
break;
case Queen: Name = "a Queen";
break;
default: Name = "Somehow you missed the board";
break;
}
cout << "On " << ++X << "," << ++Y << " there is " << Name << endl;
return 0;
}