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

Pointers

Pointers: A pointer is a variable whose value is a memory address. A pointer contains the memory address of a variable that, in turn, contains a specific value. In this sense, a variable name directly references a value, and a pointer indirectly references a value. Syntax: type * variable ; Interpretation: The value of the pointer variable ptr is a memory address. A data item whose address is stored in this variable must be of the specified type. Dynamic Memory Management: C/C++ enables programmers to control the allocation and deallocation of memory in a program for any built in or user defined type. The ability for a program to obtain more memory space at execution time to hold new nodes, and to release space no longer needed is known as dynamic memory management. Syntax: int *num = (int *)malloc(sizeof (int)*numCount); or int *ptr = (int *)calloc(numCount, sizeof (int)); /* returns a pointer to a section of memory just large enough to hold the integers, whose q...

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. ...

Object Oriented Programming (OOP)

Object Oriented Programming: Object Oriented Programming (OOP) is a programming concept which used in the modern programming world. Languages like Java, C++,  and Python support Object Oriented Programming (OOP). It works on the principle that objects are the most important part of a program. In OOP we think in terms of objects and every object has its attributes (properties) and a state (behavior/functions). Object Oriented Programming (OOP) is a technique of system modeling and its main purpose is to understand the product before developing it and manipulating these objects to achieve a specific task.   Pillars of Object Oriented Language (OOP): There are four basic principles of Object Oriented Language (OOP). Inheritance Polymorphism Data Encapsulation Abstraction WHY is Object Oriented Language (OOP) NEEDED? Problems with Procedural Languages: Functions have unrestricted access to global data Unrelated Functions and data. Before Object Oriente...