A Project Report
on
Tic-Tac-Toe in C++
BACHELOR OF COMPUTER APPLICATION
By
Siddharth Sagar
AJU/221122
Under the guidance of
Mr. Debanjan Ghosh
(Assistant Professor)
&
Dr. Arvind Kumar Pandey
(Dean)
ARKA JAIN University, JHARKHAND
DEPARTMENT OF COMPUTER SCIENCE & INFORMATION TECHNOLOGY
CERTIFICATE
This is to certify that the project entitled “Tic Tac Toe in C++”, is bonafide
work of Siddharth Sagar bearing enrolment Number: AJU/221122 under the
guidance of Mr. Debanjan Ghosh submitted during studies BACHELOR OF
COMPUTER APPLICATION Program from ARKA JAIN University,
Jharkhand during the year 2023-2024.
Dr. Arvind Kumar Pandey
Dean
ARKA JAIN University, Jharkhand
[email protected] ii
ABSTRACT
This abstract presents a C++ implementation of the classic “Tic Tac Toe in C++” offering a
code-based rendition of the popular two-player strategy game. Utilizing object-oriented
programming principles, the program employs a 2D array to create a 3x3 grid as the game
board. Players take turns entering their chosen moves, marked as X or O, and the program
verifies the validity of each move while checking for a winning alignment. The game
incorporates user-friendly input mechanisms and feedback to guide players through the
gameplay. It includes logic for evaluating win conditions across rows, columns, and
diagonals, as well as detecting a draw when all cells are filled without a winner. The abstract
highlights the use of conditional statements and loops for turn management, move validation,
and game outcome determination. The code fosters a comprehensive understanding of C++
syntax, array manipulation, and basic game development concepts. In essence, this abstract
encapsulates a functional and educational implementation of Tic-Tac-Toe in C++,
showcasing the fusion of classic gameplay with programming techniques.
iii
ACKNOWLEDGEMENT
I would like to express my heartfelt gratitude to assistant professor Mr. Debanjan Ghosh and Dean
Dr. Arvind Kumar Pandey who gave me the golden opportunity to make this wonderful project on
“Tic Tac Toe in C++” which helped me in doing a lot of research and to learn many new things. I
am thankful to them.
iv
DECLARATION
I, Siddharth Sagar, hereby declare that the Project entitled “Tic Tac Toe in C++” was undertaken at
ARKA JAIN University as part of my BACHELOR OF COMPUTER APPLICATION program.
This project was completed during my second year, forth-semester studies as a mini project. The
primary objective of this project was to design and implement a game of Tic Tac Toe using the C+
+ programming language. It involved extensive coding, debugging, and testing to ensure the game's
functionality and user-friendliness. The project aimed to enhance my programming skills, problem-
solving abilities, and understanding of object-oriented programming concepts. I am grateful to
ARKA JAIN University for providing me with the opportunity and resources to complete this
project successfully. I submit this project report with the hope that it reflects my commitment and
passion for computer science and serves as a testament to my academic progress at ARKA JAIN
University, Jharkhand.
Siddharth Sagar
AJU/221122
v
CONTENTS
LIST OF CONTENTS PAGE NO.
BONAFIDE CERTIFICATE II
ABSTRACT III
ACKNOWLEDGEMENT IV
DECLARATION V
INTRODUCTION 7
USED SPECIFICATION 8
CODE 9-12
OUTPUT 13-14
CONCLUSION 15
REFERENCE 16
INTRODUCTION
Tic Tac Toe, also known as Noughts and Crosses, is a classic two-player game that has been
enjoyed by people of all ages for generations. In this mini-project, we will be implementing a
simple yet engaging version of Tic Tac Toe using the C++ programming language. The
objective of this project is to create a console-based game that allows two players to take
turns, placing their respective symbols (typically 'X' and 'O') on a 3x3 grid until one of them
wins by forming a row, column, or diagonal of their symbols.
Our implementation will showcase fundamental concepts of C++ programming, including
data structures, control flow, and user input handling. The project will feature a user-friendly
interface with clear instructions and error handling to ensure a seamless gaming experience. It
will provide players with the option to start a new game after the current one concludes,
offering continuous entertainment.
Throughout this project, we will explore topics such as arrays or vectors to represent the
game board, functions for game logic, conditional statements to determine the winner, and
loops for controlling the flow of the game. Additionally, we will handle various user inputs to
validate moves, preventing illegal moves and guiding players to make valid ones.
By the end of this mini-project, you will have a functional and interactive Tic Tac Toe game
that demonstrates your proficiency in C++ programming and serves as a foundation for more
complex game development projects. So, let's dive in and embark on this exciting journey to
create a classic game brought to life through code!
7
USED SPECIFICATION
1. USED HARDWARE LIST
HARDWARE SPECIFICATION
COMPONENT
Processor AMD RYZEN 3
RAM 8GB
SSD 512GB
2. USED SOFTWARE LIST
SOFTWARE VERSION
Operating System Windows 11
Software development kit VS Code 1.89
Programming language C++ 20
8
CODE
#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')
9
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";
10
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;
}
11
/*******************************************************************
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
********************************************************************/
12
OUTPUT
13
14
CONCLUSION
The “Tic Tac Toe in C++” game is most familiar among all the age groups. Intelligence can
be a property of any purpose-driven decision maker. This basic idea has been suggested many
times. An algorithm of playing “Tic Tac Toe Using C++” has been presented and tested that
works in efficient way. Creating this game was a fun and rewarding experience. It allowed me
to showcase my creativity and problem-solving skills. I faced challenges along the way, but
with perseverance, I was able to overcome them. Overall, I'm proud of the final product and
the effort I put into making it enjoyable for players.
15
REFERENCE
[1] Booch, Grady, "Object-Oriented Analysis and Design with Applications", The
Benjamin/Cummings Publishing Company, Inc., 1994.
[2] Deitel, H. M., Deitel, P. J., "C++ How To Program", Prentice-Hall, Inc., 1994.
[3] Pohl, Ira, "Object-Oriented Programming Using C++", The Benjamin/Cummings
Publishing Company, Inc., 1993.
[4] Sommerville, Ian, "Software Engineering", Addison-Wesley Publishing Company Inc.,
1992.
[5] White, Iseult, "Using the Booch Method", The Benjamin/Cummings Publishing
Company, Inc., 1994.
[6] E. Balagurusamy, “Object Oriented Programming with C++”, Tata McGraw Hill, 2021.
[7] E. Balaguruswamy, “Object Oriented Analysis and Design”, Tata McGraw Hill, 2008.
[8] K R Venugopal, Rajkumar Buyya, T Ravishankar, “Mastering C++”, Tata McGrawHill,
2017.
[9] Cooper, S., Wanda, D., Pausch, R., Alice: “3-D tool for introductory programming
concepts”, The Journal of Computing in Small Colleges, 15, (5), 107--116, 2000.
[10] Dawson, M., “Beginning C++ Game Programming”, Boston, MA: Thomson Course
Technology, 2004.
[11] Rucker, R., “ Software Engineering and Computer Games”, New York, NY: Addison-
Wesley, 2002.
[12] Watt, A., Policarpo, F., “3D Games: Real-time Rendering and Software Technology”,
NewYork, NY: Addison-Wesley, 2001.
16