Hi, I'm creating a Tic Tac Toe game and the problem I have so far is that I cant find out how to declare my array for the board in my class =( Here is my code so far. I am getting an error saying that 'Board isnt declared in this scope' so could somebody please help me with declaring Board within my class?
Code:
#include <iostream>
#include <string>
using namespace std;
class PlayerClass{
public: //public consists of functions within the playerclass
//initilises the board at the start of the game to be all 0
void initialBoard(){
int Board[3][3] = {{0,0,0},{0,0,0},{0,0,0}};
for(int row = 0; row < 3; row++){
cout<<" ";
for(int column = 0; column < 3; column++){
cout<<Board[row][column]<< " ";}
cout<<endl;}}
//prints out the board to show changed when a player move has made
void printBoard(int Board[3][3]){
for(int row = 0; row < 3; row++){
cout<<" ";
for(int column = 0; column<3;column++){
cout<<Board[row][column]<< " ";}
cout<<endl;}}
//function to ask the player where they wish to place their shape and sets it to the board
void PlayerTurn(int turn){
if (turn == 1){cout<<"which position do you want to place your letter"<<endl;
cout<<"enter in x coordinate";
cin>>x;
if(x<1||x>3){cout<<"please enter number between 1 and 3";
cin>>x;}
cout<<"enter in y coordinate";
cin>>y;
if (y<1||y>3){cout<<"please enter in a valid coordinate between 1 and 3";
cin>>y;}
else
Board[x][y]=player1;
}
else
if (turn == 2){cout<<"which position do you want to place your letter"<<endl;
cout<<"enter in x coordinate";
cin>>x;
if(x<1||x>3){cout<<"please enter number between 1 and 3";
cin>>x;}
cout<<"enter in y coordinate";
cin>>y;
if (y<1||y>3){cout<<"please enter in a valid coordinate between 1 and 3";
cin>>y;}
else
Board[x][y]=player2;}}
private: //private consists of variables used by the functions playerclass
char move;
int x;
int y;
int turn;
char player1;
char player2;
};
int main()
{
int Board[3][3] = {{0,0,0},{0,0,0},{0,0,0}};
PlayerClass Pmove;
cout<<" Tic-Tac-Toe"<<endl<<endl;
Pmove.initialBoard();
Pmove.PlayerTurn(1);
Pmove.printBoard(Board); //needs to print the board to show where player has moved
Pmove.PlayerTurn(2);
return 0;
}
Thanks so much =)