You are on page 1of 32

14th Dec,2020

(Console Game) C++Program

Presented By :Abhishek Kumar


ACKNOWLEDGEMENT

The successful completion of this project mark the beginning of an ever - going
learning experience of converting ideas and concepts into real life, practical
system. This project was a quite a learning experience for me at each and every
step. At the same time it has given me confidence to work in professional setup. I
feel the experience gained during the project will lead me to gain the bright
prospect in the future.
DECLARATION

I, ABHISHEK KUMAR, Student of B.tech (CSE) declare that the project titled “TIC
TAC TOE ” which is submitted by me to Department of computer science and
engineering, B.N. College of engineering & Technology , Lucknow affiliated to
Abdul kalam Technical University.

Date: 15 TH OCT 2020


ABHISHEK KUMAR
INDEX
1. Short introduction ToC++
2. Introduction to Tic Tac Toe (Console Game) C++Program
3. System Requirements
4. Coding
5. Output Result
6. Initialization Of Variables &explanatorystatements
7. Game Loops
8. Bibliography
CERTIFICATE
Short Introduction ToC++
C++ is a general-purpose programming language.
It has imperative, object oriented and generic
programming features, while also providing
facilities for low-level memorymanipulation.

It was designed with a bias toward system programmingand embedded,


resource-constrained and large systems, with performance,
efficiency and flexibility of use as its design highlights. C++
has also been found useful in many other contexts, withkey
strengths being software infrastructure and resource-
constrained applications , including desktop applications, servers (e.g. e-
commerce, web search or SQLservers), and performance-critical
applications (e.g. telephone switches orspace probes).C++ is
a compiled language, with implementations of itavailable on manyplatforms.
Introduction to Tic Tac Toe
(Console Game) C++Program

Tic-tac-toe (or Noughts and crosses, Xs and Os) is a pencil-


and-paper 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 respective marks in a horizontal, vertical,or
diagonal row wins the game.
This program is a game program, Tic Tac
Toe. Most of us have played this game in
our leisure time, and we have make aC+
+ program on it.
Introduction to Tic Tac Toe
(Console Game) C++Program

This game uses board to control players


In each turn players enter a number and
choose a move
Simplify programing assumes that player
one always moves first and uses X's
Player two moves at 2nd position and
uses O's

A Sample Screen Shot of a Game


System Requirements

• Operating system: Windows XP, Vista 7 and 8.

• Processor : Pantium III/1.4 GHzprocessor.

• RAM:512 MB

• Hard Disk:900 MB Spacerequired


Tic Tac Toe
(Console Game) C++Program

How the program structured :

At the time when program start we initialize variables, and we run


the game loop until the game end or players choose to quit
The game consists of three steps

• Display board
• Get players move
• Check for gameend
CODING
# include <iostream.h>
#include <conio.h>

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;
clrscr();
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')
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--;
getch();
}
i=checkwin();
player++;
}
while(i==-1);
board();
if(i==1)
cout<<"==>\aPlayer "<<--player<<" win ";
Else
cout<<"==>\aGame draw";
getch();
return 0;
} /*********************************************
FUNCTION TO RETURN GAME STATUS
1FOR GAME IS OVER WITH RESULT
-1FOR GAME IS IN PROGRESS
O GAME IS OVERAND NORESULT
**********************************************/
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;
}
/*******************************************************************
FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
********************************************************************/
void board()
{
clrscr();
cout << "\n\n\tTic TacToe\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
********************************************************************/
OUTPUT
Initialization Of Variables&
explanatory statements

This portion of code is for initialization of variables,


the variables of squares are initializing with the
characters from 1to 9.
The player turn will be initializing to 1because since
the player 1makes the first turn
Game over is initialize to true but that does not really
matter for this program because after game loop
game check itself for winner.
Initialization Of Variables&
explanatory statements

Program comments are explanatory statements that we can include in the C++ code
that we write and helps anyone reading its source code. All programming languages
allow for some form of comments.
C++ supports single line and multi-line comments. All characters available inside any
comment are ignored by C++compiler.
C++ comments start with /* and end with */. For example:

// This is acomment

/* C++ comments can also


span multiple lines
*/
Game Loops

After initialization game began to move forward for main game


loop these loops are while and do while loop which are
encapsulated in statements that what to do or not to do.
Once we enter the game loop
The first thing will be done is print
the game board which displays the
tic tac toe game board in console
window
Remember we initialize these
squares with characters from 1to 9
for basic console input and output.
Game Loops

!!Notice that the console window


prompts the player for move
The player's moves are handledby

!!
the next portion of code

When we run the program the board looks like this


Game Loops
( player's moves)

cPlayerMark determines that first player has X and second has 0


This portion of statements check for player turn if it's not the first
player move its promoted the move to next player
Then the next line gets the valid move of the player
Game Loops
( player's moves)

If players input an invalid move it's prompted for another


move and says try again like this ..

A Screen Shot ofan Invalid Move


Game Loops
( player's moves)

The cin statement gets the valid


move for theplayer
Notice that it's begin with another
loop it has pretty much statements to
check the conditions.

The check forvalid move is pretty


large branch of square check.
Game Loops
( player's moves)

Each branch of the if statement makes two check, the first input
check that the input is valid digit from 1to 9 and second check is for
make sure of the input is digit not an character, second check also
make sure that the number which is entered not entered
Previously
Game Loops
( player's moves)

After the valid move the series of checks perform to check


the games conditions.

Note there are the nine ways to end the game, 8 conditions
to win the game and 1condition for draw the game.

The first conditions check the ending game condition


through
the walls of 1st square.
Game Loops
( player's moves)

The second if statement handles the 4 cases from


themiddle 5th square .
Game Loops
( player's moves)

The third if statement handles the 2 cases from the 9th square

In each of these cases we check that the squares not equals to its number
character.
This check ensures that we have an extra O and the other two checks make
sure that the other two squares have the same O in the series like this one
Those cases cover the win condition however game will be ended and draw like
this.
Game Loops
( player's moves)

Screen Shot “ Game Draw”


Game Loops
(Final Check)

After that we print the ending board of the game and tells
whose win and ask the user to play another game or quit
If the user want play again the looping is continue and the
board will be reset and the player turn also set back to 1

On the other hand the game will be quit .


Game Loops
(Final Check)

The final check takes cover of the case that game will be draw
and all the squares will be marked.
When we determine that the game is over we run through over
final condition
If the game is over we check for the game that someone is win
the game Boolean which we set at the last part, if some has
won then it will be last one or last player which is moved.
BIBLIOGRAPHY

•Help from internet


2007 Xoax[xoax.net ( C++Lesson # 9)]
http://www.cppforschool.com
http://www.geeksforgeeks.org

•Help from book


Introduction to C++(sumita arora)

•Help from friends

You might also like