I don't have any questions about this piece of code; it compiles just fine in Visual Studio 2010. I just wanted to post it to see if anyone has any good ideas on how to simplify it, or pointing out any bad practices I have have done.
// Tic Tac Toe Game
#include <iostream>
#include <iomanip>
using namespace std;
class TicTacToe
{
private:
char Board[3][3];
int play();
void playermove();
int turn(int);
int check (int, int);
void boardProgress(char x[][3]);
int Winner(char x[][3]);
int player1, player2, draw, player,winner,done;
int row, col;
public:
int TG, T1, T2, TD;
void intBoard();
int Stats(int);
char playAgain(char);
}playBall;
int main()
{
playBall.TG = 0,playBall.T1 = 0,playBall.T2 = 0,playBall.TD = 0;
int Winner = 0;
// This will only display when the program is first run
cout << "You, are about to witness the program of the century.\n\n";
char done = false;
while (!done)
{
// This will display before each game
cout << "COMMENCE TIC TAC TOE!!!!\n\n"
"Ladies first, please pick the row then the column (1 through 3).\n\n";
// Play a game and remember who won (if anyone)
playBall.intBoard();
//If you want to play again or not
done = playBall.playAgain(done);
}
// Finish
cout << "\n\nThank you for playing!\n";
system ("pause");
return 0;
}
void TicTacToe::intBoard()
{
done = false;
winner = 0;
player = 2;
player1 = 1;
player2 = 2;
draw = 3;
int i,k;
for (k=0;k<3;k++)
{
for (i=0;i<3;i++)
{
Board[k][i]=0;
}
}
play();
}
int TicTacToe::play()
{
int done = false;
boardProgress(Board);
while(!done)
{
playermove();
boardProgress(Board);
done = Winner(Board);
}
Stats(winner);
return 0;
}
void TicTacToe::playermove()
{
int answer = false;
while (answer == false)
{
cout <<"Row: ";
cin >> row;
row--;
cout <<"Colume: ";
cin >> col;
col--;
answer = check(row,col);
}
player = turn(player);
if (player == 1)
Board[row][col] = 'X';
else if (player == 2)
Board[row][col] = 'O';
else
cout<< "Failed.";
}
int TicTacToe::check(int row, int col)
{
if(Board[row][col] == 0)
{
return true;
}
else if (Board[row][col] != 0)
{
cout <<"Can go there, Dude!\n\n";
return false;
}
else
{
cout<< "Going through Check";
Winner(Board);
}
return false;
}
int TicTacToe::turn(int player)
{
switch(player)
{
case 1: player = 2;
{
cout<<"\nPlayers 1 turn.\n\n";
break;
}
case 2: player = 1;
{
cout<<"\nPlayers 2 turn.\n\n";
break;
}
}
return player;
}
int TicTacToe::Winner(char x[][3])
{
winner = 0;
int count = 0;
int a = row;
int b = col;
for (a=0;a<3;a++)
{
for (b=0;b<3;b++)
{
if (Board[a][b]==0)
{
count++;
}
}
}
if (count > 0)
{
int row, col, r, c, d, ro, co, dO;
for (row=0; row<3; row++)
{
r=0;
ro=0;
for (col=0; col<3; col++)
{
if(x[row][col]=='X')
r++;
if(x[row][col]=='O')
ro++;
if (ro==3)
{
winner=2;
}
if (r==3)
{
winner=1;
}
}
}
for (col=0; col<3; col++)
{
r=0;
ro=0;
for (row=0; row<3; row++)
{
if(x[row][col]=='X')
r++;
if(x[row][col]=='O')
ro++;
if (ro==3)
{
winner=2;
}
if (r==3)
{
winner=1;
}
}
}
if (x[0][0]=='X' && x[1][1]=='X' && x[2][2]=='X')
{
winner=1;
}
else if (x[0][0]=='O' && x[1][1]=='O' && x[2][2]=='O')
{
winner=2;
}
else if (x[2][0]=='X' && x[1][1]=='X' && x[0][2]=='X')
{
winner=1;
}
else if (x[2][0]=='O' && x[1][1]=='O' && x[0][2]=='O')
{
winner=2;
}
}
else if (count == 9)
{
cout << "Its a draw, Bummer.";
winner = 3;
}
else
{
cout<< "Next Player go, MEOW!\n\n";
}
if (winner > 0)
{
done = true;
}
return done;
}
int TicTacToe::Stats(int winner)
{
playBall.TG++;
switch(winner)
{
case 1: T1++;
{
break;
}
case 2: T2++;
{
break;
}
case 3: TD++;
{
break;
}
}
cout<<"You have played this many games: " << TG;
cout<<"\nPlayer1 won has won: " << T1 << " games.";
cout<<"\nPlayer2 won has won: " << T2 << " games.";
cout<<"\nYou have had: " << TD << " draws.";
return 0;
}
char TicTacToe::playAgain(char done)
{
cout <<"\n\nWanna play again!?\n\n";
cout <<"Y/N: ";
cin >> done;
if(done == 'Y' || done == 'y')
{
done = false;
}
else
{
done = true;
}
return done;
}
void TicTacToe::boardProgress(char x[][3])
{
cout << " | | \n";
cout << " " << x[0][0] << " | " << x[0][1] << " | " << x[0][2] <<" \n";
cout << " _____|_____|_____\n";
cout << " | | \n";
cout << " " << x[1][0] << " | " << x[1][1] << " | " << x[1][2] <<" \n";
cout << " _____|_____|_____\n";
cout << " | | \n";
cout << " " << x[2][0] << " | " << x[2][1] << " | " << x[2][2] <<" \n";
cout << " | | \n";
}