I am trying to make a tic tac toe game in C++. I am trying to use a 2d char
array to construct the board, but when my code below goes to compile, I get an error error: cannot convert '<brace-enclosed initializer list>' to 'char' in assignment
Below is the code:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
// Player Class
class Player
{
public:
explicit Player(string name){
this->name = name;
}
string getName(){
return name;
}
string setName(string newname){
name = newname;
}
int generateRowMovement(){
int row = rand() % 2; // Random number between 0 and 2
return row;
}
int generateColMovement(){
int col = rand() % 2; // Random number between 0 and 2
return col;
}
int placeMove(){
int rowMove, colMove;
rowMove = rand() % 2;
colMove = rand() % 2;
return rowMove, colMove;
}
private:
string name;
};
//Board Class
class Board
{
public:
explicit Board(){
this->state[3][3] = {
{'.', '.', '.'},
{'.', '.', '.'},
{'.', '.', '.'}}
} // end board
void printBoard(char state[3][3]){
for (int i =0; i < 3; i ++){
for (int j =0; j < 3; j++){
cout << state[i][j] << " ";
}
cout << endl;
}
} //end printboard
void printBoard(){
for (int i =0; i < 3; i ++){
for (int j =0; j < 3; j++){
cout << this->state[i][j] << " ";
} //end inner j for
cout << endl;
} // end outer i for
} //end printboard
private:
char state[3][3];
};
int main(){
// Player Tony("Tony");
// cout << Tony.getName();
Player Tony("Tony");
Board game();
cout << Tony.getName() << endl;
Tony.setName("Bob");
cout << Tony.getName() << endl;
int currRow, currCol;
currRow = Tony.generateRowMovement();
currCol = Tony.generateColMovement();
cout << currRow << currCol << endl;
return 0;
} //end main
If I add the following block to main
(and adjust some other things):
char grid[3][3] = { {'.', '.', '.'},
{'.', '.', '.'},
{'.', '.', '.'}};
for (int i =0; i < 3; i ++){
for (int j =0; j < 3; j++){
cout << grid[i][j] << " ";
} //end inner j for
cout << endl;
} // end outer i for
I get the expected result of:
. . .
. . .
. . .
Why can I not make the default construction of this object a [3][3] char
array like above?