15+ CPP Tic Tac Toe Examples

This post contains a total of 15+ Hand-Picked C++ Tic Tac Toe examples with Source Code. All the Tic Tac Toe programs are made using C++ Programming Language.

You can use the source code of these programs for educational purpose with credits to the original owner.

Related Posts

Click a Code to Copy it.

1. By Sandeep

Made by Sandeep. A simple Tic Tac Toe program that runs itself and gives different outcomes every time. It shows the moves a human choose and the computer choose and at last announces the winner. ( Source )

// A C++ Program to play tic-tac-toe

#include<bits/stdc++.h>
using namespace std;

#define COMPUTER 1
#define HUMAN 2

#define SIDE 3 // Length of the board

// Computer will move with 'O'
// and human with 'X'
#define COMPUTERMOVE 'O'
#define HUMANMOVE 'X'

// A function to show the current board status
void showBoard(char board[][SIDE])
{
	printf("\n\n");
	
	printf("\t\t\t %c | %c | %c \n", board[0][0],
							board[0][1], board[0][2]);
	printf("\t\t\t--------------\n");
	printf("\t\t\t %c | %c | %c \n", board[1][0],
							board[1][1], board[1][2]);
	printf("\t\t\t--------------\n");
	printf("\t\t\t %c | %c | %c \n\n", board[2][0],
							board[2][1], board[2][2]);

	return;
}

// A function to show the instructions
void showInstructions()
{
	printf("\t\t\t Tic-Tac-Toe\n\n");
	printf("Choose a cell numbered from 1 to 9 as below"
			" and play\n\n");
	
	printf("\t\t\t 1 | 2 | 3 \n");
	printf("\t\t\t--------------\n");
	printf("\t\t\t 4 | 5 | 6 \n");
	printf("\t\t\t--------------\n");
	printf("\t\t\t 7 | 8 | 9 \n\n");
	
	printf("-\t-\t-\t-\t-\t-\t-\t-\t-\t-\n\n");

	return;
}


// A function to initialise the game 
void initialise(char board[][SIDE], int moves[])
{
	// Initiate the random number generator so that 
	// the same configuration doesn't arises
	srand(time(NULL)); 
	
	// Initially the board is empty
	for (int i=0; i<SIDE; i++)
	{
		for (int j=0; j<SIDE; j++)
			board[i][j] = ' ';
	}
	
	// Fill the moves with numbers
	for (int i=0; i<SIDE*SIDE; i++)
		moves[i] = i;

	// randomise the moves
	random_shuffle(moves, moves + SIDE*SIDE);
	
	return;
}

// A function to declare the winner of the game
void declareWinner(int whoseTurn)
{
	if (whoseTurn == COMPUTER)
		printf("COMPUTER has won\n");
	else
		printf("HUMAN has won\n");
	return;
}

// A function that returns true if any of the row
// is crossed with the same player's move
bool rowCrossed(char board[][SIDE])
{
	for (int i=0; i<SIDE; i++)
	{
		if (board[i][0] == board[i][1] &&
			board[i][1] == board[i][2] && 
			board[i][0] != ' ')
			return (true);
	}
	return(false);
}

// A function that returns true if any of the column
// is crossed with the same player's move
bool columnCrossed(char board[][SIDE])
{
	for (int i=0; i<SIDE; i++)
	{
		if (board[0][i] == board[1][i] &&
			board[1][i] == board[2][i] && 
			board[0][i] != ' ')
			return (true);
	}
	return(false);
}

// A function that returns true if any of the diagonal
// is crossed with the same player's move
bool diagonalCrossed(char board[][SIDE])
{
	if (board[0][0] == board[1][1] &&
		board[1][1] == board[2][2] && 
		board[0][0] != ' ')
		return(true);
		
	if (board[0][2] == board[1][1] &&
		board[1][1] == board[2][0] &&
		board[0][2] != ' ')
		return(true);

	return(false);
}

// A function that returns true if the game is over
// else it returns a false
bool gameOver(char board[][SIDE])
{
	return(rowCrossed(board) || columnCrossed(board)
			|| diagonalCrossed(board) );
}

// A function to play Tic-Tac-Toe
void playTicTacToe(int whoseTurn)
{
	// A 3*3 Tic-Tac-Toe board for playing 
	char board[SIDE][SIDE];
	
	int moves[SIDE*SIDE];
	
	// Initialise the game
	initialise(board, moves);
	
	// Show the instructions before playing
	showInstructions();
	
	int moveIndex = 0, x, y;
	
	// Keep playing till the game is over or it is a draw
	while (gameOver(board) == false && 
			moveIndex != SIDE*SIDE)
	{
		if (whoseTurn == COMPUTER)
		{
			x = moves[moveIndex] / SIDE;
			y = moves[moveIndex] % SIDE;
			board[x][y] = COMPUTERMOVE;
			printf("COMPUTER has put a %c in cell %d\n",
					COMPUTERMOVE, moves[moveIndex]+1);
			showBoard(board);
			moveIndex ++;
			whoseTurn = HUMAN;
		}
		
		else if (whoseTurn == HUMAN)
		{
			x = moves[moveIndex] / SIDE;
			y = moves[moveIndex] % SIDE;
			board[x][y] = HUMANMOVE;
			printf ("HUMAN has put a %c in cell %d\n",
					HUMANMOVE, moves[moveIndex]+1);
			showBoard(board);
			moveIndex ++;
			whoseTurn = COMPUTER;
		}
	}

	// If the game has drawn
	if (gameOver(board) == false && 
			moveIndex == SIDE * SIDE)
		printf("It's a draw\n");
	else
	{
		// Toggling the user to declare the actual
		// winner
		if (whoseTurn == COMPUTER)
			whoseTurn = HUMAN;
		else if (whoseTurn == HUMAN)
			whoseTurn = COMPUTER;
		
		// Declare the winner
		declareWinner(whoseTurn);
	}
	return;
}

// Driver program
int main()
{
	// Let us play the game with COMPUTER starting first
	playTicTacToe(COMPUTER);
	
	return (0);
}

2. By Mooaholic

Made by Mooaholic. ( Source )

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

char b[3][3] = {'1','2','3','4','5','6','7','8','9'};


void draw(){
    cout<<"+-----------+"<<endl;
    for (int i = 0; i < 3; i++){
        cout<<"| ";
        for (int j = 0; j < 3; j++){
            cout<<b[i][j]<<" | ";
        }
    cout<<endl;
    cout<<"+-----------+"<<endl;
    }
}

void clearboard(){
    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 3; j++){
            b[i][j] = ' ';
        }
    }
}
void resetboard(){
    int m = '1';
    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 3; j++){
            b[i][j] = m;
            m++;
        }
    }
}
int checkwin(){
    int win = 0;
    if (b[0][0] == b[0][1] && b[0][1] == b[0][2]){ if (b[0][0] == 'X') return 1; if (b[0][0] == 'O') return 2; }
    if (b[1][0] == b[1][1] && b[1][1] == b[1][2]){ if (b[1][0] == 'X') return 1; if (b[1][0] == 'O') return 2; }
    if (b[2][0] == b[2][1] && b[2][1] == b[2][2]){ if (b[2][0] == 'X') return 1; if (b[2][0] == 'O') return 2; }
    if (b[0][0] == b[1][0] && b[1][0] == b[2][0]){ if (b[0][0] == 'X') return 1; if (b[0][0] == 'O') return 2; }
    if (b[0][1] == b[1][1] && b[1][1] == b[2][1]){ if (b[0][1] == 'X') return 1; if (b[0][1] == 'O') return 2; }
    if (b[0][2] == b[1][2] && b[1][2] == b[2][2]){ if (b[0][2] == 'X') return 1; if (b[0][2] == 'O') return 2; }
    if (b[0][0] == b[1][1] && b[1][1] == b[2][2]){ if (b[0][0] == 'X') return 1; if (b[0][0] == 'O') return 2; }
    if (b[0][2] == b[1][1] && b[1][1] == b[2][0]){ if (b[0][2] == 'X') return 1; if (b[0][2] == 'O') return 2; }
}
void bypass(){
    int t = 1;
    while(t == 1){
        int r = (rand()%9)+1;
        if ((r == 1)&&(b[0][0] == ' ')){
            b[0][0] = 'O';
            t = 0;
        }
        else if ((r == 2)&&(b[0][1] == ' ')){
            b[0][1] = 'O';
            t = 0;
        }
        else if ((r == 3)&&(b[0][2] == ' ')){
            b[0][2] = 'O';
            t = 0;
        }
        else if ((r == 4)&&(b[1][0] == ' ')){
            b[1][0] = 'O';
            t = 0;
        }
        else if ((r == 5)&&(b[1][1] == ' ')){
            b[1][1] = 'O';
            t = 0;
        }
        else if ((r == 6)&&(b[1][2] == ' ')){
            b[1][2] = 'O';
            t = 0;
        }
        else if ((r == 7)&&(b[2][0] == ' ')){
            b[2][0] = 'O';
            t = 0;
        }
        else if ((r == 8)&&(b[2][1] == ' ')){
            b[2][1] = 'O';
            t = 0;
        }
        else if ((r == 9)&&(b[2][2] == ' ')){
            b[2][2] = 'O';
            t = 0;
        }
    }
}
void AIeasy(){
    int t = 1, time = 0;
    while(t == 1){
        if (time = 70){
            bypass();
            break;
        }    
        int r = (rand()%9)+1;
        if ((r == 1)&&(b[0][0] == ' ')){
            if ((b[0][1] == 'X' && b[0][2] == 'X')||(b[1][0] == 'X' && b[2][0] == 'X')||(b[1][1] == 'X' && b[2][2] == 'X')||(b[0][1] == 'O' && b[0][2] == 'O')||(b[1][0] == 'O' && b[2][0] == 'O')||(b[1][1] == 'O' && b[2][2] == 'O')){}
            else{
                 b[0][0] = 'O';
                 t = 0;
            }
         }
        else if ((r == 2)&&(b[0][1] == ' ')){
            if ((b[0][0] == 'X' && b[0][2] == 'X')||(b[1][1] == 'X' && b[2][1] == 'X')||(b[0][0] == 'O' && b[0][2] == 'O')||(b[1][1] == 'O' && b[2][1] == 'O')){}
            else{
                b[0][1] = 'O';
                t = 0;
            }
        }
            
        else if ((r == 3)&&(b[0][2] == ' ')){
            if ((b[0][0] == 'X' && b[0][1] == 'X')||(b[1][2] == 'X' && b[2][2] == 'X')||(b[1][1] == 'X' && b[2][0] == 'X')||(b[0][0] == 'O' && b[0][1] == 'O')||(b[1][2] == 'O' && b[2][2] == 'O')||(b[1][1] == 'O' && b[2][0] == 'O')){}
            else{
                b[0][2] = 'O';
                t = 0;
            }
        }
        else if ((r == 4)&&(b[1][0] == ' ')){
            if ((b[1][1] == 'X' && b[1][2] == 'X')||(b[0][0] == 'X' && b[2][0] == 'X')||(b[1][1] == 'O' && b[1][2] == 'O')||(b[0][0] == 'O' && b[2][0] == 'O')){}
            else{
                b[1][0] = 'O';
                t = 0;
            }
        }
        else if ((r == 5)&&(b[1][1] == ' ')){
            if ((b[1][0] == 'X' && b[1][2] == 'X')||(b[0][1] == 'X' && b[2][1] == 'X')||(b[0][0] == 'X' && b[2][2] == 'X')||(b[0][2] == 'X' && b[2][0] == 'X')||(b[1][0] == 'O' && b[1][2] == 'O')||(b[0][1] == 'O' && b[2][1] == 'O')||(b[0][0] == 'O' && b[2][2] == 'O')||(b[0][2] == 'O' && b[2][0] == 'O')){}
            else{
                b[1][1] = 'O';
                t = 0;
            }
        }
        else if ((r == 6)&&(b[1][2] == ' ')){
            if ((b[1][0] == 'X' && b[1][1] == 'X')||(b[0][2] == 'X' && b[2][2] == 'X')||(b[1][0] == 'O' && b[1][1] == 'O')||(b[0][2] == 'O' && b[2][2] == 'O')){}     
            else{
                b[1][2] = 'O';
                t = 0;
            }
        }
        else if ((r == 7)&&(b[2][0] == ' ')){
            if ((b[2][1] == 'X' && b[2][2] == 'X')||(b[0][0] == 'X' && b[1][0] == 'X')||(b[1][1] == 'X' && b[0][2] == 'X')||(b[2][1] == 'O' && b[2][2] == 'O')||(b[0][0] == 'O' && b[1][0] == 'O')||(b[1][1] == 'O' && b[0][2] == 'O')){}
            else{
                b[2][0] = 'O';
                t = 0;
            }
        }
        else if ((r == 8)&&(b[2][1] == ' ')){
            if ((b[2][0] == 'X' && b[2][2] == 'X')||(b[0][1] == 'X' && b[1][1] == 'X')||(b[2][0] == 'O' && b[2][2] == 'O')||(b[0][1] == 'O' && b[1][1] == 'O')){}
            else{
                b[2][1] = 'O';
                t = 0;
            }
        }
        else if ((r == 9)&&(b[2][2] == ' ')){
            if ((b[2][0] == 'X' && b[2][1] == 'X')||(b[0][2] == 'X' && b[1][2] == 'X')||(b[0][0] == 'X' && b[1][1] == 'X')||(b[2][0] == 'O' && b[2][1] == 'O')||(b[0][2] == 'O' && b[1][2] == 'O')||(b[0][0] == 'O' && b[1][1] == 'O')){}
            else{
                b[2][2] = 'O';
                t = 0; 
            }
        }
        time++;
    }
}
void AImed(){
    int t = 1;
    while (t == 1){
        int r = (rand()%9)+1;
        int r2 = (rand()%2)+1;
        if ((r2 == 1)&&((b[0][0] == ' ')&&((b[0][1] == 'X' && b[0][2] == 'X')||(b[1][0] == 'X' && b[2][0] == 'X')||(b[1][1] == 'X' && b[2][2] == 'X')||(b[0][1] == 'O' && b[0][2] == 'O')||(b[1][0] == 'O' && b[2][0] == 'O')||(b[1][1] == 'O' && b[2][2] == 'O')))){
            b[0][0] = 'O';
            t = 0;
        }
        else if ((r2 != 1)&&((b[0][1] == ' ')&&((b[0][0] == 'X' && b[0][2] == 'X')||(b[1][1] == 'X' && b[2][1] == 'X')||(b[0][0] == 'O' && b[0][2] == 'O')||(b[1][1] == 'O' && b[2][1] == 'O')))){
            b[0][1] = 'O';
            t = 0;
        }
        else if ((r2 != 1)&&((b[0][2] == ' ')&&((b[0][0] == 'X' && b[0][1] == 'X')||(b[1][2] == 'X' && b[2][2] == 'X')||(b[1][1] == 'X' && b[2][0] == 'X')||(b[0][0] == 'O' && b[0][1] == 'O')||(b[1][2] == 'O' && b[2][2] == 'O')||(b[1][1] == 'O' && b[2][0] == 'O')))){
            b[0][2] = 'O';
            t = 0;
        }
        else if ((r2 != 1)&&((b[1][0] == ' ')&&((b[1][1] == 'X' && b[1][2] == 'X')||(b[0][0] == 'X' && b[2][0] == 'X')||(b[1][1] == 'O' && b[1][2] == 'O')||(b[0][0] == 'O' && b[2][0] == 'O')))){
            b[1][0] = 'O';
            t = 0;
        }
        else if ((r2 != 1)&&((b[1][1] == ' ')&&((b[1][0] == 'X' && b[1][2] == 'X')||(b[0][1] == 'X' && b[2][1] == 'X')||(b[0][0] == 'X' && b[2][2] == 'X')||(b[0][2] == 'X' && b[2][0] == 'X')||(b[1][0] == 'O' && b[1][2] == 'O')||(b[0][1] == 'O' && b[2][1] == 'O')||(b[0][0] == 'O' && b[2][2] == 'O')||(b[0][2] == 'O' && b[2][0] == 'O')))){
            b[1][1] = 'O';
            t = 0;
        }
        else if ((r2 != 1)&&((b[1][2] == ' ')&&((b[1][0] == 'X' && b[1][1] == 'X')||(b[0][2] == 'X' && b[2][2] == 'X')||(b[1][0] == 'O' && b[1][1] == 'O')||(b[0][2] == 'O' && b[2][2] == 'O')))){
            b[1][2] = 'O';
            t = 0;
        }
        else if ((r2 != 1)&&((b[2][0] == ' ')&&((b[2][1] == 'X' && b[2][2] == 'X')||(b[0][0] == 'X' && b[1][0] == 'X')||(b[1][1] == 'X' && b[0][2] == 'X')||(b[2][1] == 'O' && b[2][2] == 'O')||(b[0][0] == 'O' && b[1][0] == 'O')||(b[1][1] == 'O' && b[0][2] == 'O')))){
            b[2][0] = 'O';
            t = 0;
        }
        else if ((r2 != 1)&&((b[2][1] == ' ')&&((b[2][0] == 'X' && b[2][2] == 'X')||(b[0][1] == 'X' && b[1][1] == 'X')||(b[2][0] == 'O' && b[2][2] == 'O')||(b[0][1] == 'O' && b[1][1] == 'O')))){
            b[2][1] = 'O';
            t = 0;
        }
        else if ((r2 != 1)&&((b[2][2] == ' ')&&((b[2][0] == 'X' && b[2][1] == 'X')||(b[0][2] == 'X' && b[1][2] == 'X')||(b[0][0] == 'X' && b[1][1] == 'X')||(b[2][0] == 'O' && b[2][1] == 'O')||(b[0][2] == 'O' && b[1][2] == 'O')||(b[0][0] == 'O' && b[1][1] == 'O')))){
            b[2][2] = 'O';
            t = 0;
        }
        else if ((r == 1)&&(b[0][0] == ' ')){
            b[0][0] = 'O';
            t = 0;
        }
        else if ((r == 2)&&(b[0][1] == ' ')){
            b[0][1] = 'O';
            t = 0;
        }
        else if ((r == 3)&&(b[0][2] == ' ')){
            b[0][2] = 'O';
            t = 0;
        }
        else if ((r == 4)&&(b[1][0] == ' ')){
            b[1][0] = 'O';
            t = 0;
        }
        else if ((r == 5)&&(b[1][1] == ' ')){
            b[1][1] = 'O';
            t = 0;
        }
        else if ((r == 6)&&(b[1][2] == ' ')){
            b[1][2] = 'O';
            t = 0;
        }
        else if ((r == 7)&&(b[2][0] == ' ')){
            b[2][0] = 'O';
            t = 0;
        }
        else if ((r == 8)&&(b[2][1] == ' ')){
            b[2][1] = 'O';
            t = 0;
        }
        else if ((r == 9)&&(b[2][2] == ' ')){
            b[2][2] = 'O';
            t = 0;
        }
    }
}
void AIhard(){
    int t = 1;
    while (t == 1){
        int r = (rand()%9)+1;
        if ((b[0][0] == ' ')&&((b[0][1] == 'X' && b[0][2] == 'X')||(b[1][0] == 'X' && b[2][0] == 'X')||(b[1][1] == 'X' && b[2][2] == 'X')||(b[0][1] == 'O' && b[0][2] == 'O')||(b[1][0] == 'O' && b[2][0] == 'O')||(b[1][1] == 'O' && b[2][2] == 'O'))){
            b[0][0] = 'O';
            t = 0;
        }
        else if((b[0][1] == ' ')&&((b[0][0] == 'X' && b[0][2] == 'X')||(b[1][1] == 'X' && b[2][1] == 'X')||(b[0][0] == 'O' && b[0][2] == 'O')||(b[1][1] == 'O' && b[2][1] == 'O'))){
            b[0][1] = 'O';
            t = 0;
        }
        else if((b[0][2] == ' ')&&((b[0][0] == 'X' && b[0][1] == 'X')||(b[1][2] == 'X' && b[2][2] == 'X')||(b[1][1] == 'X' && b[2][0] == 'X')||(b[0][0] == 'O' && b[0][1] == 'O')||(b[1][2] == 'O' && b[2][2] == 'O')||(b[1][1] == 'O' && b[2][0] == 'O'))){
            b[0][2] = 'O';
            t = 0;
        }
        else if ((b[1][0] == ' ')&&((b[1][1] == 'X' && b[1][2] == 'X')||(b[0][0] == 'X' && b[2][0] == 'X')||(b[1][1] == 'O' && b[1][2] == 'O')||(b[0][0] == 'O' && b[2][0] == 'O'))){
            b[1][0] = 'O';
            t = 0;
        }
        else if ((b[1][1] == ' ')&&((b[1][0] == 'X' && b[1][2] == 'X')||(b[0][1] == 'X' && b[2][1] == 'X')||(b[0][0] == 'X' && b[2][2] == 'X')||(b[0][2] == 'X' && b[2][0] == 'X')||(b[1][0] == 'O' && b[1][2] == 'O')||(b[0][1] == 'O' && b[2][1] == 'O')||(b[0][0] == 'O' && b[2][2] == 'O')||(b[0][2] == 'O' && b[2][0] == 'O'))){
            b[1][1] = 'O';
            t = 0;
        }
        else if ((b[1][2] == ' ')&&((b[1][0] == 'X' && b[1][1] == 'X')||(b[0][2] == 'X' && b[2][2] == 'X')||(b[1][0] == 'O' && b[1][1] == 'O')||(b[0][2] == 'O' && b[2][2] == 'O'))){
            b[1][2] = 'O';
            t = 0;
        }
        else if ((b[2][0] == ' ')&&((b[2][1] == 'X' && b[2][2] == 'X')||(b[0][0] == 'X' && b[1][0] == 'X')||(b[1][1] == 'X' && b[0][2] == 'X')||(b[2][1] == 'O' && b[2][2] == 'O')||(b[0][0] == 'O' && b[1][0] == 'O')||(b[1][1] == 'O' && b[0][2] == 'O'))){
            b[2][0] = 'O';
            t = 0;
        }
        else if ((b[2][1] == ' ')&&((b[2][0] == 'X' && b[2][2] == 'X')||(b[0][1] == 'X' && b[1][1] == 'X')||(b[2][0] == 'O' && b[2][2] == 'O')||(b[0][1] == 'O' && b[1][1] == 'O'))){
            b[2][1] = 'O';
            t = 0;
        }
        else if ((b[2][2] == ' ')&&((b[2][0] == 'X' && b[2][1] == 'X')||(b[0][2] == 'X' && b[1][2] == 'X')||(b[0][0] == 'X' && b[1][1] == 'X')||(b[2][0] == 'O' && b[2][1] == 'O')||(b[0][2] == 'O' && b[1][2] == 'O')||(b[0][0] == 'O' && b[1][1] == 'O'))){
            b[2][2] = 'O';
            t = 0;
        }
        else if ((r == 1)&&(b[0][0] == ' ')){
            b[0][0] = 'O';
            t = 0;
        }
        else if ((r == 2)&&(b[0][1] == ' ')){
            b[0][1] = 'O';
            t = 0;
        }
        else if ((r == 3)&&(b[0][2] == ' ')){
            b[0][2] = 'O';
            t = 0;
        }
        else if ((r == 4)&&(b[1][0] == ' ')){
            b[1][0] = 'O';
            t = 0;
        }
        else if ((r == 5)&&(b[1][1] == ' ')){
            b[1][1] = 'O';
            t = 0;
        }
        else if ((r == 6)&&(b[1][2] == ' ')){
            b[1][2] = 'O';
            t = 0;
        }
        else if ((r == 7)&&(b[2][0] == ' ')){
            b[2][0] = 'O';
            t = 0;
        }
        else if ((r == 8)&&(b[2][1] == ' ')){
            b[2][1] = 'O';
            t = 0;
        }
        else if ((r == 9)&&(b[2][2] == ' ')){
            b[2][2] = 'O';
            t = 0;
        }
    }
}
void playerturn(){
    int turn = 1, t = 0;
    while(turn == 1){
        cout<<"Please enter the square number you choose: "<<endl;
        cin>>t;
        if((t == 1)&&(b[0][0] == ' ')){
            b[0][0] = 'X';
            turn = 0;
        }
        else if((t == 2)&&(b[0][1] == ' ')){
            b[0][1] = 'X';
            turn = 0;
        }
        else if((t == 3)&&(b[0][2] == ' ')){
            b[0][2] = 'X';
            turn = 0; 
        }
        else if((t == 4)&&(b[1][0] == ' ')){
            b[1][0] = 'X';
            turn = 0;
        }
        else if((t == 5)&&(b[1][1] == ' ')){
            b[1][1] = 'X';
            turn = 0;
        }
        else if((t == 6)&&(b[1][2] == ' ')){
            b[1][2] = 'X';
            turn = 0; 
        }
        else if((t == 7)&&(b[2][0] == ' ')){
            b[2][0] = 'X';
            turn = 0;
        }
        else if((t == 8)&&(b[2][1] == ' ')){
            b[2][1] = 'X';
            turn = 0;
        }
        else if((t == 9)&&(b[2][2] == ' ')){
            b[2][2] = 'X';
            turn = 0; 
        }
        else{
            cout<<"That square has already been taken!"<<endl;
        }
    }
}
void player2turn(){
    int turn = 1, t = 0;
    while(turn == 1){
        cout<<"Player 2 enter the square number you choose: "<<endl;
        cin>>t;
        if((t == 1)&&(b[0][0] == ' ')){
            b[0][0] = 'O';
            turn = 0;
        }
        else if((t == 2)&&(b[0][1] == ' ')){
            b[0][1] = 'O';
            turn = 0;
        }
        else if((t == 3)&&(b[0][2] == ' ')){
            b[0][2] = 'O';
            turn = 0; 
        }
        else if((t == 4)&&(b[1][0] == ' ')){
            b[1][0] = 'O';
            turn = 0;
        }
        else if((t == 5)&&(b[1][1] == ' ')){
            b[1][1] = 'O';
            turn = 0;
        }
        else if((t == 6)&&(b[1][2] == ' ')){
            b[1][2] = 'O';
            turn = 0; 
        }
        else if((t == 7)&&(b[2][0] == ' ')){
            b[2][0] = 'O';
            turn = 0;
        }
        else if((t == 8)&&(b[2][1] == ' ')){
            b[2][1] = 'O';
            turn = 0;
        }
        else if((t == 9)&&(b[2][2] == ' ')){
            b[2][2] = 'O';
            turn = 0; 
        }
        else{
            cout<<"That square has already been taken!"<<endl;
        }
    }
}
int main() {
    srand(time(NULL));
    int game = 1, d = 0, w = 0, s = 0, s2 = 0, p = 0;
    string name, name2, dif;
    char again = ' ';
    cout<<"Welcome to Tic Tac Toe, please enter your name: "<<endl;
    cin>>name;
    cout<<"Hello "<<name<<", would you like to play against the Computer or another Person?(1 = Computer, 2 = Person.)"<<endl;
    cin>>p;
    if (p == 2){
            cout<<"Please enter Player 2 name: "<<endl;
            cin>>name2;
    }
    do{
        if (p == 1){
            cout<<"Please choose a difficulty(2 Player Comming Soon), 1 = Easy, 2 = Medium, 3 = Hard"<<endl;
            cin>>d;
            if (d == 1) dif = "easy";
            else if (d == 2) dif = "medium";
            else dif = "hard";
        }
        draw();
        cout<<"Use these numbers to play!"<<endl;
        if (p == 1){
            cout<<"You chose "<<dif<<" difficulty, enjoy!"<<endl;
        }
        clearboard();
        int i = 1;
        while (i <=5){
            playerturn();
            draw();
            w = checkwin();
            if (w == 1){
            cout<<name<<" Wins!!!"<<endl;
            s+=1;
            break;
            }
            if (i ==5){
                cout<<"Draw!!!"<<endl;
                break;
            }
            if (p == 1){
                if (d == 1) AIeasy();
                else if (d == 2) AImed();
                else AIhard();
            }
            else{
                player2turn();
            }
            draw();
            w = checkwin();
            if (w == 2){
                if (p == 1){
                    cout<<"Computer Wins!!!"<<endl;
                    if (s >= 1) s-=1;
                    break;
                }
                else{
                    cout<<name2<<" Wins!!!"<<endl;
                    s2+=1;
                    break;
                }
            }
            i++;
        }
        resetboard();
        if (p == 1){
            cout<<"Your score is currently "<<s<<endl;
        }
        else {
            cout<<name<<"'s score is currently "<<s<<endl;
            cout<<name2<<"'s score is currently "<<s2<<endl;
        }
        cout<<"Would you like to play again?(y/n)"<<endl;
        cin>>again;
    } while (again == 'y');
    if(p == 1){
        cout<<"Thank you for playing, "<<name<<" your total score is "<<s<<".\nIf you enjoyed the game please give it a thumbs up.\nMade by: Mooaholic."<<endl;
    }
    else{
        cout<<"Thank you for playing,\n"<<name<<" your total score is "<<s<<".\n"<<name2<<" your total score is "<<s2<<"\nIf you enjoyed the game please give it a thumbs up.\nMade by: Mooaholic."<<endl;
    }
    return 0;
}

3. By Omar

Made by Omar. Enter the Coordinates of an array, array is(3X3) and Counting Starts From 0. The Game is Two Players Game One will Enter the X and The Other Will Enter the O. Its Just Like a TIC TAC TOE a little Difference is you should Enter the Coordinates instead of Number. ( Source )

#include <iostream>
#include <cstdlib>
#include <dos.h>
#include <time.h>
#include <conio.h>
using namespace::std;


int main(int argc, char** argv) {
	char arr[3][3];
	char value='1';
	int player=2;
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<3;j++)
		{
			arr[i][j]=value;
			value++;
	}
	}
	while(true)
	{
		for(int i=0;i<3;i++)
	{
		cout<<"\n\n\n";
		for(int j=0;j<3;j++)
		{
			cout.width(5);
		cout<<arr[i][j]<<" ";
	}
		cout<<endl<<endl;
	}
	int x,y;
	if(player%2==0)
	{
		cout<<"Player 1 Enter the Cordinates:"<<endl<<endl;
	do{
	cout<<"Enter the X Cordinate:";
	cin>>x;
}while(x>2);
do{
	cout<<"\nEnter the Y Cordinate:";
	cin>>y;
}while(y>2);
	arr[x][y]='X';
	player=1;
	system("cls");
}
else
{
	cout<<"Player 2 Enter the Cordinates:"<<endl<<endl;
	do{
	cout<<"Enter the X Cordinate:";
	cin>>x;
}while(x>2);
do{
	cout<<"\nEnter the Y Cordinate:";
	cin>>y;
}while(y>2);
	arr[x][y]='O';
	player=2;
	system("cls");
}
	if(arr[0][0]==arr[0][1] && arr[0][0]==arr[0][2])
	{
	cout<<"Congratulation You Won:!";
	break;
}
	else if(arr[1][0]==arr[1][1] && arr[1][0]==arr[1][2])
	{
	cout<<"Congratulation You Won:!";
	break;
}
else if(arr[0][0]==arr[1][0] && arr[0][0]==arr[2][0])
{
	cout<<"Congratulation You Won:!";
	break;
}
else if(arr[0][0]==arr[1][1] && arr[0][0]==arr[2][2])
{
	cout<<"Congratulation You Won:!";
	break;
}
else if(arr[2][0]==arr[2][1] && arr[2][0]==arr[2][2])
{
	cout<<"Congratulation You Won:!";
	break;
}
else if(arr[2][0]==arr[1][1] && arr[2][0]==arr[0][2])
{
	cout<<"Congratulation You Won:!";
	break;
}
else if(arr[2][2]==arr[1][2] && arr[2][2]==arr[0][2])
{
	cout<<"Congratulation You Won:!";
	break;
}
else if(arr[0][1]==arr[1][1] && arr[0][1]==arr[2][1])
{
	cout<<"Congratulation You Won:!";
	break;
}

	}
	
	return 0;
}

4. By Pragya Shukla

Made by Pragya Shukla. ( Source )


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<dos.h>
#include<ctype.h>
#include<stdio.h>
void main()
{
char option,ch[1];
int mine[9],computer[9],ptr[9],list[10],player1[9],player2[9];
int player,turn=0,color,i,j;
int driver=9,mode=2;
void computer_turn(int list[10],int ptr[10],int computer[10], int i);
void mine_turn(int list[10],int ptr[10],int mine[10],int i,int player);
void display(int list[10],int i, char ch[1]);
int result(int ptr[10],int i,int player);
initgraph(&driver,&mode,"");
cleardevice();
setcolor(LIGHTBLUE);
settextstyle(8,HORIZ_DIR,9);
outtext("TIC TAC TOE");
int midx=getmaxx()/2;
int midy=getmaxy()/2;
settextstyle(8,HORIZ_DIR,5);
outtextxy(midx-90,midy,"Loading");
for(int pass=1;pass<=6;pass++)
for(j=-90;j<=90;j=j+30)
{
setcolor(BLUE);
setfillstyle(SOLID_FILL,BLUE);
circle(midx+j,midy+100,12);
circle(midx+j,midy+100,10);
floodfill(midx+j,midy+100,BLUE);
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
delay(150);
floodfill(midx+j,midy+100,BLACK);
}
again:cleardevice();
setcolor(LIGHTBLUE);
settextstyle(8,HORIZ_DIR,9);
outtext("Select Game");
settextstyle(8,HORIZ_DIR,6);
outtextxy(20,160,"1 - Single Player");
outtextxy(20,260,"2 - Double Player");
outtextxy(20,360,"Q - Quit");
player=getche();
if(player=='q' || player=='Q')exit(0);
else if(player!=49 && player!=50 && (player!='q' || player!='Q'))goto again;
cleardevice();
game:clrscr();
cleardevice();
setbkcolor(BLACK);
settextstyle(1,0,6);
setlinestyle(0,3,3);
setcolor(BLUE);
line(200,140,200,getmaxy());
line(400,140,400,getmaxy());
line(50,250,getmaxx()-50,250);
line(50,370,getmaxx()-50,370);
outtextxy(174,188,"1      2      3");
outtextxy(174,308,"4      5      6");
outtextxy(174,428,"7      8      9");
for(i=1;i<=9;i++)
list[i]=10,player1[i]=player2[i]=ptr[i]=mine[i]=computer[i]=20*i;
settextstyle(1,0,10);
for(i=1;i<=9;i++)
{
if(player=='1')
{
if(turn%2==0)
if(i%2!=0)
goto my;
else
goto com;
else
if(i%2!=0)
goto com;
else
goto my;
my:mine_turn(list,ptr,mine,i,player);
setcolor(RED);
display(list,i,"0");
if(result(mine,i,player))
break;
continue;
com:computer_turn(list,ptr,computer,i);
setcolor(GREEN);
display(list,i,"X");
if(result(computer,i,player))
break;
continue;
}
else
{
if(i%2!=0)
{
mine_turn(list,ptr,player1,i,player);
setcolor(RED);
display(list,i,"0");
if(result(player1,i,player))
break;
}
else
{
mine_turn(list,ptr,player2,i,player);
setcolor(GREEN);
display(list,i,"X");
if(result(player2,i,player))
break;
}
}
}
sleep(2);
cleardevice();
cout<<"  Play again     PRESS      y";
cout<<"  Main Menu     PRESS      m";
cout<<"  Quit        PRESS      q";
wrong:flushall();
option=getche();
if(option=='y' || option=='Y')
{
turn++;
goto game;
}
else if(option=='m' || option=='M')
{
goto again;
}
else if(option=='q' || option=='Q')
{
exit(0);
}
else goto wrong;
}
int result(int ptr[10],int i,int player)
{
void line_draw(int ptr[10]);
clearviewport();
settextstyle(1,0,9);
setcolor(5);
if(ptr[1]==ptr[2]&&ptr[1]==ptr[3] || ptr[4]==ptr[5]&&ptr[4]==ptr[6] ||
ptr[7]==ptr[8]&&ptr[7]==ptr[9] || ptr[1]==ptr[4]&&ptr[1]==ptr[7] ||
ptr[2]==ptr[5]&&ptr[2]==ptr[8] || ptr[3]==ptr[6]&&ptr[3]==ptr[9] ||
ptr[1]==ptr[5]&&ptr[1]==ptr[9] || ptr[3]==ptr[5]&&ptr[3]==ptr[7])
if(ptr[5]==0||ptr[1]==0||ptr[9]==0)
{
delay(80);
if(player=='1')
outtextxy(150,0,"Victory");
else
outtext("Player1 WINS");
line_draw(ptr);
return 1;
}
else
{
delay(80);
if(player=='1')
outtextxy(160,0,"Defeat");
else
outtext("Player2 WINS");
line_draw(ptr);
return 1;
}
else
{
if(i==9)
{
delay(80);
outtextxy(110,0,"Try Again");
}
return 0;
}
}
void mine_turn(int list[10],int ptr[10],int mine[10],int i, int player)
{
char temp;
again:setviewport(0,0,638,120,0);
clearviewport();
setcolor(LIGHTBLUE);
settextstyle(1,0,6);
if(player=='1')
outtext("      Your Turn");
else if(i%2!=0)
outtext("        Player 1");
else
outtext("        Player 2");
outtextxy(0,50,"Make your Move 1 to 9");
temp=getche();
if(temp=='q' || temp=='Q')
exit(0);
else if(isdigit(temp) && temp!='0')
list[i]=toascii(temp)-48;
else
goto again;
if(i!=1)
for(int j=1;j<i;j++)
if(list[i]==list[j])
goto again;
if(player=='1')
mine[list[i]]=0;
else if(i%2!=0)
mine[list[i]]=0;
else
mine[list[i]]=1;
if(player=='1')
ptr[list[i]]=0;
else if (i%2!=0)
ptr[list[i]]=0;
else
ptr[list[i]]=1;
}
void computer_turn(int list[10],int ptr[10],int computer[10], int i)
{
com_again:
if(ptr[1]!=0 && ptr[1]!=1 && (ptr[2]==ptr[3] || ptr[4]==ptr[7] ||ptr[5]==ptr[9]))
list[i]=1;
else if(ptr[2]!=0 && ptr[2]!=1 && (ptr[1]==ptr[3] || ptr[5]==ptr[8]))
list[i]=2;
else if(ptr[3]!=0 && ptr[3]!=1 && (ptr[1]==ptr[2] || ptr[6]==ptr[9] ||ptr[5]==ptr[7]))
list[i]=3;
else if(ptr[4]!=0 && ptr[4]!=1 && (ptr[1]==ptr[7] || ptr[5]==ptr[6]))
list[i]=4;
else if(ptr[5]!=0 && ptr[5]!=1 && (ptr[1]==ptr[9] || ptr[3]==ptr[7] || ptr[4]==ptr[6] || ptr[2]==ptr[8]))
list[i]=5;
else if(ptr[6]!=0 && ptr[6]!=1 && (ptr[4]==ptr[5] || ptr[3]==ptr[9]))
list[i]=6;
else if(ptr[7]!=0 && ptr[7]!=1 && (ptr[1]==ptr[4] || ptr[8]==ptr[9] ||ptr[5]==ptr[3]))
list[i]=7;
else if(ptr[8]!=0 && ptr[8]!=1 && (ptr[5]==ptr[2] || ptr[7]==ptr[9]))
list[i]=8;
else if(ptr[9]!=0 && ptr[9]!=1 && (ptr[1]==ptr[5] || ptr[7]==ptr[8] ||ptr[3]==ptr[6]))
list[i]=9;
else
list[i]=random(9)+1;
if(i!=1)
for(int j=1;j<i;j++)
if(list[i]==list[j])
goto com_again;
computer[list[i]]=1;
ptr[list[i]]=1;
}
void display(int list[10],int i, char ch[1])
{
settextstyle(1,0,10);
switch(list[i])
{
case 1:outtextxy(100,110,ch);
 break;
case 2:outtextxy(270,110,ch);
 break;
case 3:outtextxy(460,110,ch);
 break;
case 4:outtextxy(100,230,ch);
 break;
case 5:outtextxy(270,230,ch);
 break;
case 6:outtextxy(460,230,ch);
 break;
case 7:outtextxy(100,350,ch);
 break;
case 8:outtextxy(270,350,ch);
 break;
case 9:outtextxy(460,350,ch);
 break;
 }
}
void line_draw(int ptr[10])
{
setcolor(BROWN);
setlinestyle(SOLID_LINE,1,3);

if(ptr[1]==ptr[2] && ptr[2]==ptr[3])
line(80,190,getmaxx()-100,190);
else if(ptr[4]==ptr[5] && ptr[5]==ptr[6])
line(80,310,getmaxx()-100,310);
else if(ptr[7]==ptr[8] && ptr[8]==ptr[9])
line(80,430,getmaxx()-100,430);
else if(ptr[1]==ptr[4] && ptr[4]==ptr[7])
line(130,137,130,getmaxy());
else if(ptr[2]==ptr[5] && ptr[5]==ptr[8])
line(300,137,300,getmaxy());
else if(ptr[3]==ptr[6] && ptr[6]==ptr[9])
line(487,137,487,getmaxy());
else if(ptr[1]==ptr[5] && ptr[5]==ptr[9])
line(80,148,550,getmaxy());
else if(ptr[3]==ptr[5] && ptr[5]==ptr[7])
line(530,148,80,getmaxy());
}
// by pragya 

5. By Keshav Varma

Made by Keshav Varma. A simple Tic Tac Toe game made using C++. Players get to choose who will start, after starting player 1 and player 2 will get their chances to pick their moves. ( Source )

#include <iostream>
using namespace std;
char square[10] = {'0','1','2','3','4','5','6','7','8','9'};
int checkwin()      
 /*function to check the status of the game
  * 1-game is over with result
  * 0-game is over with no result
  * -1-game is not over          */
{
	if(square[1]==square[2]&&square[2]==square[3])
	return 1;
	else if(square[1]==square[5]&&square[5]==square[9])
	return 1;
	else if(square[1]==square[4]&&square[4]==square[7])
	return 1;
    else if(square[2]==square[5]&&square[5]==square[8])
	return 1;
	else if(square[3]==square[6]&&square[6]==square[9])
	return 1;
	else if(square[3]==square[5]&&square[5]==square[7])
	return 1;
	else if(square[4]==square[5]&&square[5]==square[6])
	return 1;
	else if(square[7]==square[8]&&square[8]==square[9])
	return 1;
	else if(square[1]!='1'&&square[2]!='2'&&square[3]!='3'&&square[4]!='4'&&square[5]!='5'&&square[6]!='6'&&square[7]!='7'&&square[8]!='8'&&square[9]!='9')
	return 0;
	else 
	return -1;
}

void board()
{ 
	cout<<"\n\nTic Tac Toe\n\n";
	cout<<"Player 1 (X) and Player 2 (O)\n\n";
	cout<<endl;
	cout<<square[1]<<"  "<<square[2]<<"  "<<square[3]<<endl;
	cout<<square[4]<<"  "<<square[5]<<"  "<<square[6]<<endl;
	cout<<square[7]<<"  "<<square[8]<<"  "<<square[9]<<endl;
}
int main()
{
	int player,i,choice;
	char mark;
	cout<<"Enter player number(1/2)";
	cin>>player;
	do
	{
	 board();
	 player=(player%2)?1:2;
	 cout<<"Player "<<player<<"     Enter a number :";
	 cin>>choice;
	 mark=(player==1)?'X':'O';
	 if(choice==1&&square[1]=='1')
	     square[1]=mark;
	 else if(choice==2&&square[2]=='2')
		 square[2]=mark;
	 else if(choice==3&&square[3]=='3')
      	 square[3]=mark;
	 else if(choice==4&&square[4]=='4')
		 square[4]=mark;
     else if(choice==5&&square[5]=='5')	
		 square[5]=mark;
	 else if(choice==6&&square[6]=='6')
		 square[6]=mark;
     else if(choice==7&&square[7]=='7')
		 square[7]=mark;
 	 else if(choice==8&&square[8]=='8')
		 square[8]=mark;
	 else if(choice==9&&square[9]=='9')
		 square[9]=mark;
     else
     {
     cout<<"invalid move";
     continue;
     }
	 i=checkwin();
	 player++;
	}while(i==-1);
if(i==1)
cout<<" Player "<<--player<<" WIN! ";
else if(i==0)
cout<<" Game draw ";
return 0;
}

6. By Hero Franesa

Made by Hero Franesa. Works the same as the above program. ( Source )

#include <iostream>
using namespace std;

char square[10] = {'o','1','2','3','4','5','6','7','8','9'};

int checkwin();
void board();

int main()
{
	int player = 1,i,choice;

	char mark;
	do
	{
		board();
		player=(player%2)?1:2;

		cout << "Player " << player << ", enter a number:  ";
		cin >> choice;

		mark=(player == 1) ? 'X' : 'O';

		if (choice == 1 && square[1] == '1')

			square[1] = mark;
		else if (choice == 2 && square[2] == '2')

			square[2] = mark;
		else if (choice == 3 && square[3] == '3')

			square[3] = mark;
		else if (choice == 4 && square[4] == '4')

			square[4] = mark;
		else if (choice == 5 && square[5] == '5')

			square[5] = mark;
		else if (choice == 6 && square[6] == '6')

			square[6] = mark;
		else if (choice == 7 && square[7] == '7')

			square[7] = mark;
		else if (choice == 8 && square[8] == '8')

			square[8] = mark;
		else if (choice == 9 && square[9] == '9')

			square[9] = mark;
		else
		{
			cout<<"Invalid move ";

			player--;
			cin.ignore();
			cin.get();
		}
		i=checkwin();

		player++;
	}while(i==-1);
	board();
	if(i==1)

		cout<<"==>\aPlayer "<<--player<<" win ";
	else
		cout<<"==>\aGame draw";

	cin.ignore();
	cin.get();
	return 0;
}

/*********************************************

	FUNCTION TO RETURN GAME STATUS
	1 FOR GAME IS OVER WITH RESULT
	-1 FOR GAME IS IN PROGRESS
	O GAME IS OVER AND NO RESULT
**********************************************/

int checkwin()
{
	if (square[1] == square[2] && square[2] == square[3])

		return 1;
	else if (square[4] == square[5] && square[5] == square[6])

		return 1;
	else if (square[7] == square[8] && square[8] == square[9])

		return 1;
	else if (square[1] == square[4] && square[4] == square[7])

		return 1;
	else if (square[2] == square[5] && square[5] == square[8])

		return 1;
	else if (square[3] == square[6] && square[6] == square[9])

		return 1;
	else if (square[1] == square[5] && square[5] == square[9])

		return 1;
	else if (square[3] == square[5] && square[5] == square[7])

		return 1;
	else if (square[1] != '1' && square[2] != '2' && square[3] != '3'
                    && square[4] != '4' && square[5] != '5' && square[6] != '6'
                  && square[7] != '7' && square[8] != '8' && square[9] != '9')

		return 0;
	else
		return -1;
}


/*******************************************************************
     FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
********************************************************************/


void board()
{
	system("cls");
	cout << "\n\n\tTic Tac Toe\n\n";

	cout << "Player 1 (X)  -  Player 2 (O)" << endl << endl;
	cout << endl;

	cout << "     |     |     " << endl;
	cout << "  " << square[1] << "  |  " << square[2] << "  |  " << square[3] << endl;

	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;

	cout << "  " << square[4] << "  |  " << square[5] << "  |  " << square[6] << endl;

	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;

	cout << "  " << square[7] << "  |  " << square[8] << "  |  " << square[9] << endl;

	cout << "     |     |     " << endl << endl;
}

/*******************************************************************
				END OF PROJECT
********************************************************************/

7. By Sarfaraz Alam

Made by Sarfaraz Alam. ( Source )

#include <bits/stdc++.h>
using namespace std;
int tic();
void tac();
int toe();
char board[10] = {'O', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
int tic()
{ // done
  int choice, player = 1, i;
  char sym;
  do
  {
    tac();
    player = (player % 2) ? 1 : 2;
    cout << "Enter Player " << player << " : ";
    cin >> choice;
    sym = (player == 1) ? 'X' : 'O';
    if (choice == 1 && board[1] == '1')
    {
      board[1] = sym;
    }
    else if (choice == 2 && board[2] == '2')
    {
      board[2] = sym;
    }
    else if (choice == 3 && board[3] == '3')
    {
      board[3] = sym;
    }
    else if (choice == 4 && board[4] == '4')
    {
      board[4] = sym;
    }
    else if (choice == 5 && board[5] == '5')
    {
      board[5] = sym;
    }
    else if (choice == 6 && board[6] == '6')
    {
      board[6] = sym;
    }
    else if (choice == 7 && board[7] == '7')
    {
      board[7] = sym;
    }
    else if (choice == 8 && board[8] == '8')
    {
      board[8] = sym;
    }
    else if (choice == 9 && board[9] == '9')
    {
      board[9] = sym;
    }
    else
    {
      cout << "Wrong Input ";
      player--;
      cin.ignore();
      cin.get();
    }
    i = toe();
    player++;
  } while (i == -1);
  tac();
  if (i == 1)
  {
    cout << "Player " << --player << " Win \U0001F389";
  }
  else
  {
    system("cls");
    cout << "Game Draw ";
    tic();
    cin.ignore();
    cin.get();
    return 0;
  }
}
int toe()
{                                                   // Complete
  if (board[1] == board[2] && board[2] == board[3]) // 1 == 2 == 3;
  {
    return 1;
  }
  else if (board[4] == board[5] && board[5] == board[6]) // 4 == 5 == 6;
  {
    return 1;
  }
  else if (board[7] == board[8] && board[8] == board[9]) // 7 == 8 == 9;
  {
    return 1;
  }
  else if (board[1] == board[4] && board[4] == board[7]) // 1 == 4 == 7;
  {
    return 1;
  }
  else if (board[2] == board[5] && board[5] == board[8]) // 2 == 5 == 8;
  {
    return 1;
  }
  else if (board[3] == board[6] && board[6] == board[9]) // 3 == 6 == 9;
  {
    return 1;
  }
  else if (board[1] == board[5] && board[5] == board[9]) // 1 == 5 == 9;
  {
    return 1;
  }
  else if (board[3] == board[5] && board[5] == board[7]) // 3 == 5 == 7;
  {
    return 1;
  }
  else if (board[1] != '1' &&
           board[2] != '2' &&
           board[3] != '3' &&
           board[4] != '4' &&
           board[5] != '5' &&
           board[6] != '6' &&
           board[7] != '7' &&
           board[8] != '8' &&
           board[9] != '9')
  {
    return 0;
  }
  else
  {
    return -1;
  }
};
void tac()
{
  system("cls");
  cout << "\t\t\t\t\t\t   Tic Tac Toe\n";

  cout << "\t\t\t\t\t    Player 1 (X)  -  Player 2 (O)" << endl
       << endl;
  cout << endl;

  cout << "\t\t\t\t\t\t  _________________" << endl;
  cout << "\t\t\t\t\t\t  | " << board[1] << "  |  " << board[2] << "  |  " << board[3] << "  |  " << endl;

  cout << "\t\t\t\t\t\t  _____|_____|_____" << endl;
  cout << "\t\t\t\t\t\t       |     |     " << endl;

  cout << "\t\t\t\t\t\t  | " << board[4] << "  |  " << board[5] << "  |  " << board[6] << "  |  " << endl;

  cout << "\t\t\t\t\t\t  _____|_____|_____" << endl;
  cout << "\t\t\t\t\t\t       |     |     " << endl;

  cout << "\t\t\t\t\t\t  | " << board[7] << "  |  " << board[8] << "  |  " << board[9] << "  |  " << endl;

  cout << "\t\t\t\t\t\t  _________________ " << endl
       << endl;
}
int main()
{
  tic();
}

8. By soufiane_ben_yaala

Made by soufiane_ben_yaala. You need to choose a number between 1 – 9 to pick your next move. ( Source )

#include <iostream>

using namespace std;

int main()
{
    char PLAYER_X='X',PLAYER_O='O';
    char lastPlayer=PLAYER_O,currentPlayer=PLAYER_X;
    char arr[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};
    bool tab[10]={false,false,false,false,false,false,false,false,false,false};
    int n;
    int x=0;
    for(int i=0;i<9;i++)
    {
        cout << "\n current board state:\n" <<endl;
        for(int i=0;i<3;i++)
            {
                cout << "    ";
                for(int j=0;j<3;j++)
                    { if ((j==0) || (j==1))
                      cout << arr[i][j] << "|";
                      if (j==2)
                      cout << arr[i][j] << "\n" << endl;
                    }
                cout << "    -+-+-\n" <<endl;
            }
     if(((arr[0][0]=='X') && (arr[0][1]=='X') && (arr[0][2]=='X')) || ((arr[0][0]=='O') && (arr[0][1]=='O') && (arr[0][2]=='O')))
        {cout << "player "<< lastPlayer <<" wins on the top now" <<endl;
        x=1;
        break;}
     if(((arr[1][0]=='X') && (arr[1][1]=='X') && (arr[1][2]=='X')) || ((arr[1][0]=='O') && (arr[1][1]=='O') && (arr[1][2]=='O')))
        {cout << "player "<< lastPlayer <<" wins on the middle now" <<endl;
        x=1;
        break;}
     if(((arr[2][0]=='X') && (arr[2][1]=='X') && (arr[2][2]=='X')) || ((arr[2][0]=='O') && (arr[2][1]=='O') && (arr[2][2]=='O')))
        {cout << "player "<< lastPlayer <<" wins on the bottom now" <<endl;
        x=1;
        break;}
    if((((arr[0][0]=='X') && (arr[1][1]=='X') && (arr[2][2]=='X'))||((arr[2][0]=='X')&&(arr[1][1]=='X')&&(arr[0][2]=='X')))|| (((arr[0][0]=='O') && (arr[1][1]=='O') && (arr[2][2]=='O'))||((arr[2][0]=='O')&&(arr[1][1]=='O')&&(arr[0][2]=='O'))))
        {cout << "player "<< lastPlayer <<" wins on the upward diagonal now" <<endl;
        x=1;
        break;}
    if(((arr[0][0]=='X') && (arr[1][0]=='X') && (arr[2][0]=='X'))|| ((arr[0][0]=='O') && (arr[1][0]=='O') && (arr[2][0]=='O')))
        {cout << "player "<< lastPlayer <<" wins on the right now" <<endl;
        x=1;
        break;}
    if(((arr[0][1]=='X') && (arr[1][1]=='X') && (arr[2][1]=='X'))|| ((arr[0][1]=='O') && (arr[1][1]=='O') && (arr[2][1]=='O')))
        {cout << "player "<< lastPlayer <<" wins on the middle now" <<endl;
        x=1;
        break;}
    if(((arr[0][2]=='X') && (arr[1][2]=='X') && (arr[2][2]=='X'))|| ((arr[0][2]=='O') && (arr[1][2]=='O') && (arr[2][2]=='O')))
        {cout << "player "<< lastPlayer <<" wins on the left now" <<endl;
        x=1;
        break;}



       cout << "Player "<< currentPlayer <<", enter a number between 1 and 9: " ;
       cin >> n;

       if ((n>9) || (tab[n]==true))
            {while((tab[n]==true) || (n>9))
               {
                   cout << "That square is not available. Try again." << endl;
                   cout << "Player "<< currentPlayer <<", enter a number between 1 and 9: " ;
                    cin >> n;
               }
            }


    if ((n==1) || (n==2) || (n==3))
                   arr[0][n-1]=currentPlayer;
                else {if ((n==4) || (n==5) || (n==6))
                          arr[1][n-4]=currentPlayer;

                     else
                          arr[2][n-7]=currentPlayer;}
     tab[n]=true;
     swap(currentPlayer,lastPlayer);


    }
    if (x==0)
    {cout << "\n current board state:\n" <<endl;
        for(int i=0;i<3;i++)
            {
                cout << "    ";
                for(int j=0;j<3;j++)
                    { if ((j==0) || (j==1))
                      cout << arr[i][j] << "|";
                      if (j==2)
                      cout << arr[i][j] << "\n" << endl;
                    }
                cout << "    -+-+-\n" <<endl;
            }
        cout <<"Draw.Nobody wins"<< endl;}
    return 0;
}

9. By Mickael Saavedra

Made by Mickael Saavedra. C++ Tic Tac Toe program, Please copy/paste in your IDE and replace system(“clear”) by system(“cls”) if you’re using Windows instead of Linux. ( Source )

#include <iostream>

using namespace std;

main()
{
  char grid[9], example[9];
  char initValue = '1', continueGame;
  bool playerO = false, playerX = true, playerOwins, playerXwins, gameOver = false;
  int cell;
  while((playerO || playerX) && gameOver == false) // As long as player0 or playerX are playing and that the variable gameOver hasn't been set to 1
  {
    playerXwins = (
    (grid[0] == 'X' && grid[1] == 'X' && grid[2] == 'X') ||
    (grid[3] == 'X' && grid[4] == 'X' && grid[5] == 'X') ||
    (grid[6] == 'X' && grid[7] == 'X' && grid[8] == 'X') ||
    (grid[0] == 'X' && grid[3] == 'X' && grid[6] == 'X') ||
    (grid[1] == 'X' && grid[4] == 'X' && grid[7] == 'X') ||
    (grid[2] == 'X' && grid[5] == 'X' && grid[8] == 'X') ||
    (grid[0] == 'X' && grid[4] == 'X' && grid[8] == 'X') ||
    (grid[2] == 'X' && grid[4] == 'X' && grid[6] == 'X')); // We declare all possible outcomes that define playerX winning

    playerOwins = (
    (grid[0] == 'O' && grid[1] == 'O' && grid[2] == 'O') ||
    (grid[3] == 'O' && grid[4] == 'O' && grid[5] == 'O') ||
    (grid[6] == 'O' && grid[7] == 'O' && grid[8] == 'O') ||
    (grid[0] == 'O' && grid[3] == 'O' && grid[6] == 'O') ||
    (grid[1] == 'O' && grid[4] == 'O' && grid[7] == 'O') ||
    (grid[2] == 'O' && grid[5] == 'O' && grid[8] == 'O') ||
    (grid[0] == 'O' && grid[4] == 'O' && grid[8] == 'O') ||
    (grid[2] == 'O' && grid[4] == 'O' && grid[6] == 'O')); // We declare all possible outcomes that define playerO winning

    cout << "Welcome to the game of Tic Tac Toe by SilentSib!" << endl << endl;
    cout << " ------------- " << endl;
    
    for(int i=0;i<9;i++)
    {
      cout.width(2);
      example[i] = initValue++;
      cout << " | " << example[i];
      if(i==2) cout << " |" << endl << " ------------- " << endl;
      if(i==5) cout << " |" << endl << " ------------- " << endl;
      if(i==8) cout << " |";
    }
    cout << endl << " ------------- " << endl << endl; // We show the grid with numbers from 1-9 to tell the player how to play
    initValue = '1'; // We reinitialize initValue to 1 so that at every round, the same grid is displayed
    
    if(playerXwins) { playerX = true; playerO = false; } // If playerX has won, we make sure that the last player displayed is playerX
    else if(playerOwins) { playerO = true; playerX = false; } // If playerO has won, we make sure that the last player displayed is playerO
    
    if(playerX) cout << "Player 1 (X): " << endl << endl;
    else if(playerO) cout << "Player 2 (O): " << endl << endl;
    
    cout << " ------------- " << endl;
    for(int i=0;i<9;i++)
    {
      cout.width(2);
      if(grid[i] != 'X' && grid[i] != 'O') cout << " | " << " ";
      else cout << " | " << grid[i];
      if(i==2) cout << " |" << endl << " ------------- " << endl;
      if(i==5) cout << " |" << endl << " ------------- " << endl;
      if(i==8) cout << " |";
    }
    cout << endl << " ------------- " << endl << endl; // The grid is refreshed at every new turn
    
    if(playerXwins) { cout << "Player 1 has won!" << endl << endl; gameOver = true; } // gameOver is declared upon playerX winning
    else if(playerOwins) { cout << "Player 2 has won!" << endl << endl; gameOver = true; } // gameOver is declared upon playerO winning

    for(int i=0;i<9 && (grid[i] == 'X' || grid[i] == 'O');i++)
    {
      if(i==8 && !(playerXwins || playerOwins))
      {
        gameOver = true;
        cout << "This is a draw." << endl << endl;
      }
    } // Definition of what constitutes a draw
    
    if(gameOver == false) // We continue the game as long as gameOver hasn't been declared
    {
      cout << "Please choose the cell value (1-9) where you want to play: "; cin >> cell; cout << endl;
      while(cell < 1 || cell > 9 || isalpha(cell) || grid[cell-1] == 'X' || grid[cell-1] == 'O')
      {
        cin.clear();
        cin.ignore();
        cout << "Impossible to play here." << endl << endl;
        cout << "Please choose the cell value (1-9) where you want to play: "; cin >> cell; cout << endl;
      }
      if(playerX)
      {
        grid[cell-1] = 'X';
        playerX = false;
        playerO = true;
        system("clear");
      }
      else if(playerO)
      {
        grid[cell-1] = 'O';
        playerO = false;
        playerX = true;
        system("clear");
      }
    }
    else // This code is run if gameOver is true
    {
      cout << "Do you want to play a new game? (y/n) "; cin >> continueGame;
      while(continueGame != 'y' && continueGame != 'Y' && continueGame != 'n' && continueGame != 'N')
      {
        cin.clear();
        cin.ignore();
        cout << endl << "Incorrect choice. Please try again: "; cin >> continueGame;
      }
      if(continueGame == 'y' || continueGame == 'Y')
      {
        gameOver=false;
        for(int i=0;i<9;i++)
        {
          grid[i] = '\0';
          playerXwins = false;
          playerOwins = false;
          playerX = true;
          playerO = false;
          system("clear");
        } // Player wants to continue playing, so we reset all values used in the game to start over
      }
      else if(continueGame == 'n' || continueGame == 'N')
      {
        cout << endl << "Thank you for playing this game!";
        return 0;
      }
    }
  }
}

10. By Nicholas I

Made by Nicholas I. Its a AI C++ Tic Tac Toe game. The game is played by Ai, so you get different outputs every time you run the program. ( Source )

// "tic-tac-toe" AI versus AI

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int check(short int (&n)[9]){
    short int e=0;
    if (n[0]==n[1] && n[1]==n[2] 
    && n[2]!=8){e++;}
    if (n[3]==n[4] && n[4]==n[5] 
    && n[5]!=8){e++;}
    if (n[6]==n[7] && n[7]==n[8] 
    && n[8]!=8){e++;}
    if (n[0]==n[3] && n[3]==n[6] 
    && n[6]!=8){e++;}
    if (n[1]==n[4] && n[4]==n[7] 
    && n[7]!=8){e++;}
    if (n[2]==n[5] && n[5]==n[8] 
    && n[8]!=8){e++;}
    if (n[0]==n[4] && n[4]==n[8] 
    && n[8]!=8){e++;}
    if (n[2]==n[4] && n[4]==n[6] 
    && n[6]!=8){e++;}
    return e;
}
int main() {
    short int m[9];
    short int a,b,c,t=9;
    srand (time(0));
    for(a=0;a<9;a++) {
        m[a]=8;
    }
  turn:
//player1
    a = rand()%t;
    b = 0;
    while (a>=0){
        if (m[b]==8){
            a--;
        }
        b++;
    }
    m[b-1]=1;
    t--;
    if(check(m)>0){
        cout <<"PLAYER X WIN"<<endl;
        goto end;
    }
    if(t==0){
        cout <<"-DRAW-"<<endl;
        goto end;
    }
    
//player2
    a = rand()%t;
    b = 0;
    while (a>=0){
        if (m[b]==8){
            a--;
        }
        b++;
    }
    m[b-1]=2;
    t--;
    if(check(m)>0){
        cout <<"PLAYER O WIN"<<endl;
        goto end;
    }
    if(t==0){
        cout <<"-DRAW-"<<endl;
        goto end;
    }
    
    goto turn;
    end:
//show result
    t=3;
    c=0;
    for(b=0;b<3;b++,t=t+3){
        for (a=c;a<t;c++,a++){
            if(m[a]==8) {
                cout <<" -";
            }
            if(m[a]==1) {
                cout <<" X";
            }
            if(m[a]==2) {
                cout <<" O";
            }
        }
        cout<<endl;
    }
}

11. By Black Hammer

Made by Black Hammer. ( Source )

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int N = 3, EMPTY = 0, COMP = 1, USER = -1;

int main()
{
	int board[N][N], row, col, temp_row, temp_col, round, i;
	bool win;

	srand(time(NULL));

	// init board[][] to be empty
	for (row = 0; row < N; row++)
		for (col = 0; col < N; col++)
			board[row][col] = EMPTY;

	// run the game (at most N*N times)
	for (round = 0; round < N*N; round++)
	{
		// get the next move
		do
		{
			if (round % 2 == 0)
			{
				cout << "\n Enter your move (by entering two numbers between 0 and 2): ";
				cin >> row >> col;
			}
			else
			{
				row = rand() % N;
				col = rand() % N;
			}
		} while ( row<0 || row>=N || col<0 || col>=N || board[row][col]!=EMPTY);
		
		// mark the board
		board[row][col] = (round % 2 == 0) ? USER : COMP;

		// draw current board
		cout << endl << "Board after round # " << round << endl;
		for (temp_row = 0; temp_row < N; temp_row++)
		{
			for (temp_col = 0; temp_col < N; temp_col++)
				if (board[temp_row][temp_col] == EMPTY)
					cout << " - ";
				else if (board[temp_row][temp_col] == USER)
					cout << " x ";
				else cout << " o ";
				cout << endl;
		}

		// check if there is a winner
		win = true;
		for (i = 0; i < N && win; i++)
			if (board[row][i] != board[row][col])
				win = false;
		if (win)
			break;

		win = true;
		for (i = 0; i < N && win; i++)
			if (board[i][col] != board[row][col])
				win = false;
		if (win)
			break;

		win = true;
		for (i = 0; i < N && win; i++)
			if (board[i][i] != board[N - 1][N - 1] || board[i][i] == 0)
				win = false;
		if (win)
			break;

		win = true;
		for (i = 0; i < N && win; i++)
			if (board[N - 1 - i][N - 1 - i] != board[0][N - 1] || board[N - 1 - i][N - 1 - i] == 0)
				win = false;
		if (win)
			break;
	}

	if (win)
		if (round % 2 == 0)
			cout << "\n\n     YOU WON ;(";
		else
			cout << "\n\n     I WON ;)";
	else
		cout << "\n\n     -*-  TIE  -*-";

	cout << endl << endl;
	return 0;
}	

12. By Harsha Krishna

Made by Harsha Krishna. ( Source )

#include <GL/glut.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

int board[3][3];    // board for gameplay
int turn;            // current move
int result;            // Result of the game
bool over;            // Is the game Over?

/*
    Sets the board for Tic Tac Toe
*/
void Intialize()
{
    turn=1;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
            board[i][j]=0;
    }
}
/*
    Called when any key from keyboard is pressed
*/
void OnKeyPress(unsigned char key,int x,int y)
{
    switch(key)
    {
        case 'y':
        if(over==true)
        {
            over=false;
            Intialize();
        }
        break;
        case 'n':
        if(over==true)
        {
            exit(0);
        }
        break;
        default:
            exit(0);
    }
}

/*
    Called when Mouse is clicked 
*/
void OnMouseClick(int button,int state,int x,int y)    
{
    if(over==false && button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
    {
        if(turn==1)
        {
            if(board[(y-50)/100][x/100]==0)
            {
                board[(y-50)/100][x/100]=1;
                turn=2;
            }
        }
        else if(turn==2)
        {
            if(board[(y-50)/100][x/100]==0)
            {
                board[(y-50)/100][x/100]=2;
                turn=1;
            }
        }
    }    
}

/*
    Utility function to draw string     
*/
void DrawString(void *font,const char s[],float x,float y)
{
    unsigned int i;
    glRasterPos2f(x,y);
    for(i=0;i<strlen(s);i++)
    {
        glutBitmapCharacter(font,s[i]);
    }
}

/*
    Function to draw up the horizontal and vertical lines
*/
void DrawLines()
{
    glBegin(GL_LINES);
    glColor3f(0,0,0);
    
    glVertex2f(100,50);
    glVertex2f(100,340);
    glVertex2f(200,340);
    glVertex2f(200,50);
    
    glVertex2f(0,150);
    glVertex2f(300,150);
    glVertex2f(0,250);
    glVertex2f(300,250);
    
    glEnd();        
}

/*
    Utility function to draw the circle
*/
void DrawCircle(float cx, float cy, float r, int num_segments) 
{
    glBegin(GL_LINE_LOOP);
    for (int i = 0; i < num_segments; i++)   
    {
        float theta = 2.0f * 3.1415926f * float(i) / float(num_segments);//get the current angle 
        float x = r * cosf(theta);//calculate the x component 
        float y = r * sinf(theta);//calculate the y component 
        glVertex2f(x + cx, y + cy);//output vertex 
    }
    glEnd();
}

/*
    Function to draw the cross and circle of Tic Tac Toe
*/
void DrawXO()
{
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            if(board[i][j]==1)
            {
                glBegin(GL_LINES);
                glVertex2f(50 + j * 100 - 25, 100 + i * 100 - 25);
                glVertex2f(50 + j * 100 + 25, 100 + i * 100 + 25);
                glVertex2f(50 + j * 100 - 25, 100 + i * 100 + 25);
                glVertex2f(50 + j * 100 + 25, 100 + i * 100 - 25);
                glEnd();
            }
            else if(board[i][j]==2)
            {
                
                DrawCircle(50 + j*100 , 100 + i*100 , 25 , 15);
            }
        }
    }
}

/*
    Function to check if there is any winner 
*/
bool CheckWinner()
{
    int i, j;
    // horizontal check
    for(i=0;i<3;i++)
    {
        for(j=1;j<3;j++)
        {
            if(board[i][0]!=0 && board[i][0]==board[i][j])
            {
                if(j==2)
                {
                    return true;
                }
            }
            else
                break;
        }
    }
    // vertical check
    for(i=0;i<3;i++)
    {
        for(j=1;j<3;j++)
        {
            if(board[0][i]!=0 && board[0][i]==board[j][i])
            {
                if(j==2)
                    return true;
            }
            else
                break;
        }
    }
    // Diagonal check
    if((board[0][0]!=0 && board[0][0]==board[1][1] && board[0][0]==board[2][2]) 
    || (board[2][0]!=0 && board[2][0]==board[1][1] && board[2][0]==board[0][2]))
        return true;
    return false;
}

/*
    function to check if there is draw
*/
bool CheckIfDraw()
{
    int i, j;
    bool draw;
    for(i=0;i<3;i++)
    { 
        for(j=0;j<3;j++)
        { 
            if(board[i][j]==0)
                return false;
        }
    }
    return true;    
}

/*
    Function to display up everything
*/
void Display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(1, 1, 1, 1);
    glColor3f(0, 0, 0);
    if(turn == 1)
        DrawString(GLUT_BITMAP_HELVETICA_18, "Player1's turn", 100, 30);    
    else
        DrawString(GLUT_BITMAP_HELVETICA_18, "Player2's turn", 100, 30);    
    
    DrawLines();
    DrawXO();
    
    if(CheckWinner() == true)
    {
        if(turn == 1)
        {
            over = true;
            result = 2;
        }
        else
        {
            over = true;
            result = 1; 
        }
    }
    else if(CheckIfDraw() == true)
    {
        over = true;
        result = 0;
    }
    if(over == true)
    {
        DrawString(GLUT_BITMAP_HELVETICA_18, "Game Over", 100, 160);
        if(result == 0)
            DrawString(GLUT_BITMAP_HELVETICA_18, "It's a draw", 110, 185);
        if(result == 1)
            DrawString(GLUT_BITMAP_HELVETICA_18, "Player1 wins", 95, 185);
        if(result == 2)
            DrawString(GLUT_BITMAP_HELVETICA_18, "Player2 wins", 95, 185);
        DrawString(GLUT_BITMAP_HELVETICA_18, "Do you want to continue (y/n)", 40, 210);
    }
    glutSwapBuffers();
}

/*
    Function to reshape
*/
void Reshape(int x, int y)
{
    glViewport(0, 0, x, y);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, x, y, 0, 0, 1);
    glMatrixMode(GL_MODELVIEW);
}

/*
    Driver Function
*/
int main(int argc, char **argv)
{
    Intialize();
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
    glutInitWindowPosition(550,200);
    glutInitWindowSize(300,350);
    glutCreateWindow("Tic Tac Toe");
    glutReshapeFunc(Reshape);
    glutDisplayFunc(Display);
    glutKeyboardFunc(OnKeyPress);
    glutMouseFunc(OnMouseClick);
    glutIdleFunc(Display);
    glutMainLoop();
    return 0;
}

13. By Vatsal Singh

Made by Vatsal Singh. At the start of the program you will get the option to set names for both players, after that you can proceed to play the game. ( Source )

#include <iostream>
#include <stdlib.h>
#include <string.h>

using namespace std;


class Game
{
    char arr[10] = {'0','1','2','3','4','5','6','7','8','9'};

    int player=1, place, check;
    int score1=0, score2=0;
    char mark, Name1[10], Name2[10], PName[10];

public:

    void Display();
        void Reset();

    void Interaction(); //MAIN
        void Intro();
        void Score();

    void Logic();

};



void Game::Display()
{
    system("cls");

//HEADER
    cout << "\n\n\t\t     TIC TAC TOE" << endl;
    cout << "\t\t -x_______________x-" << endl;

    cout << "\n\t    " << Name1 << " (X)              "<< Name2 << " (O)\n\n" << endl;

//GRID (MAINFRAME)
    cout << "\t\t" << "      |       |   " << endl;
    cout << "\t\t   " << arr[1] << "  |   " << arr[2] << "   |  " << arr[3] << endl;

    cout << "\t\t" << "______|_______|______" << endl;
    cout << "\t\t" << "      |       |   " << endl;

    cout << "\t\t   " << arr[4] << "  |   " << arr[5] << "   |  " << arr[6] << endl;

    cout << "\t\t" << "______|_______|______" << endl;
    cout << "\t\t" << "      |       |   " << endl;

    cout << "\t\t   " << arr[7] << "  |   " << arr[8] << "   |  " << arr[9] << endl;
    cout << "\t\t" << "      |       |   \n\n" << endl;
}



//RESTARTING
void Game::Reset()
{
  player=1;

  arr[0] = '0';
  arr[1] = '1';
  arr[2] = '2';
  arr[3] = '3';
  arr[4] = '4';
  arr[5] = '5';
  arr[6] = '6';
  arr[7] = '7';
  arr[8] = '8';
  arr[9] = '9';

 /*
 arr[10] = {'0','1','2','3','4','5','6','7','8','9'};

 WAS NOT WORKING
 */
}



void Game::Interaction()
{
    system("cls");

//GAME STARTS
    do
    {
        Display();

    //CHANGING PLAYERS TURN (1&2)
            if(player%2==0)
            {
                player = 2;
                strcpy(PName,Name2);
            }
            else
            {
                strcpy(PName,Name1);
                player = 1;
            }

    //TAKING INPUT
        cout << "\t\t     " << PName << "'s Turn\n" << endl;
        cout << "\t\tEnter the place value : " ;
        cin >> place;

        mark = (player==1) ? 'X':'O' ;       //boolean expression

    //MARKING THE PLACE
        if(place == 1 && arr[1] == '1')  arr[1] = mark;
        else if(place == 2 && arr[2] == '2')  arr[2] = mark;
        else if(place == 3 && arr[3] == '3')  arr[3] = mark;
        else if(place == 4 && arr[4] == '4')  arr[4] = mark;
        else if(place == 5 && arr[5] == '5')  arr[5] = mark;
        else if(place == 6 && arr[6] == '6')  arr[6] = mark;
        else if(place == 7 && arr[7] == '7')  arr[7] = mark;
        else if(place == 8 && arr[8] == '8')  arr[8] = mark;
        else if(place == 9 && arr[9] == '9')  arr[9] = mark;
        else
        {
            cout << "Please enter valid place no. !!!!" << endl;
            player--;
        }

    //FOR INCREMENTING THE LOOP CONDITION
        player++;
        Logic();        //TO CHECK THE WINNING CONDITION

        if(check==1)
        {
            Display();
        }
    }
    while(check==0);        //CONTINUATION CONDITION IS WHEN CHECK IS 0

//RESULT
    if(check==1)
    {
        cout << "\n\t\t      CONGRATULATIONS!!!\n";
        cout << "\n\t      "<< PName << " HAS WON THE GAME!!!" << endl;

        if(player%2==0)
        {
            score1++;
        }
        else
        {
            score2++;
        }
    }
    else
    {
        cout << "\n\t\t      MATCH HAS BEEN DRAW!!!" << endl;
    }
}



void Game::Intro()
{
//HEADER
    cout << "\n\n\t\t     TIC TAC TOE" << endl;
    cout << "\t\t -x_______________x-" << endl;
    cout << "\n\t       Created by Vatsal Singh\n\n" << endl;

//INTRODUCTION OF USERS
    cout << "\tEnter Player 1 Name : " ;
    cin >> Name1;

    cout << "\tEnter Player 2 Name : ";
    cin >> Name2;
}



void Game::Score()
{
    char Exit;

    system("cls");

    cout << "\n\n\t\t      SCOREBOARD" << endl;
    cout << "\t\t  -x_______________x-\n" << endl;

    cout << "\t\t"  << Name1 << "\t\t  " << Name2 << endl;
    cout << "\n\t\t  " << score1 << "\t\t     " << score2;

//FOR GIVING TIME TO SEE RESULTS
    cout << "\n\n\n\n\n\tPress any key to exit " ;
    cin >> Exit;
}



void Game::Logic()
{
//WINNING CONDITIONS-->

    //CHECKING HORIZONTALLY
    if(mark==arr[1] && mark==arr[2] && mark==arr[3]) check = 1;
    else if(mark==arr[4] && mark==arr[5] && mark==arr[6]) check = 1;
    else if(mark==arr[7] && mark==arr[8] && mark==arr[9]) check = 1;

    //CHECKING VERTICALLY
    else if(mark==arr[1] && mark==arr[4] && mark==arr[7]) check = 1;
    else if(mark==arr[2] && mark==arr[5] && mark==arr[8]) check = 1;
    else if(mark==arr[3] && mark==arr[6] && mark==arr[9]) check = 1;

    //CHECKING DIAGONALLY
    else if(mark==arr[1] && mark==arr[5] && mark==arr[9]) check = 1;
    else if(mark==arr[3] && mark==arr[5] && mark==arr[7]) check = 1;

//CONTINUATION CONDITION
    else if(arr[1]=='1' || arr[2]=='2' || arr[3]=='3' || arr[4]=='4' || arr[1]=='5' || arr[5]=='6' || arr[7]=='7' || arr[8]=='8' || arr[9]=='9')
    {
        check = 0;
    }

//DRAW CONDITION
    else
    {
        check = -1;
    }
}



int main()
{
    char choice;

    Game obj;
    obj.Intro();

     do
     {
        obj.Interaction();
        obj.Reset();

        cout << "\n\t\tPLAY AGAIN (Y/N) : " ;
        cin >> choice;

     //TO PLAY AGAIN
     }while(choice == 'Y' || choice == 'y');

    obj.Score();

    return 0;
}

14. By Mohammad Aaqib

Made by Mohammad Aaqib. Simple C++ Tic Tac Toe. The game begins with a toss, you need to pick heads or tails, after the toss you can give your move by entering row and column place. ( Source )

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

class TicTacToe
{
    int **game;
    int counter;

public:

    TicTacToe()
    {
        game = new int*[3];
        for (int i = 0; i < 3; i++)
            game[i] = new int[3];

        counter = 0;

        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
                game[i][j] = 0;
        }
    }

    ~TicTacToe()
    {
        for (int i = 0; i < 3; i++)
        {
            delete game[i];
        }
        delete[] game;
        game = NULL;
    }

    

    void input(int&, int&);
    void display();
    bool winner();
    void checker(int&, int&);
    void play_1();
    void play_2();
    void play();
};

void TicTacToe:: display()
{
    system("cls");
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
            cout << game[i][j] << "\t";

        cout << endl;
    }
}

void TicTacToe:: input(int& row, int& column)
{
    cout << "Enter row you want to input (0 to 2): ";
    cin >> row;
    while (row < 0 || row > 2)
    {
        cout << "Invalid Input. Enter row from 0 to 2: ";
        cin >> row;
    }

    cout << "Enter column you want to input (0 to 2): ";
    cin >> column;
    while (column < 0 || column > 2)
    {
        cout << "Invalid Input. Enter column from 0 to 2: ";
        cin >> column;
    }
}

void TicTacToe:: checker(int& row, int& column)
{
    input(row, column);
    while (game[row][column] != 0)
    {
        cout << "Entry already filled\n";
        input(row, column);
    }
}

void TicTacToe:: play_1()
{
    int counter = 0;
    int row;
    int column;

    cout << "\n\n\t\tPLAYER 1 TURN:\n";
    checker(row, column);
    game[row][column] = 1;
    display();

}

void TicTacToe:: play_2()
{
    int counter = 0;
    int row;
    int column;
    cout << "\n\n\t\tPLAYER2 TURN:\n";
    checker(row, column);
    game[row][column] = 2;
    display();

}

void TicTacToe:: play()
{
    srand(time(0));
    int choose;
    int turn = rand() % 2;
    cout << "\n\tToss Time\n";
    cout << "\tPlayer2 will flip the coin\n";
    cout << "\tPlayer1 press 0 for heads or 1 for tails: ";
    cin >> choose;
    while (choose < 0 || choose > 1)
    {
        cout << "Invalid Choice!\n";
        cout << "Press 0 to choose heads or 1 for tails: ";
        cin >> choose;
    }
    display();
    if (turn == choose)
    {
        cout << "Player1 Wins the toss\n";
        while (counter != 9)
        {
            play_1();
            if (winner())
            {
                cout << "PLAYER 1 WINS!\n";
                return;
            }

            counter++;
            if (counter == 9)
                continue;

            play_2();
            if (winner())
            {
                cout << "PLAYER 2 WINS!\n";
                return;
            }
            counter++;
        }

        cout << "\n\tGAME DRAWN!\n";
    }
    else
    {
        cout << "Player2 Wins the toss\n";
        while (counter != 9)
        {
            play_2();
            counter++;
            if (winner())
            {
            cout << "PLAYER 2 WINS!\n";
            return;
            }

            if (counter == 9)
                continue;

            if (winner())
            {
                cout << "\n\tPLAYER 1 WINS!\n";
                return;
            }
            play_1();
            counter++;
        }
        cout << "\n\tGAME DRAWN!\n";
    }
}

bool TicTacToe:: winner()
{
    //horizontal check

    for (int i = 0; i < 3; i++)
    {
        if ((game[i][0] == 1 || game[i][0] == 2) && game[i][0] == game[i][1] && game[i][0] == game[i][2])
            return true;
    }

    //vertical check

    for (int i = 0; i < 3; i++)
    {
        if ((game[0][i] == 1 || game[0][i] == 2) && game[0][i] == game[1][i] && game[0][i] == game[2][i])
            return true;
    }

    //principal diagonal check

    if ((game[0][0] == 1 || game[0][0] == 2) && game[0][0] == game[1][1] && game[0][0] == game[2][2])
        return true;

    //secondary diagonal check


    if ((game[2][0] == 1 || game[2][0] == 2) && game[2][0] == game[1][1] && game[2][0] == game[0][2])
        return true;

    return false;
}

int main()
{
    TicTacToe obj;
    obj.play();
    return 0;
}

15. By Normand Lubaton

Made by Normand Lubaton. ( Source )

#include <iostream>
using namespace std;

char matrix[3][3] = {'1','2','3','4','5','6','7','8','9'};
char player = 'X';
int n;

void Draw()
{
    system("cls");
    cout << "Tic Tac Toe" << endl;
    
    for(int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            cout << matrix [i][j] << " ";
        } cout << endl;
    } 
}

void Input()
{
    int a;
    cout << "It's " << player << " turn. " << "Press a number on the field to cross/circle: ";
		
		cin >> a;
		if(a == 1)
		{
		    if(matrix[0][0] == '1')
		    	matrix [0][0] = player;
		
		    else
		    {
		    cout << "Field is already in use! Try again." << endl;
		    Input();
		    }
		}
		else if(a == 2)
		{
		    if(matrix[0][1] == '2')
		    	matrix [0][1] = player;
		
		    else
		    {
		    cout << "Field is already in use! Try again." << endl;
		    Input();
		    }
		}
		else if(a == 3)
		{
		    if(matrix[0][2] == '3')
		    	matrix [0][2] = player;
		
		    else
		    {
		    cout << "Field is already in use! Try again." << endl;
		    Input();
		    }
		}
		else if(a == 4)
		{
		    if(matrix[1][0] == '4')
		    	matrix [1][0] = player;
		
		    else
		    {
		    cout << "Field is already in use! Try again." << endl;
		    Input();
		    }
		}
		else if(a == 5)
		{
		    if(matrix[1][1] == '5')
		    	matrix [1][1] = player;
		
		    else
		    {
		    cout << "Field is already in use! Try again." << endl;
		    Input();
		    }
		}
		else if(a == 6)
		{
		    if(matrix[1][2] == '6')
		    	matrix [1][2] = player;
		
		    else
		    {
		    cout << "Field is already in use! Try again." << endl;
		    Input();
		    }
		}
		else if(a == 7)
		{
		    if(matrix[2][0] == '7')
		    	matrix [2][0] = player;
		
		    else
		    {
		    cout << "Field is already in use! Try again." << endl;
		    Input();
		    }
		}
		else if(a == 8)
		{
		    if(matrix[2][1] == '8')
		    	matrix [2][1] = player;
		
		    else
		    {
		    cout << "Field is already in use! Try again." << endl;
		    Input();
		    }
		}
		else if(a == 9)
		{
		    if(matrix[2][2] == '9')
		    	matrix [2][2] = player;
		
		    else
		    {
		    cout << "Field is already in use! Try again." << endl;
		    Input();
		    }
		}
}

void TogglePlayer()
{
    if(player == 'X')
    	{
    	player = 'O';
    	}
    else
    	player = 'X';
}

char Win()
{
	//first player
	if (matrix[0][0] == 'X' && matrix[0][1] == 'X' && matrix[0][2] == 'X')
		return 'X';
	if (matrix[1][0] == 'X' && matrix[1][1] == 'X' && matrix[1][2] == 'X')
		return 'X';
	if (matrix[2][0] == 'X' && matrix[2][1] == 'X' && matrix[2][2] == 'X')
		return 'X';

	if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == 'X')
		return 'X';
	if (matrix[0][1] == 'X' && matrix[1][1] == 'X' && matrix[2][1] == 'X')
		return 'X';
	if (matrix[0][2] == 'X' && matrix[1][2] == 'X' && matrix[2][2] == 'X')
		return 'X';

	if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == 'X')
		return 'X';
	if (matrix[2][0] == 'X' && matrix[1][1] == 'X' && matrix[0][2] == 'X')
		return 'X';

	//second player
	if (matrix[0][0] == 'O' && matrix[0][1] == 'O' && matrix[0][2] == 'O')
		return 'O';
	if (matrix[1][0] == 'O' && matrix[1][1] == 'O' && matrix[1][2] == 'O')
		return 'O';
	if (matrix[2][0] == 'O' && matrix[2][1] == 'O' && matrix[2][2] == 'O')
		return 'O';

	if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == 'O')
		return 'O';
	if (matrix[0][1] == 'O' && matrix[1][1] == 'O' && matrix[2][1] == 'O')
		return 'O';
	if (matrix[0][2] == 'O' && matrix[1][2] == 'O' && matrix[2][2] == 'O')
		return 'O';

	if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == 'O')
		return 'O';
	if (matrix[2][0] == 'O' && matrix[1][1] == 'O' && matrix[0][2] == 'O')
		return 'O';

	return '/';
}

int main() 
{
	n = 0;
	Draw();
	while (1)
	{
	    n++;
	    Input();
	    Draw();
	    if(Win() == 'X')
	    {
	        cout << "X wins!" << endl;
	        break;
	    }
	    else if (Win() == 'O')
	    {
	        cout << "O wins!" << endl;
	    }
	    else if (Win() == '/' && n == 9)
	    {
	        cout << "It's a draw!" << endl;
	        break;
	    }
	    TogglePlayer();
	}
	
	system("pause");
	return 0;
}

16. By ~aalia khan

Made by ~aalia khan. ( Source )

#include<graphics.h>
#include<conio.h>
#include<process.h>
void drawboard();
void round(int);
void cross(int);
void ask();
void turn(int);
void swap();
void winner();
int a[10],p,o,i,x,y,m,count;
void main()
{
int gd=0,gm;
initgraph(&gd,&gm,"");
for(int i=1;i<10;i++)
{
a[i]=2;
}
p=0;
o=1;
count=0;
drawboard();
while(1)
{
ask();
cleardevice();
drawboard();
winner();
swap();
}
}
void drawboard()
{
line(0,160,480,160);
line(0,320,480,320);
line(160,0,160,480);
line(320,0,320,480);
for(i=1;i<10;i++)
{
if(a[i]==0)
{
round(i);
}
if(a[i]==1)
{
cross(i);
}
}
}
void round(int a)
{
a=a-1;
x=a/3;
y=a%3;
circle(80+160*y,80+160*x,60);
}
void cross(int a)
{
a=a-1;
x=a/3;
y=a%3;
line(20+160*y,20+160*x,140+160*y,140+160*x);
line(20+160*y,140+160*x,140+160*y,20+160*x);
}
void ask()
{
m=getch();
m=m-48;
turn(m);
}
void turn(int i)
{
if(a[i]==2)
{
a[i]=p;
count=count+1;
}
else
{
outtextxy(480,10,"already marked");
ask();
}
}
void over()
{
getch();
closegraph();
exit(0);
}
void winner()
{
if(a[1]==0&&a[2]==0&&a[3]==0)
{
line(80,80,400,80);
outtextxy(480,10,"O wins");
over();
}
else if(a[1]==1&&a[2]==1&&a[3]==1)
{
line(80,80,400,80);
outtextxy(480,10,"X wins");
over();
}
else if(a[4]==0&&a[5]==0&&a[6]==0)
{
line(80,240,400,240);
outtextxy(480,10,"O wins");
over();
}
else if(a[4]==1&&a[5]==1&&a[6]==1)
{
line(80,240,400,240);
outtextxy(480,10,"X wins");
over();
}
else if(a[7]==0&&a[8]==0&&a[9]==0)
{
line(80,400,400,400);
outtextxy(480,10,"O wins");
over();
}
else if(a[7]==1&&a[8]==1&&a[9]==1)
{
line(80,400,400,400);
outtextxy(480,10,"X wins");
over();
}
else if(a[1]==0&&a[4]==0&&a[7]==0)
{
line(80,80,80,400);
outtextxy(480,10,"O wins");
over();
}
else if(a[1]==1&&a[4]==1&&a[7]==1)
{
line(80,80,80,400);
outtextxy(480,10,"X wins");
over();
}
else if(a[2]==0&&a[5]==0&&a[8]==0)
{
line(240,80,240,400);
outtextxy(480,10,"O wins");
over();
}
else if(a[2]==1&&a[5]==1&&a[8]==1)
{
line(240,80,240,400);
outtextxy(480,10,"X wins");
over();
}
else if(a[3]==0&&a[6]==0&&a[9]==0)
{
line(400,80,400,400);
outtextxy(480,10,"O wins");
over();
}
else if(a[3]==1&&a[6]==1&&a[9]==1)
{
line(400,80,400,400);
outtextxy(480,10,"X wins");
over();
}
else if(a[1]==0&&a[5]==0&&a[9]==0)
{
line(80,80,400,400);
outtextxy(480,10,"O wins");
over();
}
else if(a[1]==1&&a[5]==1&&a[9]==1)
{
line(80,80,400,400);
outtextxy(480,10,"X wins");
over();
}
else if(a[3]==0&&a[5]==0&&a[7]==0)
{
line(80,400,400,80);
outtextxy(480,10,"O wins");
over();
}
else if(a[3]==1&&a[5]==1&&a[7]==1)
{
line(80,400,400,80);
outtextxy(480,10,"X wins");
over();
}
else if(count==9)
{
outtextxy(480,10,"Draw");
over();
}
}
void swap()
{
p=p+o;
o=p-o;
p=p-o;
}