我目前正试图在c++中制作一个tic tac。我目前正在创建一个函数来检查游戏的winstate,但是当我尝试运行它时,我会得到以下错误:
61:34:错误:不能将‘std::string’{aka‘std::__cxx11::basic_string’}转换为‘std::string()3’{aka‘std::__cxx11::basic_string()3’} 61 cout << HasWon(Board3,Length);\x {aka std::__cxx11::basic_string} main.cpp:6:19:注意:初始化‘int HasWon’的参数1( std::string (*)3,int )‘6 x int HasWon(string GameBoard3,int boardLength);x ~^~~
我知道这与字符和字符串有关,但我不知道如何在我当前的程序中实现它。这是代码:
#include <iostream>
using namespace std;
int AIMove(int PlayerMove);
int HasWon(string GameBoard[3][3], int boardLength);
int main () {
int Length = 3;
string Board[Length][Length] = {
{" ", " ", " "},
{" ", " ", " "},
{" ", " ", " "},
};
int NodeMap[Length][Length] = {
{0,0,0},
{0,0,0},
{0,0,0},
};
int NodeNumber = 1;
for(int h = Length - 1; h >= 0;h--) {
for(int d = 0; d < Length;d++) {
NodeMap[h][d] = NodeNumber;
NodeNumber++;
}
}
int Player;
bool HasMoved = false;
bool run = true;
while(run) {
//Prints Tic Tac Toe board to the screen
for(int h = 0; h < Length;h++) {
cout << " ";
for(int d = 0; d < Length;d++) {
cout << Board[h][d];
if(d < Length - 1) { cout <<"||"; }
}
if(h < Length - 1) { cout << "\n---------\n"; }
}
//Gets player input
cout << "\n choose a number 1-9 to place an X: ";
cin >> Player;
//Adds it to the board
HasMoved = false;
while(!HasMoved) {
for(int h = 0; h < Length; h++) {
for(int d = 0; d < Length; d++) {
if(NodeMap[h][d] == Player && Board[h][d] == " ") {
Board[h][d] = "X";
HasMoved = true;
}
}
}
if(!HasMoved) {
cout << "\n choose a number 1-9 to place an X: ";
cin >> Player;
}
}
cout << HasWon(Board[3][3], Length);
}
}
int AIMove(int PlayerMove, int boardLength);
//Checks if anyone has won yet. 0: nothing has happend, 1: tie, 2: X win, 3: O win.
int HasWon(string GameBoard[3][3], int boardLength) {
int xNumber = 0;
int oNumber = 0;
//Checks for vertical wins
for(int d = 0;d < boardLength;d++) {
xNumber = 0;
oNumber = 0;
for(int h = 0;h < boardLength;h++) {
if(GameBoard[h][d] == "X") { xNumber++; }
if(GameBoard[h][d] == "O") { oNumber++; }
if(xNumber == boardLength ) { return 2; }
if(oNumber == boardLength ) { return 3; }
}
}
//Checks for horizontial wins
for(int h = 0;h < boardLength;h++) {
xNumber = 0;
oNumber = 0;
for(int d = 0;d < boardLength;d++) {
if(GameBoard[h][d] == "X") { xNumber++; }
if(GameBoard[h][d] == "O") { oNumber++; }
if(xNumber == boardLength ) { return 2; }
if(oNumber == boardLength ) { return 3; }
}
}
//Checks for diagonal wins
xNumber = 0;
oNumber = 0;
for(int i = boardLength - 1;i >= 0;i--) {
if(GameBoard[i][i] == "X") { xNumber++; }
if(GameBoard[i][i] == "O") { oNumber++; }
if(xNumber == boardLength ) { return 2; }
if(oNumber == boardLength ) { return 3; }
}
xNumber = 0;
oNumber = 0;
for(int i = 0;i < boardLength;i++) {
if(GameBoard[i][i] == "X") { xNumber++; }
if(GameBoard[i][i] == "O") { oNumber++; }
if(xNumber == boardLength ) { return 2; }
if(oNumber == boardLength ) { return 3; }
}
//Checks for a tie
xNumber = 0;
oNumber = 0;
for(int h = 0;h < boardLength;h++) {
for(int d = 0;d < boardLength;d++) {
if(GameBoard[h][d] == "X") { xNumber++; }
if(GameBoard[h][d] == "O") { oNumber++; }
if(xNumber+oNumber == boardLength*boardLength) { return 1; }
}
}
return 0;
}发布于 2022-03-26 05:51:33
问题是,当作为调用参数传递时,表达式Board[3][3]的类型是std::string,而函数参数GameBoard的类型实际上是‘std::string (*)[3]。这是因为string [3][3] 由于类型的衰变而使衰变为string (*)[3]。
因此,在函数的参数类型和传递的参数中存在失配。
还请注意,在标准c++中,数组的大小必须是编译时间常数。所以在你的代码中:
int Length = 3;
string Board[Length][Length]; //this is not standard C++语句string Board[Length][Length];不是标准的C++,因为Length不是常量表达式。
要解决问题,您应该向Length添加一个顶级const,并传递参数Board而不是Board[3][3],如下所示:
//-vvvvv---------------------------> const added here
const int Length = 3;
//----------------vvvvv------------> changed to Board instead of Board[3][3]
cout << HasWon(Board, Length);演示。
另一个更好的选择是使用std::vector。
https://stackoverflow.com/questions/71625486
复制相似问题