Paavai Vidhyashram Senior Secondary School: Project Report

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

PAAVAI VIDHYASHRAM

SENIOR SECONDARY
SCHOOL

PROJECT REPORT
ON
TIC TAC TOE (THE GAME)
2017-1018

In the partial fulfilment of project


Examination for the session 2017-2018
Conducted by the CENTRAL BOARD OF
SECONDARY EDUCATION
DONE BY,
V.S.VIDYASAGAR.

1|Page
ACKNOWLEDGEMENT
I would like to express my special thanks of gratitude to my
teacher (Mrs.A.Niranjani) as well as our Director(Dr.C.Satish)
and our HOD/Vice-principal(Mr.S.Rohit)who gave me this
golden opportunity to do this wonderful project on the topic(Tic
Tac Toe(the game)),which also helped me in doing a lot of
Research and I came to know about so many new things.
Secondly,I would like to thank my parents and friends who helped
me a lot in finalizing the project and for encouraging me.

2|Page
What is tic tac toe?
Tic-tac-toe (also known as noughts and crosses or Xs and Os) is
a paper-and-pencil game for two players, X and O, who take
turns marking the spaces in a 3×3 grid. The player who succeeds
in placing three of their marks in a horizontal, vertical, or
diagonal row wins the game.
The following example game is won by the first player, X:

Players soon discover that best play from both parties leads to
a draw. Hence, tic-tac-toe is most often played by young children.
Because of the simplicity of tic-tac-toe, it is often used as
a pedagogical tool for teaching the concepts of
good sportsmanship and the branch of artificial intelligence that
deals with the searching of game trees. It is straightforward to
write a computer program to play tic-tac-toe perfectly, to
enumerate the 765 essentially different positions (the state space
complexity), or the 26,830 possible games up to rotations and
reflections (the game tree complexity) on this space.[1]
The game can be generalized to an m,n,k-game in which two
players alternate placing stones of their own color on
an m×n board, with the goal of getting k of their own color in a
row. Tic-tac-toe is the (3,3,3)-game. Harary's generalized tic-tac-
toe is an even broader generalization of tic tac toe. It can also be
generalized as a nd game. Tic-tac-toe is the game where n equals
3 and d equals 2. If played properly, the game will end in a draw
making tic-tac-toe a futile game.[4]

3|Page
S.No
INDEX
1 Abstract
2 Tasks offered
o Aim
o Algorithm
o Flowchart

3 Source code
4 Output
5 Conclusion

4|Page
Abstract
The project is associated with two simple games one of which is
called as “TIC-TAC-TOE” for 2 players, one for „X‟ and other
for „O‟, who takes turns making the spaces in 3x3 grid. The „X‟
player goes first. The player who succeeds in placing their
respective colours in a horizontal, vertical or diagonal rows or
columns wins the game. If the players do not succeed in placing
their respective colours in a horizontal, vertical or diagonal rows
or columns and if there are no further blocks to be filled then the
game is considered to be over!!!

5|Page
Tasks offered
Aim:
In this project we are going to develop a simple C++
program to play the game tic tac toe(also known as Xs and
Os).

Algorithm:
1. Start.
2. Display(overview).
3. Press ‘d’ to start game.
4. Display(3x3 grid).
5. Evaluate Grid.
6. If 3 X’s in horizontal,vertical or diagonal or rows or col.
Print “X wins”.
7. If 3O’s in horizontal, vertical or diagonal or rows or col.
Print “O wins”.
8. If grid filled. Print “Game over”.
9. Stop.

6|Page
Flowchart:

7|Page
Source 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')
8|Page
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);

9|Page
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])

10 | P a g e
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";

11 | P a g e
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;


}

12 | P a g e
Output
Choose a cell numbered from 1 to 9 as below and play

1|2 |3
--------------
4|5 |6
--------------
7|8 |9

- - - - - - - - - -

COMPUTER has put a O in cell 6

| |
--------------
| |O
--------------
| |

HUMAN has put a X in cell 7

13 | P a g e
| |
--------------
| |O
--------------
X| |

COMPUTER has put a O in cell 5

| |
--------------
|O |O
--------------
X| |

HUMAN has put a X in cell 1

X| |
--------------
|O |O
--------------
X| |

COMPUTER has put a O in cell 9

14 | P a g e
X| |
--------------
|O |O
--------------
X| |O

HUMAN has put a X in cell 8

X| |
--------------
|O |O
--------------
X|X |O

COMPUTER has put a O in cell 4

X| |
--------------
O|O |O
--------------
X|X |O

15 | P a g e
COMPUTER has won

16 | P a g e
Conclusion
Our project aimed at achieving the game of “TIC-TAC-TOE”
where in the TIC TAC TOE originally cross marks circles are
used instead we have made use of filling colors into selected
blocks
We can enhance the same game by changing the grid size from
3x3 to 4x4 or higher sizes so that we can increase the level of
difficulty

17 | P a g e

You might also like