所有内容都可以编译,但是当我运行程序时,它似乎完全跳过了player1函数,直接进入了printBoard函数,当我在循环之前要求player1中需要的变量时,它会将它们带入并且仍然跳过循环。我以前有另一个布尔函数,可以正常工作
#include<iostream>
using namespace std;
char board[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
void printBoard();
bool player1(char);
int main()
{
char m;
char x;
char o;
cout << "Welcome to Tic- Tac- Toe!" << endl;
cout << "Choose your mode: " << endl;
cout << "1: Player vs Player" << endl;
cin >> userCommand;
if (userCommand == 1) {
cout << "Player vs Player" << endl;
cout << "Player 1 will use 'X'" << endl;
cout << "Player 2 will use 'O'" << endl;
printBoard();
while (!player1) {
cout << "Player 1 please enter space: ";
cin >> m;
}
printBoard();
}
else {
cout << "Player vs AI";
}
return 0;
}
void printBoard()
{
cout << board[0] << "|" << board[1] << "|" << board[2] << endl;
cout << "-"
<< " "
<< "-"
<< " "
<< "-" << endl;
cout << board[3] << "|" << board[4] << "|" << board[5] << endl;
cout << "-"
<< " "
<< "-"
<< " "
<< "-" << endl;
cout << board[6] << "|" << board[7] << "|" << board[9] << endl;
}
bool player1(char m)
{
if (board[m] == m) {
board[m] = 'x';
return true;
}
else {
return false;
}
}
答案 0 :(得分:2)
您不是在调用player1,而是在检查它(该功能)是否存在。您需要传递参数...
因此它将是:
while(!player1(m))
(您仍然可以编译,因为按名称引用函数是有效的构造-将其视为指向函数本身的指针-并自动将其转换为bool,其中null转换为false,并将非null转换为true。)