I just recently completed a tic tac toe program that I wish to change so that it uses classes. Can someone give me a hand in how I should do this? I have never used classes before in my programs and need to have an idea of how they work.
Here is my code
Code:
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int option, test, row, col, turn, option2 = 0;
char board[3][3];
//Prints the board to the screen
void drawBoard()
{
cout<<"\n";
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
cout<<"|"<<board[i][j]<<"| ";
}
cout<<"\n-------------\n";
}
}
//Function checks for a winner if the returned is: 1 if X wins, 2 if O wins, 3 if tie
int checkWinner()
{
//Checks to see if X won
int r=0;
//Checks for all possibilities for X to win in rows
if(board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')
r=1;
if(board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')
r=1;
if(board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
r=1;
//Checks for all possibilities for X to win in columns
if(board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')
r=1;
if(board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')
r=1;
if(board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')
r=1;
//Checks for all possibilities for X to win diagonally
if(board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')
r=1;
if(board[2][0] == 'X' && board[1][1] == 'X' && board[0][2] == 'X')
r=1;
//Checks to see if O won
//Checks for all possibilities for O to win in rows
if(board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')
r=2;
if(board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O')
r=2;
if(board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')
r=2;
//Checks for all possibilities for O to win in columns
if(board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O')
r=2;
if(board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O')
r=2;
if(board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O')
r=2;
//Checks for all possibilities for O to win diagonally
if(board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O')
r=2;
if(board[2][0] == 'O' && board[1][1] == 'O' && board[0][2] == 'O')
r=2;
if(r==1)
return 1;
if(r==2)
return 2;
//Checks for a tie
int s=0;
int i, j;
for (i=0; i<3; i++)
for (j=0; j<3; j++)
{
if ((board[i][j] == 'X') || (board[i][j] == 'O'))
s++;
}
if (r!=1 && r!=2 && s==9)
return 3;
}
//Computer generates a random move and see if it's available. If it is, then the move is made.
//If it isn't then it generates another random move.
void Computer()
{
col = -1;
row = -1;
while(col == -1 || row == -1)
{
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
col = rand()%3;
row = rand()%3;
if(board[row][col] != ' ')
{
col = -1;
row = -1;
}
board[row][col] = 'O';
}
}
int main()
{
do
{
option2 = 0;//Allows the game to be played multiple times.
cout<<"Enter a menu option:\n";
cout<<"-----------------------------\n\n";
cout<<"1. Play Tic Tac Toe\n";
cout<<"2. Quit\n";
cin>>option;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
board[i][j] = ' ';
}
}
if(option !=2)
{
while(option2 != -1)//Game will continue until someone wins or it is a tie.
{
if(turn%2 == 0)//If it's even then it's X's turn.
{
bool condition = true;
while(condition == true)
{
cout<<"\n\nEnter a move:\n";
cout<<"Enter the row you wish to make your move on (0,1,2)\n";
cin>>row;
cout<<"Enter the column you wish to make your move on (0,1,2)\n";
cin>>col;
if(board[row][col] == ' ')//Checks to see if the move is valid
{
board[row][col] = 'X';
turn++;
condition = false;
}
else
cout<<"Invalid move!\n";
}
}
else//The computer makes a move.
{
Computer();
turn++;
}
if(checkWinner() == 1)//X wins
{
cout<<"Congratulations! You won!\n\n";
option2 = -1;
}
else if(checkWinner() == 2)//O wins
{
cout<<"Sorry, but the computer won.\n\n";
option2 = -1;
}
else if(checkWinner() == 3)//It's a tie
{
cout<<"It seems we have a tie.\n\n";
option2 = -1;
}
drawBoard();
}
}
}while(option != 2);
cout<<"\n\n\n\nPress Enter to quit.";
cin.get();
}