Tic Tac Toe

Using:

  • variables, data types, and numerical operators
  • basic input/output
  • logic (if statements, switch statements)
  • loops (for, while, do-while)
  • arrays
  • functions

This is an example source code for Tic Tac Toe against a smart computer player. This took about a week to make in total. I had help from forums on cplusplus.com. Free to copy.


#include <iostream>
#include <vector>

using namespace std;

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

void board();
int checkwin();
int cppmove();
int cppmovesmart();

int main()
{
 int win = 0;
 int tries = 0;
 int userinput;
 int player = 1;

 while (win == 0 && tries < 9)
 {
 board();

 if(player == 1){
 cout << "\tPlayer " << player << " choose a number: ";
 cin >> userinput;
 }
 else
 {
 userinput = cppmovesmart();
 cout << "\tPlayer cpp chooses number " << userinput << "...\n\n";
 system("PAUSE");
 }




 //Invalid move, doesn't use up tries.
 if(square[userinput] == 'X' || square[userinput] == '0')
 {
 cout << "INVALID MOVE";
 }
 else
 {
 square[userinput] = (player ==1) ? 'X' : 'O';

 //Check win
 win = checkwin();

 //Change player
 player = (player == 1) ? 2 : 1;

 //Add 1 to tries
 tries++;
 }
 }
 //Undo the changed player to output winner.
 player = (player == 1) ? 2 : 1;

 //Output result.
 if(win == 1){
 board();
 cout << "\n\n\tPlayer " << player << " WINS!!!\n\n";
 }
 else{
 cout << "\n\n\tDRAW\n\n";
 }
}
/* ########################################FUNCTIONS###############################################
###################################################################################################*/

//COMPUTER MOVIES SMART
int cppmovesmart()
{
 int s;

 if(square[1] == '1' && (square[2] == square[3] || square [4] == square[7] || square[5] == square[9]))
 {

 s = 1;
 }
 else if(square[2] == '2' && (square[1] == square[3] || square [5] == square[8]))
 {
 s = 2;
 }
 else if(square[3] == '3' && (square[1] == square[2] || square [6] == square[9] || square [5] == square[7]))
 {
 s = 3;
 }
 else if(square[4] == '4' && (square[5] == square[6] || square [1] == square[7]))
 {
 s = 4;
 }
 else if(square[5] == '5' && (square[4] == square[6] || square [2] == square[8] || square [1] == square[9] || square[7] == square[3]))
 {
 s = 5;
 }
 else if(square[6] == '6' && (square[4] == square[5] || square [3] == square[9]))
 {
 s = 6;
 }
 else if(square[7] == '7' && (square[8] == square[9] || square [1] == square[4] || square [5] == square[3]))
 {
 s = 7;
 }
 else if(square[8] == '8' && (square[7] == square[9] || square [2] == square[5]))
 {
 s = 8;
 }
 else if(square[9] == '9' && (square[7] == square[8] || square [3] == square[6] || square [1] == square[5]))
 {
 s = 9;
 }
 else
 {
 s = cppmove();
 }
 return s;
}

//COMPUTER MOVES RANDOMLY
int cppmove()
{
 int r;
 vector<int> newvector(0);

 for(int i=1; i<10; i++)
 {
 if(square[i] != 'X' && square[i] != 'O')
 {
 newvector.push_back(i);
 }
 }

 srand(time(0));
 r = newvector.at(rand() % newvector.size());

 return r;
}

&nbsp;

//BUILD BOARD
void board()
{
 system("cls"); //Clears screen.
 cout << "\n\n\tTic Tac Toe\n\n";

 //builds a 3*3 board with numbers 123,456,789.
 for (int i = 1; i < 10; i = i + 3)
 {
 cout << "\t" << square[i] << " " << square[i+1] << " " << square[i+2] << endl << endl;
 }
}

int checkwin()
{
 int win = 0;

 //Horizontal Lines
 for (int i = 1; i < 10; i = i + 3)
 {
 if(square[i] == square[i+1] && square[i+1] == square[i+2])
 {
 win = 1;
 }
 }
 //Vertical Lines
 for (int i=1; i<4; i++){
 if(square[i] == square[i+3] && square[i] == square[i+6]){
 win = 1;
 }
 }
 //Diagonal Lines
 if((square[1] == square[5] && square[1] == square[9])||
 (square[3] == square[5] && square[3] == square[7])){
 win = 1;
 }
 //Return 1 if win, else return 0.
 if(win == 1){
 return 1;
 }
 else{
 return 0;
 }
}