Вы находитесь на странице: 1из 12

TIC TAC TOE

A PROJECT REPORT

Submitted by

SANJHEY HARIRAM SA.


1914163

in partial fulfillment of the requirement


for the award of the degree

of

BACHELOR OF ENGINEERING
in

COMPUTER SCIENCE AND ENGINEERING

K.S. RANGASAMY COLLEGE OF TECHNOLOGY


(An Autonomous Institution, affiliated to Anna University Chennai and Approved by AICTE, New Delhi)

TIRUCHENGODE – 637 215

03 MAY 2020
ABSTRACT

This study exhibits the application of the concept of matrices, probability and
optimization in making an electronic Tic-Tac-Toe game. For a finite number of
moves in every single game of Tic-Tac-Toe, the moves are recorded in a 3×3
matrix and the subsequent solution, or a winning combination, is presented from
the data obtained by playing the electronic game. The concept of matrices and
probability is applied in this game to make the foundation of the logic for this
game. The most productive position for placing an `X' or `O' is found out using
probabilities. Also the most effective blocking move is found out through which
a player placing `O' can block `X' from winning. The skills help in
understanding what strategy can be implemented to be on the winning side. The
study is developed with an attempt to realistically model a tic-tac-toe game, and
help in reflecting major tendencies. This knowledge helps in understanding
what strategy to implement to be on the winning side.

ALGORITHM:
 STEP 1- START GAME
 STEP 2- Decide Who is First and Second
 STEP 3- Take turn Player-1 is X whereas Player-2 is O
 STEP 4- After 9 moves there will be a result
 STEP 5- CASE 1: PLAYER 1 is WINNER…Congrats!!
CASE 2: Match draw…Best of luck for both
CASE 3: PLAYER 2 is WINNER…Congrats!!

FLOWCHAT:

START GAME
Decide who goes

First and second

Take turns going Player-1 is


X whereas Player-2 is O

After 9 moves there will be


a result

PLAYER 1 is WINNER.. Match draw…Best of luck PLAYER 2 is WINNER…


Congrats!! for both Congrats!!

PROGRAM FOR TIC TAC TOE:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
int main() {
int winner = 0, count = 0;
int data[9], index, sign, player, flag, a, b;
for (a = 0; a < 9; a++)
data[a] = ' ';

while (count < 9) {


flag = 0;
system("clear");
printf("\n\n");
printf("\t\t\t %c | %c | %c \n", data[0], data[1], data[2]);
printf("\t\t\t----+----+----\n");
printf("\t\t\t %c | %c | %c \n", data[3], data[4], data[5]);
printf("\t\t\t----+----+---\n");
printf("\t\t\t %c | %c | %c \n", data[6], data[7], data[8]);
if (count % 2 == 0) {
sign = 'X';
player = 1;
}
else {
sign = 'Y';
player = 2;
}
printf("Move for player%d(1-9):", player);
scanf("%d", &index);
if (index < 1 || index > 9) {
printf("Allowed index is 1 to 9!!\n");
continue;
}
if (data[index - 1] == 'X' || data[index - 1] == 'Y') {
printf("Position Already occupied!!\n");
Sleep(1);
continue;
}
data[index - 1] = sign;
count++;

for (a = 0; a < 9; a++) {


if (a % 3 == 0)
flag = 0;

if (data[a] == sign)
flag++;

if (flag == 3) {
winner = 1;
goto win;
}
}

flag = 0;
for (a = 0; a < 3; a++) {
for (b = a; b <= a + 6; b = b + 3) {
if (data[b] == sign)
flag++;
}
if (flag == 3) {
winner = 1;
goto win;
}
flag = 0;
}
if ((data[0] == sign && data[4] == sign && data[8] == sign) ||
(data[2] == sign && data[4] == sign && data[6] == sign)) {
winner = 1;
goto win;
}
}
win:
system("clear");
printf("\n\n");
printf("\t\t\t %c | %c | %c \n", data[0], data[1], data[2]);
printf("\t\t\t----+----+----\n");
printf("\t\t\t %c | %c | %c \n", data[3], data[4], data[5]);
printf("\t\t\t----+----+---\n");
printf("\t\t\t %c | %c | %c \n\n", data[6], data[7], data[8]);
if (winner) {
printf("Player %d is the winner. Congrats!!\n", player);
}
else {
printf("Match draw.. Best of luck for both\n");
}
return 0;
}

OUTPUT:

Вам также может понравиться