Hi this is a tic-tac-toe game you can copy sourcecode here or Download from GitHub.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <windows.h>
int board[10] = {2,2,2,2,2,2,2,2,2,2};
int turn = 1,flag = 0;
int player,comp;
void menu();
void go(int n);
void start_game();
void check_draw();
void draw_board();
void player_first();
void put_X_O(char ch,int pos);
COORD coord={0,0}; // this is global variable
//center of axis is set to the top left cornor of the screen
void gotoxy(int x,int y)
{
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
void main()
{
system("cls");
menu();
getch();
}
void menu()
{
int choice;
system("cls");
printf("\n--------MENU--------");
printf("\n1 : Play with X");
printf("\n2 : Play with O");
printf("\n3 : Exit");
printf("\nEnter your choice:>");
scanf("%d",&choice);
turn = 1;
switch (choice)
{
case 1:
player = 1;
comp = 0;
player_first();
break;
case 2:
player = 0;
comp = 1;
start_game();
break;
case 3:
exit(1);
default:
menu();
}
}
int make2()
{
if(board[5] == 2)
return 5;
if(board[2] == 2)
return 2;
if(board[4] == 2)
return 4;
if(board[6] == 2)
return 6;
if(board[8] == 2)
return 8;
return 0;
}
int make4()
{
if(board[1] == 2)
return 1;
if(board[3] == 2)
return 3;
if(board[7] == 2)
return 7;
if(board[9] == 2)
return 9;
return 0;
}
int posswin(int p)
{
// p==1 then X p==0 then O
int i;
int check_val,pos;
if(p == 1)
check_val = 18;
else
check_val = 50;
i = 1;
while(i<=9)//row check
{
if(board[i] * board[i+1] * board[i+2] == check_val)
{
if(board[i] == 2)
return i;
if(board[i+1] == 2)
return i+1;
if(board[i+2] == 2)
return i+2;
}
i+=3;
}
i = 1;
while(i<=3)//column check
{
if(board[i] * board[i+3] * board[i+6] == check_val)
{
if(board[i] == 2)
return i;
if(board[i+3] == 2)
return i+3;
if(board[i+6] == 2)
return i+6;
}
i++;
}
if(board[1] * board[5] * board[9] == check_val)
{
if(board[1] == 2)
return 1;
if(board[5] == 2)
return 5;
if(board[9] == 2)
return 9;
}
if(board[3] * board[5] * board[7] == check_val)
{
if(board[3] == 2)
return 3;
if(board[5] == 2)
return 5;
if(board[7] == 2)
return 7;
}
return 0;
}
void go(int n)
{
if(turn % 2)
board[n] = 3;
else
board[n] = 5;
turn++;
}
void player_first()
{
int pos;
check_draw();
draw_board();
gotoxy(30,18);
printf("Your Turn :> ");
scanf("%d",&pos);
if(board[pos] != 2)
player_first();
if(pos == posswin(player))
{
go(pos);
draw_board();
gotoxy(30,20);
//textcolor(128+RED);
printf("Player Wins");
getch();
exit(0);
}
go(pos);
draw_board();
start_game();
}
void start_game()
{
// p==1 then X p==0 then O
if(posswin(comp))
{
go(posswin(comp));
flag = 1;
}
else
if(posswin(player))
go(posswin(player));
else
if(make2())
go(make2());
else
go(make4());
draw_board();
if(flag)
{
gotoxy(30,20);
//textcolor(128+RED);
printf("Computer wins");
getch();
}
else
player_first();
}
void check_draw()
{
if(turn > 9)
{
gotoxy(30,20);
//textcolor(128+RED);
printf("Game Draw");
getch();
exit(0);
}
}
void draw_board()
{
int j;
for(j=9;j<17;j++)
{
gotoxy(35,j);
printf("| |");
}
gotoxy(28,11);
printf("-----------------------");
gotoxy(28,14);
printf("-----------------------");
for(j=1;j<10;j++)
{
if(board[j] == 3)
put_X_O('X',j);
else
if(board[j] == 5)
put_X_O('O',j);
}
}
void put_X_O(char ch,int pos)
{
int m;
int x = 31, y = 10;
m = pos;
if(m > 3)
{
while(m > 3)
{
y += 3;
m -= 3;
}
}
if(pos % 3 == 0)
x += 16;
else
{
pos %= 3;
pos--;
while(pos)
{
x+=8;
pos--;
}
}
gotoxy(x,y);
printf("%c",ch);
}
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <windows.h>
int board[10] = {2,2,2,2,2,2,2,2,2,2};
int turn = 1,flag = 0;
int player,comp;
void menu();
void go(int n);
void start_game();
void check_draw();
void draw_board();
void player_first();
void put_X_O(char ch,int pos);
COORD coord={0,0}; // this is global variable
//center of axis is set to the top left cornor of the screen
void gotoxy(int x,int y)
{
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
void main()
{
system("cls");
menu();
getch();
}
void menu()
{
int choice;
system("cls");
printf("\n--------MENU--------");
printf("\n1 : Play with X");
printf("\n2 : Play with O");
printf("\n3 : Exit");
printf("\nEnter your choice:>");
scanf("%d",&choice);
turn = 1;
switch (choice)
{
case 1:
player = 1;
comp = 0;
player_first();
break;
case 2:
player = 0;
comp = 1;
start_game();
break;
case 3:
exit(1);
default:
menu();
}
}
int make2()
{
if(board[5] == 2)
return 5;
if(board[2] == 2)
return 2;
if(board[4] == 2)
return 4;
if(board[6] == 2)
return 6;
if(board[8] == 2)
return 8;
return 0;
}
int make4()
{
if(board[1] == 2)
return 1;
if(board[3] == 2)
return 3;
if(board[7] == 2)
return 7;
if(board[9] == 2)
return 9;
return 0;
}
int posswin(int p)
{
// p==1 then X p==0 then O
int i;
int check_val,pos;
if(p == 1)
check_val = 18;
else
check_val = 50;
i = 1;
while(i<=9)//row check
{
if(board[i] * board[i+1] * board[i+2] == check_val)
{
if(board[i] == 2)
return i;
if(board[i+1] == 2)
return i+1;
if(board[i+2] == 2)
return i+2;
}
i+=3;
}
i = 1;
while(i<=3)//column check
{
if(board[i] * board[i+3] * board[i+6] == check_val)
{
if(board[i] == 2)
return i;
if(board[i+3] == 2)
return i+3;
if(board[i+6] == 2)
return i+6;
}
i++;
}
if(board[1] * board[5] * board[9] == check_val)
{
if(board[1] == 2)
return 1;
if(board[5] == 2)
return 5;
if(board[9] == 2)
return 9;
}
if(board[3] * board[5] * board[7] == check_val)
{
if(board[3] == 2)
return 3;
if(board[5] == 2)
return 5;
if(board[7] == 2)
return 7;
}
return 0;
}
void go(int n)
{
if(turn % 2)
board[n] = 3;
else
board[n] = 5;
turn++;
}
void player_first()
{
int pos;
check_draw();
draw_board();
gotoxy(30,18);
printf("Your Turn :> ");
scanf("%d",&pos);
if(board[pos] != 2)
player_first();
if(pos == posswin(player))
{
go(pos);
draw_board();
gotoxy(30,20);
//textcolor(128+RED);
printf("Player Wins");
getch();
exit(0);
}
go(pos);
draw_board();
start_game();
}
void start_game()
{
// p==1 then X p==0 then O
if(posswin(comp))
{
go(posswin(comp));
flag = 1;
}
else
if(posswin(player))
go(posswin(player));
else
if(make2())
go(make2());
else
go(make4());
draw_board();
if(flag)
{
gotoxy(30,20);
//textcolor(128+RED);
printf("Computer wins");
getch();
}
else
player_first();
}
void check_draw()
{
if(turn > 9)
{
gotoxy(30,20);
//textcolor(128+RED);
printf("Game Draw");
getch();
exit(0);
}
}
void draw_board()
{
int j;
for(j=9;j<17;j++)
{
gotoxy(35,j);
printf("| |");
}
gotoxy(28,11);
printf("-----------------------");
gotoxy(28,14);
printf("-----------------------");
for(j=1;j<10;j++)
{
if(board[j] == 3)
put_X_O('X',j);
else
if(board[j] == 5)
put_X_O('O',j);
}
}
void put_X_O(char ch,int pos)
{
int m;
int x = 31, y = 10;
m = pos;
if(m > 3)
{
while(m > 3)
{
y += 3;
m -= 3;
}
}
if(pos % 3 == 0)
x += 16;
else
{
pos %= 3;
pos--;
while(pos)
{
x+=8;
pos--;
}
}
gotoxy(x,y);
printf("%c",ch);
}
i like your site
ReplyDeleteI wish i could see your site. if you know what i mean.
Delete;) ;)
its nice
ReplyDeleteJanu meri jaan, meh tere kurbaan.
DeleteJanu meri jaa, meh tere kurbaan, meh tera tuh meri jaani saara hindustan.
Meh tera tuh meri jane sara hindustan.
<3 <3
Thanks
ReplyDeleteIhen i copy and paste the code it says:
ReplyDelete'::main' must return 'int'|
In function 'int posswin(int)':|
warning: unused variable 'pos'|
||=== Build finished: 1 errors, 1 warnings ===|
instead of "main" try using "int"...i had that same error when i created a Tic Tac Toe for windows Console...
Deletehope it works :D
http://codereview.stackexchange.com/questions/41304/4%C3%974-tic-tac-toe-in-c
Deletedis s much better just change 4 to 3 and 16 to 9
alok
hey can u help me wid the modification of the code??
ReplyDelete*I need to add a scoreboard that records the score of the computer and the player.
Please mail me at adarsh2811@gmail.com
hey..do u have a simple code mini project like the sale program?
ReplyDeletetq..xD
what is the function of global variable (coord)
ReplyDeleteand function "SetConsoleCursorPosition"........???????
coord is a structure. SetConsoleCursorPosition set the cursor on the screen.
Deleteok...but can't we use function "gotoxy()" instead of using SetConsoleCursorPosition .....?
Deletegotoxy() in not builtin function, you can use SetConsoleCursorPosition instead of gotoxy() but before it you have to assign the Coord.x and Coord.y
Deleteok :)
Deletethanx alot for helping me :)
I want to discuss it in detail....will you help me..?
ReplyDeleteMuhammad Wajeeh: Thanks yar. I need this code very very badly.
ReplyDeleteThank You Very Much
i converted it to C++ but there is some problem with the draw_board(), it enters into an infinite loop i suppose
ReplyDeleteI tried it out in linux platform and it gives me an error message as windows.h not found. Can I run this program only in turbo C++?
ReplyDeleteSorry you can't run it in linux platform.
Deletei need help in linux .. will you help me @Shrivalli
Deletesir?? is it for Visual C++? please reply me at my email add! chentzshockwaves@gmail.com thank you so much sir! its for our project.. :D
ReplyDeletethic progrm can we run only in turbo c++ ? can i run it in turbo c?? because when i run the program it say diclaration error the couse are "COORD coord={0,0};// this global variabal " and "#includewindow.h? pless help mee!!
ReplyDeleteThis program run in Code::Blocks IDE with MinGW compiler so it is better to download code::blocks and run it.
ReplyDeleteIt doesn't work. Here's the error: http://bpaste.net/show/C9HiO73ed6iHYoPLHKRL/
ReplyDeletehey man can i get some help.. email me at hackergeorge8@hotmail.com
ReplyDeleteis this program can run on turbo c ???..
ReplyDeleteHelo i am new to programming......
ReplyDeletecan i get the explanition of ds source code....
sir i want know the working process of this code .... can u help me .......
ReplyDeletethis program is not running in turboo c. so plz help me to run this program
ReplyDeletethis code is working if you change line 30 "void" to "int".
ReplyDeleteyour code does not even compile on ideone...why do you write such crappy and useless codes..go and get a life instead...
ReplyDeleteno indentation - nothing!!!
You look like a noob...learn some decent 'hello world' codes first instead of practicing gtk...
hey headfucked asshole ........ first compile the code asshole.....go play tic tac toe. or fuck tac toe.
ReplyDeletei need the report for this project
ReplyDeleteI get an error:
ReplyDelete[Error] '::main' must return 'int'
please help me
change void main to int main
DeleteThank you so much,
ReplyDeleteThis is the only working TIC TAC TOE code online with least possible fixes.
Just changed the "void main()" to "int main" and all works well.
You have my regards
Thank you.
I Need function for windows.h for this tic tac toe can anyone help me. please thepapex5360@gmail.com
ReplyDeleteIF ANYBODY HAVE MINI PROJECTS ON C AND C++ PLS SEND THIS MAIL
ReplyDeletemuthyala.nagaraju7@gmail.com
I want playe against other player..what i must change?
ReplyDeletei have this game in simple code made by me this code is very difficult i made it very clear and easy.......
ReplyDeleteTHEN UPLOAD IT FOR ALL.
DeleteTHANKS
can u please publish it
DeleteCan i get the explanation for this source code?
ReplyDelete