I am currently trying to make a tic-tac-toe AI in C++. I am currently making a function to check if the winstate of the game, but when I try to run it, I get this error:
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);
| ~~~~~~~^~~~~~~~~~~~~~~
I know that it has to do with char and strings, but I don't have any idea how to implement it in my current program. Here’s the code:
#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;
}
HasWon(Board[3][3], Length)->HasWon(Board, Length)andint Length = 3;->const int Length = 3;. I don't know what your intention with the[3][3]indices is and array sizes must be compile-time constants in standard C++.intwithoutconstcannot be a constant at all.