/*
############ Project Name : TIC TAC TOE
############ CODED BY
############ > Faysal Ishtiaq Rabby
############ ~ Student ID : 120909
############ Khulna University, Bangladesh.
*/
#include "stdafx.h"
#include<stdlib.h>
int main()
/* Declaring Variables */
char gamestart;
char pos_input[]={'1','2','3','4','5','6','7','8','9'};
int gameloop, option_position,input,flag=0,player_number,rand_int,i;
/* Variable Declaration Finished*/
printf("Press \"ENTER\" to start.");
scanf("%c",&gamestart);
while(gamestart == '\n')
{
/* Re-defining Variables..... */
for(i=0; i<9; i++)
pos_input[i]= (char) i+49;
flag = 0;
printf("\n\n\n\n Choose number of players \n\n maximum player : 2 \n\n minimum player : 1 \n\n to exit, input 0 \n\n Input: ");
scanf("%d",&player_number);
if(player_number == 0)
exit(1);
else if(player_number == 2)
{
/* prints game name */
printf("***************TIC TAC TOE***************\n\n\n\n");
/*prints game status */
printf("\n\n\n\n*************GAME STATUS***************\n\n");
for(option_position=0; option_position<9; option_position++)
{
/* prints new line after printing 3 position in a row */
if(option_position==0||option_position==3|| option_position==6)
printf("\n\n");
/* prints position value */
printf(" %c",pos_input[option_position]);
}
for(gameloop=0; gameloop<9; gameloop++)
{
/* Takes Input from Player 1 for the 1st, 3rd, 5th, 7th and 9th time, the loop is running*/
if(gameloop==0||gameloop==2||gameloop==4||gameloop==6||gameloop==8)
{
printf("\n\n\nPlayer 1 ( enter position value. your input is shown as \'O\' ) :");
/*Taking Input from Player 1*/
scanf("%d",&input);
/* Assigning new value to desired position */
pos_input[input-1]='O';
printf("\n\n\n\n*************GAME STATUS***************\n\n");
for(option_position=0; option_position<9; option_position++)
{
/* prints new line after printing 3 position in a row */
if(option_position==0||option_position==3|| option_position==6)
printf("\n\n");
/* prints position value */
printf(" %c",pos_input[option_position]);
}
/*******************************************************************************************
################################## DECIDING RESULT #########################################
********************************************************************************************
######################## Checks for three same input in a line #############################
################### If there, Player 1 is the WINNER !!!!!!!!!!!!! #########################
*******************************************************************************************/
if(pos_input[0]=='O'&&pos_input[1]=='O'&&pos_input[2]=='O')
{
printf("\n\nPlayer 1 wins!!!!!");
flag++;
break;
}
else if(pos_input[0]=='O'&&pos_input[3]=='O'&&pos_input[6]=='O')
{
printf("\n\nPlayer 1 wins!!!!!");
flag++;
break;
}
else if(pos_input[0]=='O'&&pos_input[4]=='O'&&pos_input[8]=='O')
{
printf("\n\nPlayer 1 wins!!!!!");
flag++;
break;
}
else if(pos_input[1]=='O'&&pos_input[4]=='O'&&pos_input[7]=='O')
{
printf("\n\nPlayer 1 wins!!!!!");
flag++;
break;
}
else if(pos_input[2]=='O'&&pos_input[5]=='O'&&pos_input[8]=='O')
{
printf("\n\nPlayer 1 wins!!!!!");
flag++;
break;
}
else if(pos_input[2]=='O'&&pos_input[4]=='O'&&pos_input[6]=='O')
{
printf("\n\nPlayer 1 wins!!!!!");
flag++;
break;
}
else if(pos_input[3]=='O'&&pos_input[4]=='O'&&pos_input[5]=='O')
{
printf("\n\nPlayer 1 wins!!!!!");
flag++;
break;
}
else if(pos_input[6]=='O'&&pos_input[7]=='O'&&pos_input[8]=='O')
{
printf("\n\nPlayer 1 wins!!!!!");
flag++;
break;
}
}
/* Takes Input from Player 2 for the 2nd, 4th, 6th and 8th time, the loop is running*/
else
{
printf("\n\n\nPlayer 2 ( enter position value. your input is shown as \'X\' ) :");
/*Taking Input from Player 1*/
scanf("%d",&input);
/* Assigning new value to desired position */
pos_input[input-1]='X';
printf("\n\n\n\n*************GAME STATUS***************\n\n");
for(option_position=0; option_position<9; option_position++)
{
/* prints new line after printing 3 position in a row */
if(option_position==0||option_position==3|| option_position==6)
printf("\n\n");
/* prints position value */
printf(" %c",pos_input[option_position]);
}
/*******************************************************************************************
################################## DECIDING RESULT #########################################
********************************************************************************************
######################## Checks for three same input in a line #############################
################### If there, Player 2 is the WINNER !!!!!!!!!!!!! #########################
*******************************************************************************************/
if(pos_input[0]=='X'&&pos_input[1]=='X'&&pos_input[2]=='X')
{
printf("\n\nPlayer 2 wins!!!!!");
flag++;
break;
}
else if(pos_input[0]=='X'&&pos_input[3]=='X'&&pos_input[6]=='X')
{
printf("\n\nPlayer 2 wins!!!!!");
flag++;
break;
}
else if(pos_input[0]=='X'&&pos_input[4]=='X'&&pos_input[8]=='X')
{
printf("\n\nPlayer 2 wins!!!!!");
flag++;
break;
}
else if(pos_input[1]=='X'&&pos_input[4]=='X'&&pos_input[7]=='X')
{
printf("\n\nPlayer 2 wins!!!!!");
flag++;
break;
}
else if(pos_input[2]=='X'&&pos_input[5]=='X'&&pos_input[8]=='X')
{
printf("\n\nPlayer 2 wins!!!!!");
flag++;
break;
}
else if(pos_input[2]=='X'&&pos_input[4]=='X'&&pos_input[6]=='X')
{
printf("\n\nPlayer 2 wins!!!!!");
flag++;
break;
}
else if(pos_input[3]=='X'&&pos_input[4]=='X'&&pos_input[5]=='X')
{
printf("\n\nPlayer 2 wins!!!!!");
flag++;
break;
}
else if(pos_input[6]=='X'&&pos_input[7]=='X'&&pos_input[8]=='X')
{
printf("\n\nPlayer 2 wins!!!!!");
flag++;
break;
}
}
}
}
else if(player_number == 1)
{
gameloop = 0;
/* prints game name */
printf("\n\n***************TIC TAC TOE***************\n\n\n\n");
/* prints game status */
printf("\n\n\n\n*************GAME STATUS***************\n\n");
for(option_position=0; option_position<9; option_position++)
{
/* prints new line after printing 3 position in a row */
if(option_position==0||option_position==3|| option_position==6)
printf("\n\n");
/* prints position value */
printf(" %c",pos_input[option_position]);
}
for(gameloop=0; gameloop<9; gameloop++)
{
if(gameloop==0||gameloop==2||gameloop==4||gameloop==6||gameloop==8)
{
printf("\n\n\nPlayer 1 ( enter position value. your input is shown as \'O\' ) :");
/*Taking Input from Player 1*/
scanf("%d",&input);
/* Assigning new value to desired position */
pos_input[input-1]='O';
printf("\n\n\n\n*************GAME STATUS***************\n\n");
for(option_position=0; option_position<9; option_position++)
{
/* prints new line after printing 3 position in a row */
if(option_position==0||option_position==3|| option_position==6)
printf("\n\n");
/* prints position value */
printf(" %c",pos_input[option_position]);
}
/*******************************************************************************************
################################## DECIDING RESULT #########################################
********************************************************************************************
######################## Checks for three same input in a line #############################
################### If there, Player 1 is the WINNER !!!!!!!!!!!!! #########################
*******************************************************************************************/
if(pos_input[0]=='O'&&pos_input[1]=='O'&&pos_input[2]=='O')
{
printf("\n\nPlayer 1 wins!!!!!");
flag++;
break;
}
else if(pos_input[0]=='O'&&pos_input[3]=='O'&&pos_input[6]=='O')
{
printf("\n\nPlayer 1 wins!!!!!");
flag++;
break;
}
else if(pos_input[0]=='O'&&pos_input[4]=='O'&&pos_input[8]=='O')
{
printf("\n\nPlayer 1 wins!!!!!");
flag++;
break;
}
else if(pos_input[1]=='O'&&pos_input[4]=='O'&&pos_input[7]=='O')
{
printf("\n\nPlayer 1 wins!!!!!");
flag++;
break;
}
else if(pos_input[2]=='O'&&pos_input[5]=='O'&&pos_input[8]=='O')
{
printf("\n\nPlayer 1 wins!!!!!");
flag++;
break;
}
else if(pos_input[2]=='O'&&pos_input[4]=='O'&&pos_input[6]=='O')
{
printf("\n\nPlayer 1 wins!!!!!");
flag++;
break;
}
else if(pos_input[3]=='O'&&pos_input[4]=='O'&&pos_input[5]=='O')
{
printf("\n\nPlayer 1 wins!!!!!");
flag++;
break;
}
else if(pos_input[6]=='O'&&pos_input[7]=='O'&&pos_input[8]=='O')
{
printf("\n\nPlayer 1 wins!!!!!");
flag++;
break;
}
}
else
{
printf("\n\n\nComputer ( enter position value. your input is shown as \'X\' ) :");
/*********************************************************************************
**********************************************************************************
########################### ARTIFICIAL INTELLIGENCE ##############################
**********************************************************************************
**********************************************************************************/
if((pos_input[0] =='X' && pos_input[1] =='X') && pos_input[2] != 'O')
{
pos_input[2]='X';
}
else if((pos_input[1] =='X' && pos_input[2] =='X') && pos_input[0] != 'O')
{
pos_input[0]='X';
}
else if((pos_input[0] =='X' && pos_input[2] =='X') && pos_input[1] != 'O')
{
pos_input[1]='X';
}
else if((pos_input[3] =='X' && pos_input[4] =='X') && pos_input[5] != 'O')
{
pos_input[5]='X';
}
else if((pos_input[4] =='X' && pos_input[5] =='X') && pos_input[3] != 'O')
{
pos_input[3]='X';
}
else if((pos_input[3] =='X' && pos_input[5] =='X') && pos_input[4] != 'O')
{
pos_input[4]='X';
}
else if((pos_input[6] =='X' && pos_input[7] =='X') && pos_input[8] != 'O')
{
pos_input[8]='X';
}
else if((pos_input[6] =='X' && pos_input[8] =='X') && pos_input[7] != 'O')
{
pos_input[2]='X';
}
else if((pos_input[7] =='X' && pos_input[8] =='X') && pos_input[6] != 'O')
{
pos_input[6]='X';
}
else if((pos_input[0] =='X' && pos_input[3] =='X') && pos_input[6] != 'O')
{
pos_input[6]='X';
}
else if((pos_input[0] =='X' && pos_input[6] =='X') && pos_input[3] != 'O')
{
pos_input[3]='X';
}
else if((pos_input[3] =='X' && pos_input[6] =='X') && pos_input[0] != 'O')
{
pos_input[0]='X';
}
else if((pos_input[1] =='X' && pos_input[4] =='X') && pos_input[7] != 'O')
{
pos_input[7]='X';
}
else if((pos_input[1] =='X' && pos_input[7] =='X') && pos_input[4] != 'O')
{
pos_input[4]='X';
}
else if((pos_input[4] =='X' && pos_input[7] =='X') && pos_input[1] != 'O')
{
pos_input[1]='X';
}
else if((pos_input[2] =='X' && pos_input[5] =='X') && pos_input[8] != 'O')
{
pos_input[8]='X';
}
else if((pos_input[2] =='X' && pos_input[8] =='X') && pos_input[5] != 'O')
{
pos_input[5]='X';
}
else if((pos_input[5] =='X' && pos_input[8] =='X') && pos_input[2] != 'O')
{
pos_input[2]='X';
}
else if((pos_input[0] =='X' && pos_input[4] =='X') && pos_input[8] != 'O')
{
pos_input[8]='X';
}
else if((pos_input[0] =='X' && pos_input[8] =='X') && pos_input[4] != 'O')
{
pos_input[4]='X';
}
else if((pos_input[4] =='X' && pos_input[8] =='X') && pos_input[0] != 'O')
{
pos_input[0]='X';
}
else if((pos_input[2] =='X' && pos_input[4] =='X') && pos_input[6] != 'O')
{
pos_input[6]='X';
}
else if((pos_input[2] =='X' && pos_input[6] =='X') && pos_input[4] != 'O')
{
pos_input[4]='X';
}
else if((pos_input[4] =='X' && pos_input[6] =='X') && pos_input[2] != 'O')
{
pos_input[2]='X';
}
else if((pos_input[0]== 'O' && pos_input[1] == 'O') && pos_input[2] != 'X')
{
pos_input[2] = 'X';
}
else if((pos_input[1] =='O' && pos_input[2] =='O') && pos_input[0] != 'X')
{
pos_input[0] ='X';
}
else if((pos_input[0] =='O' && pos_input[2] =='O') && pos_input[1] != 'X')
{
pos_input[1]= 'X';
}
else if((pos_input[0] =='O' && pos_input[4] =='O') && pos_input[8] != 'X')
{
pos_input[8]='X';
}
else if((pos_input[0] =='O' && pos_input[8] =='O') && pos_input[4] != 'X')
{
pos_input[4]='X';
}
else if((pos_input[4] =='O' && pos_input[8] =='O') && pos_input[0] != 'X')
{
pos_input[0]='X';
}
else if((pos_input[0] =='O' && pos_input[3] =='O') && pos_input[6] != 'X')
{
pos_input[6]='X';
}
else if((pos_input[0] =='O' && pos_input[6] =='O') && pos_input[3] != 'X')
{
pos_input[3]='X';
}
else if((pos_input[3] =='O' && pos_input[6] =='O') && pos_input[0] != 'X')
{
pos_input[0]='X';
}
else if((pos_input[3] =='O' && pos_input[4] =='O') && pos_input[5] != 'X')
{
pos_input[5]='X';
}
else if((pos_input[3] =='O' && pos_input[5] =='O') && pos_input[4] != 'X')
{
pos_input[4]='X';
}
else if((pos_input[4] =='O' && pos_input[5] =='O') && pos_input[3] != 'X')
{
pos_input[3]='X';
}
else if((pos_input[6] =='O' && pos_input[7] =='O') && pos_input[8] != 'X')
{
pos_input[8]='X';
}
else if((pos_input[6] =='O' && pos_input[8] =='O') && pos_input[7] != 'X')
{
pos_input[7]='X';
}
else if((pos_input[7] =='O' && pos_input[8] =='O') && pos_input[6] != 'X')
{
pos_input[6]='X';
}
else if((pos_input[0] =='O' && pos_input[4] =='O') && pos_input[8] != 'X')
{
pos_input[8]='X';
}
else if((pos_input[1] =='O' && pos_input[4] =='O') && pos_input[7] != 'X')
{
pos_input[7]='X';
}
else if((pos_input[1] =='O' && pos_input[7] =='O') && pos_input[4] != 'X')
{
pos_input[4]='X';
}
else if((pos_input[7] =='O' && pos_input[4] =='O') && pos_input[1] != 'X')
{
pos_input[1]='X';
}
else if((pos_input[2] =='O' && pos_input[5] =='O') && pos_input[8] != 'X')
{
pos_input[8]='X';
}
else if((pos_input[2] =='O' && pos_input[8] =='O') && pos_input[5] != 'X')
{
pos_input[5]='X';
}
else if((pos_input[5] =='O' && pos_input[8] =='O') && pos_input[5] != 'X')
{
pos_input[5]='X';
}
else if((pos_input[2] =='O' && pos_input[4] =='O') && pos_input[6] != 'X')
{
pos_input[6]='X';
}
else if((pos_input[2] =='O' && pos_input[6] =='O') && pos_input[4] != 'X')
{
pos_input[4]='X';
}
else if((pos_input[6] =='O' && pos_input[4] =='O') && pos_input[2] != 'X')
{
pos_input[2]='X';
}
else if(pos_input[5] == 'O' && pos_input[2] != 'O' && pos_input[2] != 'X')
{
pos_input[2]='X';
}
else if(pos_input[3] == 'O' && pos_input[0] != 'O' && pos_input[0] != 'X')
{
pos_input[0]='X';
}
else
{
if(pos_input[4] == 'O')
{
A:
/* Generates integers between 0 to 8 , including them. */
rand_int = rand()%9;
/* Verifies, whether acceptable or not. */
if(rand_int != 1 && rand_int != 3 && rand_int != 5 && rand_int != 7
&& pos_input[rand_int] != 'X' && pos_input[rand_int] != 'O')
pos_input[rand_int] = 'X';
else
goto A;
}
else
{
if(pos_input[4] != 'X')
pos_input[4] = 'X';
else
{
B:
/* Generates integers between 0 to 8 , including 0 & 8. */
rand_int = rand()%9;
/* Verifies, whether acceptable or not. */
if((rand_int == 1 || rand_int == 3 || rand_int == 5 || rand_int == 7)
&& pos_input[rand_int] != 'X' && pos_input[rand_int] != 'O')
pos_input[rand_int] = 'X';
else
goto B;
}
}
}
/*******************************************************************************
********************************************************************************
###################### ARTIFICIAL INTELLIGENCE PART FINISHED ###################
********************************************************************************
*******************************************************************************/
printf("\n\n\n\n*************GAME STATUS***************\n\n");
for(option_position=0; option_position<9; option_position++)
{
/* prints new line after printing 3 position in a row */
if(option_position==0||option_position==3|| option_position==6)
printf("\n\n");
/* prints position value */
printf(" %c",pos_input[option_position]);
}
/*******************************************************************************************
################################## DECIDING RESULT #########################################
********************************************************************************************
######################## Checks for three same input in a line #############################
################### If there, Computer is the WINNER !!!!!!!!!!!!! #########################
*******************************************************************************************/
if(pos_input[0]=='X'&&pos_input[1]=='X'&&pos_input[2]=='X')
{
printf("\n\nComputer wins!!!!!");
flag++;
break;
}
else if(pos_input[0]=='X'&&pos_input[3]=='X'&&pos_input[6]=='X')
{
printf("\n\nComputer wins!!!!!");
flag++;
break;
}
else if(pos_input[0]=='X'&&pos_input[4]=='X'&&pos_input[8]=='X')
{
printf("\n\nComputer wins!!!!!");
flag++;
break;
}
else if(pos_input[1]=='X'&&pos_input[4]=='X'&&pos_input[7]=='X')
{
printf("\n\nComputer wins!!!!!");
flag++;
break;
}
else if(pos_input[2]=='X'&&pos_input[5]=='X'&&pos_input[8]=='X')
{
printf("\n\nComputer wins!!!!!");
flag++;
break;
}
else if(pos_input[2]=='X'&&pos_input[4]=='X'&&pos_input[6]=='X')
{
printf("\n\nComputer wins!!!!!");
flag++;
break;
}
else if(pos_input[3]=='X'&&pos_input[4]=='X'&&pos_input[5]=='X')
{
printf("\n\nComputer wins!!!!!");
flag++;
break;
}
else if(pos_input[6]=='X'&&pos_input[7]=='X'&&pos_input[8]=='X')
{
printf("\n\nComputer wins!!!!!");
flag++;
break;
}
}
}
}
else
printf("\n\n\nOnly 1 or 2 player available. \n");
if((player_number == 1 || player_number ==2) && flag== 0)
printf("\n\nDRAW!!!!!!!!!!!!!!!");
printf("\n\n\nThanks for playing......\n\n\n Game re-starting.......\n\n\n Game re-started.");
}
return 0;
}
faysal.ishtiaq_1 0 Newbie Poster
pbj.codez -3 Junior Poster in Training
faysal.ishtiaq_1 0 Newbie Poster
Unimportant 18 Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.