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

Home » C++ Home » Games Home » TicTacToe Game

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

Search Projects & Source Codes:

Title TicTacToe Game
Author Harsh
Author Email harsh_sahajwani [at] indiatimes.com
Description This is a tictactoe game in C++. Although it is not too smart to prevent you from winning but it is worth playing it. I hope u enjoy it. Happy gaming!
Category C++ » Games
Hits 385251
Code Select and Copy the Code
Code : #include<iostream.h> //opening headerfile iostream for in-out #include<conio.h> //opening headerfile conio for clrscr(); #include<stdlib.h> //opening headerfile stdlib for random(int); char tic[3][3]; //global matrix declerations int d,e,f,a,t,i,j,x,y; //global variables declerations void display(); //displays the matrix void user(); //function for user's move void newdisp(); //function for display of matrix after every move void pc(); //function for pc's move int check(); //function for finding out the winner int horcheck(); //function for horizontal line check int vercheck(); //function for vertical line check int diagcheck(); //function for diagonal line check main() //main function { clrscr(); //clears the previous output screen randomize(); //initialize random function calling int d=random(2); //random function call for(i=0;i<3;i++) for(j=0;j<3;j++) tic[i][j]=' '; //assigning space ' ' to all elements of matrix display(); //display function call d==0?user():pc(); //random starting of the game depending on d getch(); //provides output by getting input without returning to program return 0; //return int to main function } void display() //display function definition { for(t=0;t<3;t++) { cout<<" "<<tic[t][0]<<" | "<<tic[t][1]<<" | "<<tic[t][2]<<endl; //figure formation if(t!=2) cout<<" --|---|--"<<endl; } } void user() //user function definition { cout<<" ENTER THE CO-ORDINATES WHERE YOU WANT TO PUT UR 'X' i.e 0,1,2 "; cin>>x>>y; if((x<0)||(x>2)&&(y<0)||(y>2)) //check for valid co-ordinates { cout<<" ENTER THE CORRECT CO-ORDINATES"; user(); //user function call } else { if(tic[x][y]==' ') //check for vacant space at entered co-ordinates { tic[x][y]='X'; //assigning user 'X' to the co-ordinates newdisp(); //newdisp function call } else { cout<<" THIS POSITION IS ALREADY FILLED. CHOOSE SOME OTHER CO-ORDINATES"; user(); //user function call } } d=check(); //check function call if(d==0) pc(); //pc function call else { cout<<" YOU ARE THE WINNER"; getche(); //requires enter to return to program. prevents return without display exit(1); //program termination } } void newdisp() //newdisp function definition { for(t=0;t<3;t++) { cout<<" "<<tic[t][0]<<" | "<<tic[t][1]<<" | "<<tic[t][2]<<endl; //displays new tictactoe after user or pc turn if(t!=2) cout<<" --|---|--"<<endl; } } void pc() //pc function call { int f,z; randomize(); //initialize random function calling cout<<" THIS IS THE COMPUTER'S MOVE "; for(i=0;i<=20;i++) { f=random(3); z=random(3); if(tic[f][z]==' ') //check for vacant space at entered co-ordinates { tic[f][z]='O'; //assigning pc 'O' to the co-ordinates goto x; //exiting for loop to display new tictactoe } else continue; // } x:newdisp(); //newdisp function call d=check(); //check function call if(d==0) user(); //user function call else { cout<<" I'M THE WINNER"; getche(); //requires enter to return to program. prevents return without display exit(1); //program termination } } check() //check function definition { horcheck(); //horcheck function call vercheck(); //vercheck function call diagcheck(); //diagcheck function call return (d||e||f); } horcheck() //horcheck function definition { if(((tic[0][0]==tic[0][1])&&(tic[0][1]==tic[0][2])&&(tic[0][1]!=' '))||((tic[1][0]==tic[1][1])&&(tic[1][1]==tic[1][2])&&(tic[1][1]!=' '))||((tic[2][0]==tic[2][1])&&(tic[2][1]==tic[2][2])&&(tic[2][2]!=' '))) d=1; //checks each element of a horizontal line to be same else //returns 1 if all 3 elements of any horizontal line are same d=0; //else returns 0 return d; } vercheck() //vercheck function definition { if(((tic[0][0]==tic[1][0])&&(tic[1][0]==tic[2][0])&&(tic[0][0]!=' '))||((tic[0][1]==tic[1][1])&&(tic[1][1]==tic[2][1])&&(tic[0][1]!=' '))||((tic[0][2]==tic[1][2])&&(tic[1][2]==tic[2][2])&&(tic[0][2]!=' '))) e=1; //checks each element of a vertical line to be same else //returns 1 if all 3 elements of any vertical line are same e=0; //else returns 0 return e; } diagcheck() //diagcheck function definition { if(((tic[0][0]==tic[1][1])&&(tic[1][1]==tic[2][2])&&(tic[0][0]!=' '))||((tic[0][2]==tic[1][1])&&(tic[1][1]==tic[2][0])&&(tic[1][1]!=' '))) f=1; //checks each element of a diagonal line to be same else //returns 1 if all 3 elements of any diagonal line are same f=0; //else returns 0 return f; }

Related Source Codes

Script Name Author
Moving ball screen saver karlmarx
The Classic Game of Snake & Ladder Lakshmi Narayana .A
Railway seat reservation question which comes in sapient VyomWorld
To calculate percentile Ravi Mathur
Send to folder ANIMESH SAHU
Analog clock and calendar Nazia & Rida
HIGH/LOW GAME MOLLY ARORA
Data structure (stack Implimentation) Swapnil B Adsure
Memory Game AnirudhSanyal
Easy Calc Anirudh Sanyal
GK Quiz Anirudh Sanyal
Hangman Game Manish Jain
Snakeman Manish Jain
Full month Calendar Nigi
Cursor shapes nigi

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=969


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