Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

tic tac toe code please help me

i need #include
using namespace std;
class Matrix


{
private:
char board[3][3];
int i, j;
int turn; //player 1 == 1; player 2 == 2
char play;
public:
Matrix ();
bool checkEmpty (int, int); //check if the user selected place is empty or not
void getInput (); //get input character from user
void display (); //display the matrix
void playGame ();
void checkWin ();
void nextPlayer ();
};
Matrix::Matrix ()


{
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
board[i][j] = ''; //initialize all the position to null
turn = 1; //player 1 play first
play = 'O';
}
bool Matrix::checkEmpty (int a, int b)


{
if (board[a][b] == '')
return true;
else
return false;
}
void Matrix::getInput ()


{
bool empty;

cout<<"Enter number of Row: ";
cin>>i;
cout<<"Enter number of Column: ";
cin>>j;

empty = checkEmpty (i, j);
if (empty == true)
board[i][j] = play;
else


{
cout<<"Already occupied!"<<endl;
nextPlayer ();
}
}
void Matrix::display ()


{
cout<<"
-----+-------+-------+-------+"<<endl;
cout<<" | Col 0 | Col 1 | Col 2 |"<<endl;
cout<<"-----+-------+-------+-------+"<<endl;
for (i = 0; i < 3; i++)


{
cout<<"Row "<<i<<"|";
for (j = 0; j < 3; j++)


{
cout<<""<<board[i][j];
cout<<"|";
}

cout<<"
-----+-------+-------+-------+"<<endl;
}
}
void Matrix::playGame ()


{
cout<<"Player ["<<turn<<"] please enter move: "<<endl;
getInput ();
checkWin ();
display ();
nextPlayer ();
}
void Matrix::nextPlayer ()


{
if (turn == 1)


{
turn = 2;
play = 'X';
}
else


{
turn = 1;
play = 'O';
}
}
void Matrix::checkWin ()


{
if ((board[0][0] == play && board[1][0] == play && board[2][0] == play) ||
(board[0][1] == play && board[1][1] == play && board[2][1] == play) ||
(board[0][2] == play && board[1][2] == play && board[2][2] == play) ||
(board[0][0] == play && board[0][1] == play && board[0][2] == play) ||
(board[1][0] == play && board[1][1] == play && board[1][2] == play) ||
(board[2][0] == play && board[2][1] == play && board[2][2] == play) ||
(board[0][0] == play && board[1][1] == play && board[2][2] == play) ||
(board[0][2] == play && board[1][1] == play && board[2][0] == play))


{
cout<<"
Player ["<<turn<<"] win!"<<endl;
exit (1);
}
}
int main ()


{
Matrix m;
char choice;

m.display ();
do


{
m.playGame ();
cout<<"
Do you want to continue?";
cin>>choice;
cout<<endl;
} while (choice != 'N' && choice != 'n');
cout<<"
Thank you for your playing....."<<endl;
return 0;
}
the code in simple code more than that i mean without class

Comments

  • kc2keokc2keo Member Posts: 110
    What compiler are you using? What errors return. I would compile this code myself but i'm inconviently on my fathers laptop without a compiler. If you post the errors returned then I might be able to point out what went wrong.

    Bye,
    George

    <------------------------------------------>

    Reach me at:

    AIM IM: kc2keo
    EMAIL: kc2keo@yahoo.com

Sign In or Register to comment.