Skip to main content

Tic-Tac-Toe Implementation in Cpp (C++) Step by Step


Tic-tac-toe:

Tic-tac-toe is also known as noughts and crosses (X or O). It is a paper-pencil game for two players, which is played on a 3×3 grid. A player wins by placing three marks in a horizontal, vertical, or diagonal row.


Tic-Tac-Toe Implementation in Cpp: 

Tic-tac-toe is one of the most popular game in computer science world and it is built almost on all programming languages like  C, C++, and Java. now let's see how it can be implemented using c++.

Steps in designing Tic-Tac-Toe:

  • Tic-Tac-Toe is played on a 3×3 grid which is similar to 3x3 matrix lets consider it as a matrix. The numbers on this grid represent empty positions at which a user can place an X or O.
  • Create a character array to represent these positions which starts drom 0-9. we will not use 0th position it is just taken for our better understanding.
 char grid[10] = {'0','1','2','3','4','5','6','7','8','9'};



void Matrix(char *grid)
{ 
 system("cls");
 cout<< "\n\n\tTic Tac Toe"<<endl<<endl;
 cout<<"\tPlayer 1 (X)  -  Player 2 (O)"<<endl<<endl;
 cout<<" \t     |     |     "<<endl;
 cout<<" \t  "<< grid[1]<< "  |  "<<grid[2]<<"  |  "<<grid[3]<<endl;
 cout<<"\t_____|_____|_____"<<endl;
 cout<<"  \t     |     |     "<<endl;
 cout<<"\t  " << grid[4] <<"  |  "<<grid[5]<<"  |  "<<grid[6]<<endl;
 cout<<"\t_____|_____|_____"<<endl;
 cout<<"  \t     |     |     "<<endl;
 cout<<" \t  "<<grid[7]<<"  |  "<<grid[8]<<"  |  "<<grid[9]<<endl;
 cout<<"  \t     |     |     "<<endl<<endl;
}

  • Now, a player only wins when he places three marks in a horizontal, vertical, or diagonal row, which means that marks on any one of these positions must be same:
  • For Horizontal: 1=2=3, 4=5=6, 7=8=9.
  • For Diagonal: 1=5=9, 3=5=7.
  • For Vertical: 1=4=7, 2=5=8, 3=6=9
  • And If all the positions are filled and no player has managed to place three marks row game is Draw.

char Win(char *grid)
{
 if (grid[1] == grid[2] && grid[1] == grid[3])
  return 'w';
 else if (grid[4] == grid[5] && grid[4] == grid[6])
  return 'w';  //Win
 else if (grid[7] == grid[8] && grid[7] == grid[9])
  return 'w';
 else if (grid[1] == grid[4] && grid[1] == grid[7])
  return 'w';
 else if (grid[2] == grid[5] && grid[2] == grid[8])
  return 'w';
 else if (grid[3] == grid[6] && grid[3] == grid[9])
  return 'w';
 else if (grid[1] == grid[5] && grid[1] == grid[9])
  return 'w';
 else if (grid[3] == grid[5] && grid[3] == grid[7])
  return 'w';
 else if (grid[1] != '1' && grid[2] != '2' && grid[3] != '3' && grid[4] != '4' && grid[5] != '5' 
   && grid[6] != '6' && grid[7] != '7' && grid[8] != '8' && grid[9] != '9')
  return 'd'; //Draw
 else
  return 'c'; //continue
}

  • Now in your main function use turn=turn*-1 to switch between players.

Tic-Tac-Toe Complete Source Code in C++:

#include <iostream>

using namespace std;

char Win(char *grid)
{
 if (grid[1] == grid[2] && grid[1] == grid[3])
  return 'w';
 else if (grid[4] == grid[5] && grid[4] == grid[6])
  return 'w';  //Win
 else if (grid[7] == grid[8] && grid[7] == grid[9])
  return 'w';
 else if (grid[1] == grid[4] && grid[1] == grid[7])
  return 'w';
 else if (grid[2] == grid[5] && grid[2] == grid[8])
  return 'w';
 else if (grid[3] == grid[6] && grid[3] == grid[9])
  return 'w';
 else if (grid[1] == grid[5] && grid[1] == grid[9])
  return 'w';
 else if (grid[3] == grid[5] && grid[3] == grid[7])
  return 'w';
 else if (grid[1] != '1' && grid[2] != '2' && grid[3] != '3' && grid[4] != '4' && grid[5] != '5' 
   && grid[6] != '6' && grid[7] != '7' && grid[8] != '8' && grid[9] != '9')
  return 'd'; //Draw
 else
  return 'c'; //continue
}

void Matrix(char *grid)
{ 
 system("cls");
 cout<< "\n\n\tTic Tac Toe"<<endl<<endl;
 cout<<"\tPlayer 1: X & Player 2: O"<<endl<<endl;
 cout<<" \t     |     |     "<<endl;
 cout<<" \t  "<< grid[1]<< "  |  "<<grid[2]<<"  |  "<<grid[3]<<endl;
 cout<<"\t_____|_____|_____"<<endl;
 cout<<"  \t     |     |     "<<endl;
 cout<<"\t  " << grid[4] <<"  |  "<<grid[5]<<"  |  "<<grid[6]<<endl;
 cout<<"\t_____|_____|_____"<<endl;
 cout<<"  \t     |     |     "<<endl;
 cout<<" \t  "<<grid[7]<<"  |  "<<grid[8]<<"  |  "<<grid[9]<<endl;
 cout<<"  \t     |     |     "<<endl<<endl;
}

int main()
{
 int turn=-1,choice;
 char mark,decision;
 char grid[10] = {'0','1','2','3','4','5','6','7','8','9'};
 
 do
 {
  Matrix(grid);
  turn=turn*-1;
  if(turn>0)
  cout<<"Player 1"<< ": Enter a Number:  ";
  else
  cout<<"Player 2"<< ": Enter a Number:  ";
  cin>>choice;
  mark=(turn == 1) ? 'X' : 'O';
  if (choice == 1 && grid[1] == '1')
   grid[1] = mark;
  else if (choice == 2 && grid[2] == '2')
   grid[2] = mark;
  else if (choice == 3 && grid[3] == '3')
   grid[3] = mark;
  else if (choice == 4 && grid[4] == '4')
   grid[4] = mark;
  else if (choice == 5 && grid[5] == '5')
   grid[5] = mark;
  else if (choice == 6 && grid[6] == '6')
   grid[6] = mark;
  else if (choice == 7 && grid[7] == '7')
   grid[7] = mark;
  else if (choice == 8 && grid[8] == '8')
   grid[8] = mark;
  else if (choice == 9 && grid[9] == '9')
   grid[9] = mark;
  else
  {
   cout<<"Invalid move ";
   turn--;
   system("pause");
  }
  decision=Win(grid);
  turn++;
 }while(decision=='c');
 Matrix(grid);
 if(decision=='w')
 {
  --turn;
  if(turn<0)
  {
   turn*=-1;
   cout<<"\tPlayer: "<<turn<<" Win! "; 
  }
  else
  cout<<"\tPlayer: "<<turn<<" Win! "; 
 }
 else
  cout<<"\tGame Draw!";
 
 return 0;
}

Comments

Popular posts from this blog

Data Types

DATA TYPES in C/C++: Since C and C++ are strongly type languages, therefore every thing must be defined. Variables are classified according to their data type, which determines the kind of information that may be stored in them. Data Types Description C-Keyword Format Specifier integer Integers are whole numbers that can have both positive and negative values but no decimal values.   int   %d or %i Float Floating type variables can hold real numbers precision of 6 digits. float %f Double can hold real numbers with the precision of 14 digits. double %f Character data type allows a variable to store only one character. char %c Implementation in C: #include<stdio.h> int main() { printf(“%d n”,18); //this will print integer printf(“%fn”,32.56); //this will print decimal number printf(“%d %d %dn”,32,56,89); // %d is a place holder for integer and it provide place for one integer printf(“%ld...

Single LinkList in Java

Linked List: Linked List contains a sequence nodes which are linked together. Each node contains a connection to another link and data. Linked list is the second most-used data structure after array. Following are the important terms to understand the concept of Linked List. Link − Each link of a linked list can store a data called an element. Next − Each link of a linked list contains a link to the next link called Next. LinkedList − A Linked List contains the connection link to the first link called First. Types of Linked List: Following are the various types of linked list. Simple Linked List − Item navigation is forward only. Doubly Linked List − Items can be navigated forward and backward. Circular Linked List − Last item contains link of the first element as next and the first element has a link to the last element as previous. Basic Operations: Insert:  Inserts at tail,  specific index. Delete: Deletes from the tail.  specific index. ...