Explanation of the main function of Tic Tac Toe reloaded
Below, I have explained main code for manual mode. For automatic mode, we need to follow some extra step, which I will explain in later posts. So follow me to keep updated. Please check my source code post to check full code file, as there are some mistakes in code below.
- #include "stdafx.h"
- #include<iostream>
- #include<conio.h>
- #include<proces.h>
- #include<vector>
- #include<cstdlib>
- #include<fstream>
From starting we have some INCLUDE statement to include the libraries which we needed in this program.
On line 1 is the library of visual studio which it uses for indentation and format. So, it always included in visual studio project.
Line 2 is for input/output stream, this provides the basic print statements and input/output.
Line 3 is used for getch() function to hold the screen, it is like co-ordination of input and output.
Line 4 is used some processing library and for clear screen. On some compiler, you can directly use clrscr() function.
On line 5 I have included vector, so maybe it is new thing to you. Vector are like an array but their size doesn't need to be define and they automatic increase.
Line 6 is for a library which give function to generate random numbers.
Line 7 is the fstream used for file manipulation, create/edit/delete.
- using namespace std;
- vector<int>arrv2; vector<int>arrv4;
- vector<int>arrv6; vector<int>arrv8;
- void board(int[]);
- int search(int[], int, int&);
- bool equal(int[], int[]);
- void save_temp(int[], int[], int[], int[], int[], int);
- int checkwin(int[]);
- void save(int[], int[], int[], int[]);
- void savef(int[], int[], int[], int[]);
- void vsize();
- void f_to_v();
- void AddRand(int[], int[], int[], int[], int);
- int random(); int randm = 0;
- void read(int[], int[], int[], int[], int[], int);
For above table:
Line 1 is to use a namespace std(standard).It is used to input and output.With this we don't need to use cin and cout like std::cin and std::cout.
Line 2,3 initializes vectors of type integer having names like arrv2,arrv4and so on. Here arr means an array and v for vector and 2 is a no which represent the count of move or turn of player. 1,3,5,7,9 moves are for the human player.
Line 4 declare a function of type void(so it return nothing).It is simple function to show the tic tac toe on console.
It takes a array of type int as parameters.
Line 5 declare a function of type int (return a int).This search function is made for to search particular inputs and give computer a number to proceed.
In this 3rd argument is a reference type.(if you can't understand reference argument please google it.)
Line 6. A function type of bool(true or false).It check equality of moves played by player1.
Line 7 A function to store all the array for temporary purpose. Parameters are not clear at the defination. So, please wait.
Line 8 function checks that a player wins or not.
Line 9 function to save temporarily values permanently in a vector.
Line 10 function to save permanent vector values to a file in disk.
Line 11 This function stores vector's size information or length of each vector.
Line 12 function makes vectors from files.
Line 13 function adds the random number generated in arrays.
Line 14 A function to generate random number and a variable randm to store that number.
Line 15 Extra function to see how values are work in program and changes are occur in arrays.
Just for logging and debugging purpose.
Now less Start from main:
- int main()
- {
- int arr2[10], arr4[10], arr6[10], arr8[10];
- for (int i = 0; i < 10; i++) //intialize all array to 48("0")
- {
- arr2[i] = 48; arr4[i] = 48; arr6[i] = 48; arr8[i] = 48;
- }
- f_to_v();
- int ac = 0, winp = 5; //ac is for automation and winpointer or counter
- start:
- int count = 1;
- char ans = 'y';
- int player = 1, i, choice = 0;
- int mark;
- int sq[10] = { 48,49,50,51,52,53,54,55,56,57 }; //IN ASCII{0,1,2,3,4,5,6,7,8,9}
Line 1,2 is main function with open bracket.So in Visual studio main has type int.
Line 3 makes 4 arrays of size 10(index = 0 to 9)
Line 4-7 is FOR loop which initialize every position of every array with a no. 48. In ASCII code it is character '0'
Line 8 is call to function f_to_v which is for storing previous value from file to vectors(arrv2,arrv4...)
Click if you want to see description of this function.Click
Line 9 is for automation purpose.I explained later.(not needed in manual mode)
Line 10 is a link function in c++.It is used to jump from the below code to this link. It will clears later, so keep reading.
Line 11 -14 had some variables count(to counts move),ans(to save answer from user),player(1 is human),i is for loops,choice(to save your given input),mark(to show X or O on console)
Line 15 intialize a array sq to 48-57 number(in ASCII 0-9 characters)
- do
- {
- //if (ac >= 400) { board(sq); } //for automation uncommented
- board(sq); //for normal uncommented
- player = (player % 2) ? 1 : 2;
- //if (player == 1) { //for automation
- // if (ac < 400)
- // choice = random();
- // else
- // {
- // cout << "Player " << player << " , enter a number: ";
- // cin > choice;
- // }
- //}
- //else
- //{
- // //cout<< "Player 2 _ENTER";
- // choice = search(sq, count, winp);
- // randm = choice;
- //}
- if (player == 1) { //for normal
- cout << "Player " << player<< " , enter a number: ";
- cin >> choice;
- }
- else
- {
- //cout << "Player 2 _ENTER";
- choice = search(sq, count, winp);
- randm = choice;
- }
Line 1,2 starts a do-while loop .Line3 is commented because we are using manaual mode.
Line 4 is call to board function and the output elements of sq. At starting these are 0-9.
Line 5 is a imediate-if statement.If player value is odd selects 1 else 2. At first time as player=1 so the output is 1.
Line 6-22 are for automatic version. Please ignore these.
Line 23 check if player==1 then statement are executed.At first iteration if clause is executed.
Line 24 is to write on console asking for a number and Line 25 takes a input which stores in choice veriable.(int value)
Line27 else clause is for computer. When it is executed the value of choice is taken from search function.
Here winp is used to check if the searched array is equal to a previous result and then choice is chosen from previous result and winp changes its value and don't save the same result string again in results.
This stuff clears when we see search function.
Line 31 assign choice value to the randm variable.
- AddRand(arr2, arr4, arr6, arr8, count);
- mark = (player == 1) ? 88 : 79; ////IN ASCII 88=X,79=O
- if (choice == 1 && sq[1] == 49)
- sq[1] = mark;
- else if (choice == 2 && sq[2] == 50)
- sq[2] = mark;
- else if (choice == 3 && sq[3] == 51)
- sq[3] = mark;
- else if (choice == 4 && sq[4] == 52)
- sq[4] = mark;
- else if (choice == 5 && sq[5] == 53)
- sq[5] = mark;
- else if (choice == 6 && sq[6] == 54)
- sq[6] = mark;
- else if (choice == 7 && sq[7] == 55)
- sq[7] = mark;
- else if (choice == 8 && sq[8] == 56)
- sq[8] = mark;
- else if (choice == 9 && sq[9] == 57)
- sq[9] = mark;
Line 1 call function AddRand to store the choice made or randm value into a specified array.(by count variable.)
Line 2 assign mark to players. So if player is 1 value is 88('X') else 79('O')
Lines 3-20 are if or elseif statement. At line 3 if choice is 1 and also 2nd element of sq(array) is default 49 then assign mark at that place.
All below statement are work similar. If we repeat or duplicate any choice then the last else condition executes. which is given in below table.
- else {
- //////////FOR INVAILD MOVE
- if (player == 1) {
- cout << "\nInvalid move! Press Enter:";
- cin.ignore();cin.get(); //To hold cursor(also useable _getch())
- }
- player--;
- count--;
- }
- save_temp(sq, arr2, arr4, arr6, arr8, count);
- i = checkwin(sq);
- player++;
- count++;
- //if (ac >= 400) { board(sq); } //for automation
- board(sq); //board(sq); //for normal
- } while (i == -1);
- //read(sq, arr2, arr4, arr6, arr8, count);
- if (i == 1) {
- cout << "==>\aPlayer " << --player << " win ";
Line 1 is a else condition .If input or choice is a invaild than this code statement executed.
Line 3 if player is human then we write invalid move on console else not.
Line 5 is to ignore the input.
Line 7 decremented player and line 8 decrement count so the previous loop executed once again.
Line 11 If all goes good.save the new arrays temperarly.
Here value at 0 index is the choice for computer to use afterword and the 9 letter after index 0 is the full sq array at that time.
So, we match 1-10 element to sq in search function and if match exist we used the 0th element as choice.
If not find match computer return a random number.(clear properly in search function.)
Line 12 i is assigned to the return of checkwin function.(check for winning)
Line 13-14 if all goes good incerement player and count.
Line 15 is for automation.So skip it.
Line 16 print updated board.
Line 17 is end of do-while loop .So if i have value -1 ,loop continue. Else there is a winner or tie.
Line 18 is to print the value worked in one game.To use it just uncommented it.
Line 19 checks if i is 1 then there is a winner(which is player-1 (previous player))
Line 20 output the winner player on console.
- if (player == 2) {
- ac++;
- if (winp <= 5) {
- save(arr2, arr4, arr6, arr8); savef(arr2, arr4, arr6, arr8); vsize(); winp = 5;
- //cout << "\nvector size:" << arrv2.size();
- }
- }
- }
- else
- {
- cout << "==>\aGame draw";
- if (winp <= 5) {
- save(arr2, arr4, arr6, arr8); savef(arr2, arr4, arr6, arr8); vsize(); winp = 5;
- //cout << "\nvector size:" << arrv2.size();
- }
- }
- char answ = 'y';
- //if (ac >= 400) //for automation
- //{
- cout << "\n////PLAY AGAIN/SAVE (Y/N):"; cin >> answ; //for normal
- if (answ == 'y' || answ == 'Y') { goto start; }
- cout << "\nEXITING ON ANY KEY";
- _getch();
- /*}
- else { goto start; } */ //for automation
- }
Line 1 if player 2(computer) is winner.
Line 2 is for automation.
Line 3 if winp is less or equal to 5. Then there is a new result, which needs to store.
Line 4 save all array in vectors and also vectors in file(savef()).
Also save vectors size in vsize and reset winp to 5.
Line 5 Uncomment if u want to see the size of each vector(defalut arrv2 just add more lines).How many values are added to previous(good stuff).
Line 9 is else. So if no one wins, match drawn. It is also a good result to save to draw a match again.
Same code as Line 4 ,so on.
Line 18 assign y to answer.Line 19 is for automation.
Line 21 ask for play again and take input in answ variable.(Yes or no)
Line 22 if answer is yes(y or Y). Program start from link start(Table 2 Line 10).
Line 23 to show existing from game on any key press.
Line 24 get a input.(hold until key press.)
Comments
Post a Comment