Hello everyone. I'm working on the infamous TicTacToe program. I have created a class(TTT) with it's private and public members. The program is human v. computer. In the hmove function I ask for the coordinates (0,0;1,2; etc.). I'm not sure how to pass the user selection to the printb function to recognize the move.
My code is posted below.
#include <iostream>
#include <time.h>
using namespace std;
class TTT
{
private:
int board[3][3];
public:
~TTT(){};
TTT();
void printb();
void hmove();
void cmove();
void checkwin(int move);
void instructions();
};
TTT::TTT()
{
cout<<"This is the constructor function....."<<endl;
int i=0,j=0;
for (i=0;i<4;++i)
for (j=0;j<4;++j)
{
board[i][j]=0;
}
for (i=0;i<3;++i)
{
cout<<" "<<board[i][j]<<" |"<<" "<<board[i][j]<<" |"<<" "<<board[i][j]<<" "<<endl;
cout<<"__"<<"_|_"<<"__|__"<<"_"<<endl;
}
cout<<" | | "<<endl<<endl;
}
void TTT::printb()
{
int i=0,j=0;
for (i=0;i<3;++i)
{
cout<<" "<<board[i][j]<<" |"<<" "<<board[i][j]<<" |"<<" "<<board[i][j]<<" "<<endl;
cout<<"__"<<"_|_"<<"__|__"<<"_"<<endl;
}
cout<<" | | "<<endl<<endl;
}
void TTT::checkwin(int move)
{
if ((board[0][0]==1&&board[0][1]==1&&board[0][2]==1)||(board[1][0]==1&&board[1][1]==1&&board[1][2]==1)||(board[2][0]==1&&board[2][1]==1&&board[2][2]==1));
cout<<"COMPUTER WINS!!"<<endl;
move=10;
if ((board[0][0]==2&&board[0][1]==2&&board[0][2]==2)||(board[1][0]==2&&board[1][1]==2&&board[1][2]==2)||(board[2][0]==2&&board[2][1]==2&&board[2][2]==2));
cout<<"HUMAN WINS!!"<<endl;
move=10;
if ((board[0][0]==1&&board[1][1]==1&&board[2][2]==1)||(board[0][2]==1&&board[1][1]==1&&board[0][0]==1))
cout<<"COMPUTER WINS!!"<<endl;
move=10;
if ((board[0][0]==2&&board[1][1]==2&&board[2][2]==2)||(board[0][2]==2&&board[1][1]==2&&board[0][0]==2))
cout<<"HUMAN WINS!!"<<endl;
move=10;
}
void TTT::hmove()
{
int x,y;
cout<<"Enter the coordinates of your move by row and column: "<<endl;
cin>>x>>y;
if(board[x][y]!=0)
{
cout<<"Invalid move. Pick another move"<<endl;
}
else
board[x][y]==2;
// for (int i=0;i<3;++i)
// for (int j=0;j<3;++j)
// {
// cout<<board[i][j];
// }
}
void TTT::cmove()
{
cout<<"Computer move."<<endl;
}
void TTT::instructions()
{
cout<<"The instructions for Tic Tac Toe are as follows: "<<endl;
cout<<"The computer will go first each game and will be represented by a 1."<<endl;
cout<<"The human is represented by a 2."<<endl;
cout<<"Continue until there are three of the same number in a row or the board is full.";
cout<<"If the board is full and noone wins try again!!"<<endl<<endl;
}
void main()
{
int move=0;
TTT play_game;
cout<<"Welcome to the TicTacToe Program."<<endl<<endl;
play_game.instructions();
// game.TTT();
play_game.hmove();
game.printb();
}
Thanks,
J