Hi guys, Here's the source code of Tic Tac toe for single player and two player in c++.I have programmed it.
I have programmed this game and I am putting here the source code for sharing knowledge and helping the others.If you have better code from me than please, share the link so everybody can learn something new.share link in comment.Here is the tic_tac_toe's source code .cpp file just click on the link and download it.
CLICK HERE DOWNLOAD SOURCE CODE OF TIC_TAC_TOE_BY_UTSAV_VANODIYA.CPP
NOTE:- I have made it in dev c++ , so if code doesn't work in another compiler , then download dev c++ from http://dev-c1.software.informer.com/download/ . if link doesn't work then download it yourself.
EXPLANATION
Starting of the program
I have used box[3][3] array for the game board.I have used ( _ ) as a blank space.You have to enter the number between 1 to 9 for adding the sign.At the starting of the program you can see the class tic which contains all the functions. then before the main function I have defined all functions.First of all we have to make function for getting information of playing mode and names, one player or two player and names. we have used function getdata() for it. it takes no any arguments and doesn't return any value.
Two player
In the two player , we have to do one thing only, we have to check only result after taking the turn.Count is zero at the starting and every time count increased, whenever count is greater than 4 , we have to check the result.Here First player's sign is 'O' and second player's turn is 'X'.
we have used function int result(int); for checking result.
result function checks the values of raws , columns and cross lines. If the condition is true , Its shows the winner name and its go to the end of the program and ask for play again and change mode. If no one is winner and count is equal to ten then function declares the match is draw.
Single player
For the single player we have used these functions.
int rcc(char);
int xy(int);
int gtch(int,int);
int tempcheck(int);
int randomization(void);
In the single player , first time user will take first turn then second time computer will take first turn.
always first player's sign is 'O' and second player's sign is 'X'.
Before the starting of the explanation of functions we should understands the logic of the tic tac toe.
there three main part related to first putting at corner , center and mid of raw/col.let's see
1.Corner
If the first player puts the sign at any corner , second player have to must to put the sign at the center
Because , if player 2 don't put at center , than definitely he will lose the game. See the image.
In above image you can see the first player puts the 'O' at upper left corner , then player two puts the 'X' at bottom left corner , then the first player puts at upper right corner , Now you have no another choice from second place , after than the first player puts sign at bottom right corner , now it's important to understand the two possibilities of winning. here first player has two possibilities to win , first is cross line and second is 3rd column. if you put 'X' at 5 or 6th place , however you can't win or draw the game. So , if you doesn't choose center , than first player can make two possibilites to win the game.It means if the choice is any corner than you have to must put at the center , then match will be draw.
2. Center
If the first player puts the sign at the center , then you have to select any one of the corner, look at the image.
First scene , If player one putted sign O at center and player 2 putted sign X at mid of first raw. then player 1 putted the sign at upper right corner , now player 2 has must put to sign at bottom left corner and after then player 1 gets chance to make two winning possibility by putting sign at mid of 3rd column. The second scene is similar scene to first scene.
You can see the image and understand the the case of two possibilities, if you put at middle of the raw or column , then first player have to chance of making winning possibilities , so we have to must put at any corner for win to match or draw to match.
3. Mid of raw or column.
If the first player put the sign at mid of raw or column first , than we have to put at center, Because mostly the game will be draw at this situation, so please as a first player don't put sign at mid.
Conclusion
From the above three scenario , we can say that we have to play game with strategy of making two winning possibilities. If our turn is second then don't give the chance for making two possibilities.From this rule we have made function rcc (raw,column,cross-line). Let's sees the work of rcc function.
Function int rcc(char);
First of all why it's name is rcc? (raw,column and cross-line). because its checks the raws , columns and cross-lines. If the function finds the required specific line , then it's return 1. if no any line is founded then its return 0. The function take one argument of character and return 0 or 1. 0 for false and 1 for true.
First of all function check the how many signs and blank spaces in the line. It will be stored in variale si (stores number of sign) and blk (number of blanks). there are two types are lines are important.
1. Line has two same signs and one blank space.
2. Line has one sign and two blank space.
1. Line has two same signs and one blank space.
When line has two same signs and one blank space , then the second player must have to put sign at the blank space , otherwise he will be lose the game.
So , first of all rcc function checks for this possibility and if it's founded then function return 1 and the place will be filled by computer's sign.this is the the starting of logic of computer. Computer first check for own sign possibility , it means it checks for own winning chance and if founded then computer puts there their sign.here computer's sign is 'X' and it has second turn. Computer will put there X.
How to generate two winning possibilities for computer???. the answer is this line which has one sign and two blank spaces. Here one thing is important and it is the at least one raw and column or one raw and cross-line or one column and cross-line has the situation of one sign and two blank spaces. if two raws has situation like this but no one column and cross-line satisfy the condition , than its not usable to generate winning possibilities.same as it's applicable for column, but in the the case of two cross-line it will generate the winning possibility.
First of all , we know that if first player puts sign at corner then other player must have to put sign at corner , so this rule is also applicable for computer as a first player. when computer starts the game its always put sign at any corner and after than the human have must to put sign at center , if human player don't do it , computer will generate the two possibilities.Let's see how it's works...
2. Line has one sign and one blank space.
How to generate two winning possibilities for computer???. the answer is this line which has one sign and two blank spaces. Here one thing is important and it is the at least one raw and column or one raw and cross-line or one column and cross-line has the situation of one sign and two blank spaces. if two raws has situation like this but no one column and cross-line satisfy the condition , than its not usable to generate winning possibilities.same as it's applicable for column, but in the the case of two cross-line it will generate the winning possibility.
First of all , we know that if first player puts sign at corner then other player must have to put sign at corner , so this rule is also applicable for computer as a first player. when computer starts the game its always put sign at any corner and after than the human have must to put sign at center , if human player don't do it , computer will generate the two possibilities.Let's see how it's works...
Look at the above image , If computer first put sign O at place upper left corner and after then human player put sign X at bottom left corner , then computer can put sign at upper right and bottom right corner also. It means If computer put sign at any corner then human player has no another chance from place like arrowed place in above figure. After this situation , function rcc checks for two winning possibilities or intersection point. See the below image.
Function rcc checks this and consider intersection point or place like bottom right corner. Here human has no any chance from first raw's mid place , after than computer put the sign at bottom right corner and win the game. Suppose if computer hasn't any intersection point or place than checks for opposite player if it's founded like given in second case , than computer try to make break the point or if more than one points are founded then computer make the situation like user can't use the intersection point and he has must to put sign at the some specific place or you can see the above image which shows the necessary place situation.
Function int xy(int);
This function takes one intger as a argument and gives us co-ordinates values.When we taking input from user , it's number between 1 to 9 , but we have used 3x3 character array , so we have convert number to the array. For example user enters the number 5 , it is second raw and second column or center place , here function xy take x=1 and y=1 , in the array it's starts from 0 and ends to n-1 , so the selected place is box[1][1]. you can easily understand it.This function takes one intger as an argument and gives us co-ordinates values.
Function int gtch(int,int);
In the Single player logic , we have to use sometime this function , it's opposite function of xy(int). xy takes one integer as an argument when gtch function takes co-ordinates and return the number between 1 to 9.
Function int randomization();
The Function randomization(); gives us the random number between 1 to 9. Suppose sometimes in the game , computer has two possible place for putting sign than , computer use this function and put the sign randomly , like a computer has first turn and it's starting the game , now it has 9 place to put sign but computer will put sign at any one corner from the 4 corner. here 4 corner are possible places.
so this function can return the value 1,3,7 or 9.
Function int tempcheck(int);
This function checks the selected place is eligible or not , it means whenever randomization function called, it's necessary to check the place is blank or not. When the scene of Intersection point are executed we have to use this function.
Thanks Bro for sharing your code it's really nice and really helped me. Keep it up...
ReplyDeleteWelcome bro..
ReplyDeleteCool, bro! Such awesome code for tic tac toe is not found anywhere else on the internet. Very informative and extremely helpful. Thanx
ReplyDeleteWelcome bro..
Deletenice game
ReplyDelete