Add to Favorites    Make Home Page 198 Online  

 Language Categories  
 Our Services  

Home » C Home » Games and Graphics Home » Simple Tic Tac Toe Game

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Simple Tic Tac Toe Game
Author Rahul Roy
Author Email rahul_nit06 [at] rediffmail.com
Description This is a simple tic tac toe program built without using graphics.
Category C » Games and Graphics
Hits 99627
Code Select and Copy the Code
#include<stdio.h> #include<conio.h> void Board(); void PlayerX(); void PlayerO(); void Player_win(); void check(); int win=0,wrong_X=0,wrong_O=0,chk=0; char name_X[30]; char name_O[30]; int pos_for_X[3][3]; int pos_for_O[3][3]; int pos_marked[3][3]; void main() { int i,ch,j; char ans; /* clrscr(); printf("\n\t\t\t\tTIC TAC TOE"); printf("\n\t\t\t\t"); for(i=1;i<=11;i++) { delay(10000); printf("*"); }*/ do { clrscr(); printf("\n\t\t\t\tTIC TAC TOE"); printf("\n\t\t\t\t"); for(i=1;i<=11;i++) { delay(10000); printf("*"); } printf("\n1.Start The Game"); printf("\n2.Quit The Game"); printf("\nEnter your choice(1-2) : "); scanf("%d",&ch); switch(ch) { case 1: chk=0; win=0; for(i=1;i<=3;i++) { for(j=1;j<=3;j++) { pos_for_X[i][j]=0; pos_for_O[i][j]=0; pos_marked[i][j]=0; } } printf("\n\n"); clrscr(); printf("\nEnter the name of the player playing for \'X\': "); fflush(stdin); gets(name_X); printf("\nEnter the name of the player playing for \'O\': "); fflush(stdin); gets(name_O); Board(); for(;;) { if(win==1) break; check(); if(chk==9) { printf("\n\t\t\tMATCH DRAWS!!"); printf("\nPress any key...."); break; } else chk=0; printf("\nTURN FOR %s:",name_X); PlayerX(); do { if(wrong_X!=1) break; wrong_X=0; printf("\nTURN FOR %s:",name_X); PlayerX(); }while(wrong_X==1); check(); if(chk==9) { printf("\n\t\t\tMATCH DRAWS"); printf("\nPress any key...."); break; } else chk=0; printf("\nTURN FOR %s:",name_O); PlayerO(); do { if(wrong_O!=1) break; wrong_O=0; printf("\nTURN FOR %s:",name_O); PlayerO(); }while(wrong_O==1); } Board(); if(win!=1) { printf("\n\t\t\tMATCH DRAWS!!"); printf("\nPress any key......."); } getch(); break; case 2: printf("\n\n\n\t\t\tThank You For Playing The Game."); printf("\n\t\t\t###############################"); getch(); exit(1); break; } printf("\nWant To Play(Y/N) ? "); fflush(stdin); scanf("%c",&ans); }while(ans=='y' || ans=='Y'); } void Board() { int i,j; clrscr(); printf("\n\t\t\t\tTIC TAC TOE BOARD"); printf("\n\t\t\t\t*****************"); printf("\n\n\n"); printf("\n\t\t\t 1\t 2\t 3"); for(i=1;i<=3;i++) { printf("\n \t\t\t _____________________________"); printf("\n \t\t\t؛\t ؛\t ؛\t ؛"); printf("\n\t\t%d\t",i); for(j=1;j<=3;j++) { if(pos_for_X[i][j]==1) { printf(" X"); printf(" "); } else if(pos_for_O[i][j]==1) { printf(" O"); printf(" "); } else { printf(" "); continue; } } printf("\n\t\t\t؛\t ؛\t ؛\t ؛"); } printf("\n\t\t\t------------------------------"); Player_win(); } void PlayerX() { int row,col; if(win==1) return; printf("\nEnter the row no. : "); fflush(stdin); scanf("%d",&row); printf("Enter the column no. : "); fflush(stdin); scanf("%d",&col); if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 || col>3) { printf("\nWRONG POSITION!! Press any key....."); wrong_X=1; getch(); Board(); } else { pos_for_X[row][col]=1; pos_marked[row][col]=1; Board(); } } void PlayerO() { int row,col; if(win==1) return; printf("\nEnter the row no. : "); scanf("%d",&row); printf("Enter the column no. : "); scanf("%d",&col); if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 || col>3) { printf("\nWRONG POSITION!! Press any key...."); wrong_O=1; getch(); Board(); } else { pos_for_O[row][col]=1; pos_marked[row][col]=1; Board(); } } void Player_win() { int i; for(i=1;i<=3;i++) { if(pos_for_X[i][1]==1 && pos_for_X[i][2]==1 && pos_for_X[i][3]==1) { win=1; printf("\n\nRESULT: %s wins!!",name_X); printf("\nPress any key............"); return; } } for(i=1;i<=3;i++) { if(pos_for_X[1][i]==1 && pos_for_X[2][i]==1 && pos_for_X[3][i]==1) { win=1; printf("\n\nRESULT: %s wins!!",name_X); printf("\nPress any key............"); return; } } if(pos_for_X[1][1]==1 && pos_for_X[2][2]==1 && pos_for_X[3][3]==1) { win=1; printf("\n\nRESULTL: %s wins!!",name_X); printf("\nPress any key......"); return; } else if(pos_for_X[1][3]==1 && pos_for_X[2][2]==1 && pos_for_X[3][1]==1) { win=1; printf("\n\nRESULT: %s wins!!",name_X); printf("\nPress any key....."); return; } for(i=1;i<=3;i++) { if(pos_for_O[i][1]==1 && pos_for_O[i][2]==1 && pos_for_O[i][3]==1) { win=1; printf("\n\nRESULT: %s wins!!",name_O); printf("\nPress any key....."); return; } } for(i=1;i<=3;i++) { if(pos_for_O[1][i]==1 && pos_for_O[2][i]==1 && pos_for_O[3][i]==1) { win=1; printf("\n\nRESULT: %s wins!!",name_O); printf("\nPress any key....."); return; } } if(pos_for_O[1][1]==1 && pos_for_O[2][2]==1 && pos_for_O[3][3]==1) { win=1; printf("\n\nRESULT: %s wins!!",name_O); printf("\nPress any key....."); return; } else if(pos_for_O[1][3]==1 && pos_for_O[2][2]==1 && pos_for_O[3][1]==1) { win=1; printf("\n\nRESULT: %s wins!!",name_O); printf("\nPress any key....."); return; } } void check() { int i,j; for(i=1;i<=3;i++) { for(j=1;j<=3;j++) { if(pos_marked[i][j]==1) chk++; else continue; } } }

Related Source Codes

Script Name Author
gjcphvod VyomWorld
gjcphvod VyomWorld
gjcphvod VyomWorld
gjcphvod VyomWorld
The Game Opposite as seen on Nokia 2300 Mobile Manikanta
RECURSIVE BALANCED QUICK SORT ashish
Radix Sort ashish
Change your mouse pointer Ashim
The blinking star Shashank
Data Validation Crylittlebaby
To search a file by giving file type like mp3 or mpeg or doc Prashanth SR
Menus Demonstration B.Chidhambaram
Employee Database Project Using C. Reenku Raman Nayak
Creating a Lexical Analyzer in c fahad bader al-buhairi فهد بدر البحيري
Calendar Program Omkar & Devendra

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:


Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions
Copyright ©2003-2014 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptId=1221


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review