Add to Favorites    Make Home Page 11485 Online  
 Language Categories  
 Our Services  

Home » C Home » Beginners / Lab Assignments Home » Tic-tac-toe game (Mini Project)

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

Search Projects & Source Codes:

Title Tic-tac-toe game (Mini Project)
Author Ratnam
Author Email reths [at] hotmail.com
Description This program will let to two players tp play tic tac toe game.It uses pointers and structures to implement the game Hope you enjoy it.Please feel free to email if you have any doubts about any codes.

Category C » Beginners / Lab Assignments
Hits 369570
Code Select and Copy the Code
Code : /* File: tictactoe.c Author: Nagarethinam Date: 30/09/04 17:37 Description: This source code is about Tic-tac-toe game . */ /******************Preprocessor****************/ #include<stdio.h> #include<stdlib.h> #include <string.h> #include <conio.h> #include <conio.c> void menu (void); //function prototype void gameins (void);//function prototype void loading(void);//function prototype void play(void); //function prototype void profile(void);//function prototype void IntroPage(void);//function prototype void getnames(void);//function prototype struct player { char name1[20]; char name2[20]; char winner[20]; } player; /****************** This is the main () function**********/ int main() { IntroPage(); menu(); system("pause") return 0; } /*********** this is user defined IntroPage() function**********/ void IntroPage() { printf(" "); printf(" ************************************************ "); printf(" * * "); printf(" * * "); printf(" * Welcome To * "); printf(" * ****************** * "); printf(" * * "); printf(" * TIC TAC TOE created by Ratnam * "); printf(" * * "); printf(" ***************************************** "); printf(" "); printf(" Press any Key to continue...."); getch(); } // end of IntroPage() function /*******************this is user defined Menu() function*******/ void menu() { int selection; do { system("cls");//clear away any display on previous screen printf(" "); printf(" ****************************************** "); printf(" * MENU * "); printf(" * ******** * "); printf(" * * "); printf(" * 1.Game Instruction * "); printf(" * 2.Play Tic Tac Toe * "); printf(" * 3.Player's Profile * "); printf(" * 4.Exit * "); printf(" * * "); printf(" * * "); printf(" * * "); printf(" ****************************************** "); printf(" Please Select the Option :"); scanf(" %d",&selection); switch (selection)//to select each of the option { case 1: gameins(); //function call, program will jump to the user defined Funct break; case 2: getnames();//function call, program will jump to the user defined Funct play();//function call, program will jump to the user defined Funct break; case 3: profile();//function call, program will jump to the user defined Funct break; case 4: break; } //end of switch case } while(selection !=4);//Repeat itself until selection equels to 4 }// end of Menu() function /********* this is user defined getnames() function**********/ void getnames() { FILE *in; //File pointer declaration in=fopen("C:\My Documents\Cpro\reths.txt","ab+");//File opening printf(" "); printf(" Player1 Enter Your name:"); scanf("%s",player.name1); printf(" Player2 Enter Your name:"); scanf("%s",player.name2); fwrite(&player,sizeof(player),1,in);//User input values are written in the file fclose(in); //File being Closed printf(" "); printf(" %s will be X, %s will be 0. ",player.name1,player.name2); }// end of getnames() function /**************** this is user defined gameinst() function*******/ void gameins() { system("cls");//clear away any display on previous screen printf(" Object of the Game: "); printf(" To be the first player to get three 'X' or 'O' "); printf(" of their playing pieces in a row horizontally, "); printf(" diagonally, or vertically. "); printf(" "); printf("Press any Key to return to Menu:"); getch(); }// end of gameins() function /*********** this is user defined profile() function*********/ void profile() { system("cls"); FILE *in; //File pointer in=fopen("C:\My Documents\Cpro\reths.txt","ab+");//file opening to write printf(" ************+***************+**** "); printf(" * player1 | Player2 | "); printf(" ************+***************+**** "); rewind(in);//Rewinding tp bring the ponter to first position while(fread(&player,sizeof(player),1,in))//reading the file { printf(" *%-25s%-20s ",player.name1,player.name2);//printing to scr } fclose(in); system("pause"); } /****** this is user defined play() function***********/ void play() { loading(); system("cls");//clear away any display on previous screen char tic_tac_no[9]={49,50,51,52,53,54,55,56,57};// this ASCII characters represent 1,2,3,4,5,6,7,8 char *ptic= tic_tac_no; //ponter declaration int i,j;//"i" & "j" are user input number that will represent 1-9 int b=0; //"b" will check for draw condition while(1) //Keeps looping until program exit itself forcibly { printf(" "); printf(" %c | %c | %c ", *ptic,*(ptic+1),*(ptic+2)); printf(" ----------------- "); printf(" %c | %c | %c ", *(ptic+3),*(ptic+4),*(ptic+5)); printf(" ----------------- "); printf(" %c | %c | %c ", *(ptic+6),*(ptic+7),*(ptic+8)); printf(" "); do { printf(" %s Enter Your Choice :",player.name1); scanf(" %d",&i);//"i" would be accepted and would represent 1-9 printf(" "); }while((i<1)||(i>9));//only 1-9 would be allowed if((*(ptic+(i-1))!='X')&&(*(ptic+(i-1))!='O'))//this is make sure { //only Num not occupied by *(ptic+(i-1))='X'; //'X' and 'o' would be allowed } else { do { printf("Reenter your Choice as %d is taken up :",i); scanf("%d",&i); }while((i<1)||(i>9)||(*(ptic+(i-1))=='X')||(*(ptic+(i-1))=='O' )); *(ptic +(i-1))='X'; //Above condition check for availabity of space } system("cls"); b++; //'b' is incremented each time program loops to check for draw printf(" "); printf(" %c | %c | %c ", *ptic,*(ptic+1),*(ptic+2)); printf(" ----------------- "); printf(" %c | %c | %c ", *(ptic+3),*(ptic+4),*(ptic+5)); printf(" ----------------- "); printf(" %c | %c | %c ", *(ptic+6),*(ptic+7),*(ptic+8)); //This Condition will check for Player 1 win if (( *ptic=='X'&& *(ptic+1) == 'X'&& *(ptic+2) == 'X') || ( *(ptic+3) == 'X' && *(ptic+4)=='X'&& *(ptic+5) == 'X')|| ( *(ptic+6) == 'X' && *(ptic+7) == 'X' &&*(ptic+8) == 'X') || ( *ptic=='X' && *(ptic+3)=='X' && *(ptic+6)=='X' )|| ( *(ptic+1)=='X' && *(ptic+4)=='X' && *(ptic+7)=='X' )|| ( *(ptic+2)=='X' && *(ptic+5)=='X' && *(ptic+8)=='X' ) || ( *ptic=='X' && *(ptic+4)=='X' && *(ptic+8)=='X' ) || ( *(ptic+2)=='X' &&*(ptic+4)=='X' && *(ptic+6)=='X' )) { printf(" %s won ",player.name1); system("pause"); break; } else if(b==9)//Draw is reached if b==9 { printf(" This game Resulted in Draw "); system("pause"); break; } do// loop until the player2 enters num from 1-9 { printf(" %s Enter Your Choice:",player.name2); scanf(" %d",&j); }while((j<1)||(j>9)); if((*(ptic+(j-1))!='X')&&(*(ptic+(j-1))!='O')) { *(ptic+(j-1))='O';//assign only if not occupied by X or O } else//ask the user enter again { do //will ask to reenter until he types the available number { printf("Reenter Your Choice as %d is taken up :",j); scanf("%d",&j); }while((j<1)||(j>9)||(*(ptic+(j-1))=='X')||(*(ptic+(j-1))=='O' )); *(ptic +(j-1))='O'; } b++;// Increments as the program loops //This condition will check if player 2 won if (( *ptic=='O'&& *(ptic+1) == 'O'&& *(ptic+2) == 'O') || ( *(ptic+3) == 'O' && *(ptic+4)=='O'&& *(ptic+5) == 'O')|| ( *(ptic+6) == 'O' && *(ptic+7) == 'O' &&*(ptic+8) == 'O') || ( *ptic=='O' && *(ptic+3)=='O' && *(ptic+6)=='O' )|| ( *(ptic+1)=='O' && *(ptic+4)=='O' && *(ptic+7)=='O' )|| ( *(ptic+2)=='O' && *(ptic+5)=='O' && *(ptic+8)=='O' ) || ( *ptic=='O' && *(ptic+4)=='O' && *(ptic+8)=='O' ) || ( *(ptic+2)=='O' &&*(ptic+4)=='O' && *(ptic+6)=='O' )) { printf(" %s won ",player.name2); system("pause"); break; } system("cls"); } } /***** this is user defined loading() function to creaTE LOADING eFFECT*********/ void loading() { int x; int wait=6;//will create delay for 6 secs printf("Loading"); for(x=1;x<=wait;x++) { printf("."); _sleep(1000); } printf("Done! "); }

Related Source Codes

Script Name Author
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
Stop double Process for start in C Cedrik Jurak
Stop double Process for start in C Cedrik Jurak
Time Scheduler Atiq Anwar
A timepass game between atmost two players Rahul Roy

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 | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=860


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