Tic Tac Toe Game using Array
Program Name – Computer
Engineering
Program Code – CO-3-I
Course Name – Date
Structure Using C
Course Code – 22317
Project Title
“Tic Tac Toe Game using
Array”
Academic Year 2018-19
|
Acknowledgment
We
express our sincere gratitude to our collage Government polytechnic, Dhule for
providing us an opportunity to undertake and complete such an interesting
project report.
We
are very thankful to our respected faculty Mr. A. S. Ahmad for the confidence
she had on us regarding this project. We are very much obliged to our respected
principal, Shri. R. G. Wadekar for inspiring and motivating us to bring out a
successful project.
We
are very grateful to our respected faculty Mr. A. S. Ahmad who was our backbone
and our guide throughout this project. Without her assistance we couldn’t have
completed this project within a short period of a time.
It
would be really very unfair without mention of our friends and families for the
immense love and moral support they have given is truly immeasurable.
Abstraction
The project
is basically game which is Tic Tac Toe. This game contains ‘x’ and ‘o’. It is a
two player game. The first player plays as ‘x’ and another as ‘o’. The first
one to make a line is winner. So the program is designed in a way that is easy
to understand by the user.
The program contains various functions
like checkwin(), board(),etc. The detailed information of this functions is
given below but the basic idea about them is in their name. So the function
checkwin() checks whether the game is completed i.e. won by someone or drawn.
And the board() function is used to display the board or the play area of this
game. This program is in c code. We have used array to store the data of ‘x’
and ‘o’ of the players as we have to use it as a part of the project.
Explanation of Functions used in the program:
1.
Checkwin():
This is a function which checks whether the game is completed
or is still running or it checks the array elements and determines the winner
by checking whether the ‘X’ or ‘O’ are In lines.
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;
}
In
the above code the conditions are checked like: if (square[1] == square[2] &&
square[2] == square[3]). Thus if the element in the array element square[1]
will be equal to square[2] and the element in the array element square[2] will
be equal to square[3] thus a line will be formed and the game is won by the
particular player so it returns 1. Similarly the other conditions are checked
and the last condition checks whether the game is completed or remaining.
1.
Board()
This
is a function to display the board or the play area for the player. The code of
this function is as follows:
void
board()
{
clrscr();
printf("\n\n\t\t\t\tTic Tac
Toe\n\n");
printf("\t\t\tPlayer 1 (X) -
Player 2 (O)\n\n\n");
printf("\t\t\t\t |
| \n");
printf("\t\t\t\t %c
| %c | %c
\n", square[1], square[2], square[3]);
printf("\t\t\t\t_____|_____|_____\n");
printf("\t\t\t\t |
| \n");
printf("\t\t\t\t %c
| %c | %c
\n", square[4], square[5], square[6]);
printf("\t\t\t\t_____|_____|_____\n");
printf("\t\t\t\t |
| \n");
printf("\t\t\t\t %c
| %c | %c
\n", square[7], square[8], square[9]);
printf("\t\t\t\t |
| \n\n");
}
This code
displays the play area and the characters inside the play area. Numbers from 1
to 9 are displayed in this function. The data is modified when we call the
function i.e. the number will be changed according to the inputs given by the
user.
2.
main():-
In the main
body, at the first all variables are declared which will be used in the
program. Then we have used do-while
loop. At the start of the loop the decision of which player is active is done.
Then the value of the block is asked from that particular player. Then the mark
is selected for that i.e. whether it is ‘X’ or ‘O’. Then conditions are checked that which block
is selected by the player and then the value of variable mark is copied to the
array element that resembles the block. If the conditions of the block are
false than it displays that the entered number is invalid. Then the function checkwin() is called and
the result that is returned by the function is stored in the variable. And then
it is checked that whether the game is completed, incomplete or draw. This loop
iterates till the value of the variable is -1 and board function is called
every time to change the board every time the value is correctly inputed.
C code of the project is as follows;-
#include
<stdio.h>
#include
<conio.h>
char
square[10] = { '0', '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;
printf("\t\t\tPlayer %d, enter
a number: ", player);
scanf("%d", &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
{
printf("\t\t\tInvalid move
");
player--;
getch();
}
i = checkwin();
player++;
}while (i == - 1);
board();
if (i == 1)
printf("\t\t\t==>\aPlayer %d
win ", --player);
else
printf("\t\t\t==>\aGame
Over");
getch();
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;
}
void
board()
{
system("cls");
printf("\n\n\t\t\t\tTic Tac
Toe\n\n");
printf("\t\t\tPlayer 1 (X) -
Player 2 (O)\n\n\n");
printf("\t\t\t\t |
| \n");
printf("\t\t\t\t %c
| %c | %c
\n", square[1], square[2], square[3]);
printf("\t\t\t\t_____|_____|_____\n");
printf("\t\t\t\t |
| \n");
printf("\t\t\t\t %c
| %c | %c
\n", square[4], square[5], square[6]);
printf("\t\t\t\t_____|_____|_____\n");
printf("\t\t\t\t |
| \n");
printf("\t\t\t\t %c
| %c | %c
\n", square[7], square[8], square[9]);
printf("\t\t\t\t |
| \n\n");
}
Outputs: