我目前正在尝试用C++制作一个tic-tac-toe AI。我目前正在制作一个函数来检查游戏的winstate,但当我尝试运行它时,我得到了这个错误:
61:34: error: cannot convert ‘std::string’ {aka ‘std::__cxx11::basic_string’} to ‘std::string (*)[3]’ {aka ‘std::__cxx11::basic_string (*)[3]’}
61 | cout << HasWon(Board[3][3], Length);
| ~~~~~~~~~~^
| |
| std::string {aka std::__cxx11::basic_string<char>}
main.cpp:6:19: note: initializing argument 1 of ‘int HasWon(std::string (*)[3], int)’
6 | int HasWon(string GameBoard[3][3], int boardLength);
| ~~~~~~~^~~~~~~~~~~~~~~
字符串
我知道它与char和string有关,但我不知道如何在我当前的程序中实现它。代码如下:
#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;
}
型
1条答案
按热度按时间z9gpfhce1#
问题在于,当作为调用参数传递时,表达式
Board[3][3]
的类型是std::string
,而函数参数GameBoard
的类型实际上是‘std::string (*)[3]
。这是因为string [3][3]
由于类型衰减而衰减为string (*)[3]
。因此,函数
HasWon
的参数类型与传递的参数存在不匹配。在标准C++中,数组的大小必须是一个编译时常量。
字符串
语句
string Board[Length][Length];
不是标准C++,因为Length
不是常量表达式。要解决这个问题,您应该将顶层
const
添加到Length
,并传递参数Board
而不是Board[3][3]
,如下所示:型
Demo的一个。
另一种替代方法(更好)是使用std::vector。