我正在努力寻找一种方法,将用户的输入分解到棋盘上的某个位置。例如(A1)。我想确保他们输入的字母介于A和H之间,以及介于1和8之间的数字。不知道比较ASCII是否是最好的方法?
使用C++,这是我尝试过的片段。 startingp是用户的输入
char startChar = startingp[0];
int SCascii = startChar;
int startInt = startingp[1];
if (!(ascii_A <= SCascii >= ascii_H) || !(1 <= startInt >= 8))
{
cout << "Your inputted move, " << startPos << ", is invalid." << endl;
cout << "Enter the coordinates of the piece you want to move. (eg A1) : ";
cin >> startingp;
cout << endl;
}
最佳答案
这种情况:
(ascii_A <= SCascii >= ascii_H)
是不正确的。至少,我认为这不是您的意思。相反,请执行以下操作:
((ascii_A <= SCascii) && (SCascii >= ascii_H))
除此之外,您的逻辑似乎很合理。
关于c++ - 验证用户输入为坐标(A1…),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61562379/