Simple Tic-Tac-Toe

Below you can find the code of Simple Tic Tac Toe in C++ and little explanation how it works. If you copy and paste this code, it works fine in Visual studio or with any C++ compiler. If you are already familiar with  Tic Tac Toe code in c++, then you can directly move to my game. Otherwise you should first try this and understand it.. I have explained working of all these functions below this code.
  1.  #include<iostream.h>//<iostream> in vs2017
  2.  using namespace std;
  3.  char square[10]={'o','1','2','3','4','5','6','7','8','9'};
  4.  int checkwin();
  5.  void board();
  6.  int main()
  7.  {
  8.       int player=1,i,choice;
  9.       char mark;
  10.       do
  11.      {
  12.          board();
  13.          player=(player%2)?1:2;
  14.          cout<<"Player"<<player;
  15.          cin>>choice;
  16.          mark=(player==1)? 'X':'O';
  17.          if(choice==1&&square[1]=='1')
  18.                 square[1]=mark;
  19.          elseif(choice==2&&square[2]=='2')
  20.                square[2]=mark;
  21.          elseif(choice==3&&square[3]=='3')
  22.                square[3]=mark;
  23.          elseif(choice==4&&square[4]=='4')
  24.                square[4]=mark;
  25.          elseif(choice==5&&square[5]=='5')
  26.                square[5]=mark;
  27.          elseif(choice==6&&square[6]=='6')
  28.                square[6]=mark;
  29.          elseif(choice==7&&square[7]=='7')
  30.                square[7]=mark;
  31.          elseif(choice==8&&square[8]=='8')
  32.                square[8]=mark;
  33.          elseif(choice==9&&square[9]=='9')
  34.               square[9]=mark;
  35.           else
  36.          {
  37.                cout<<"Invalid move";
  38.                player--;
  39.               cin.ignore();
  40.               cin.get();
  41.           }
  42.              i=checkwin();
  43.              player++;
  44.       }while(i==-1);
  45.       board();
  46.       if(i==1)
  47.           cout<<"==>>>>\aPlayer"<<player;
  48.       else
  49.           cout<<"==>>>>\aGame draw";
  50.           cin.ignore();
  51.           cin.get();
  52.           return 0;
  53. }
  54. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ Win=1, Draw=0, Continue=-1
  55. int checkwin()
  56. {
  57.       if(square[1]==square[2]&&square[2]==square[3])
  58.            return 1;
  59.       elseif(square[4]==square[5]&&square[5]==square[6])
  60.             return 1;
  61.       elseif(square[7]==square[8]&&square[8]==square[9])
  62.             return 1;
  63.       elseif(square[1]==square[4]&&square[4]==square[7])
  64.             return 1;
  65.       elseif(square[2]==square[5]&&square[5]==square[8])
  66.             return 1;
  67.       elseif(square[3]==square[6]&&square[6]==square[9])
  68.             return 1;
  69.       elseif(square[1]==square[5]&&square[5]==square[9])
  70.              return 1;
  71.       elseif(square[3]==square[5]&&square[5]==square[7])
  72.              return 1;
  73.  elseif(square[1]!='1'&&square[2]!='2'&&square[3]!='3'&&square[4]!='4'&&square[5]!='5'&&square[6]!='6'&&square[7]!='7'&&square[8]!='8'&&square[9]!='9')
  74.           return 0;
  75.      else
  76.           return -1;
  77. }
  78. ////// FUNCTION TO DRAW BOARD OF TIC TAC TOE /////////
  79. void board()
  80. {
  81.         system("cls");
  82.        cout<<"\n\n\tTic Tac Toe\n\n";
  83.        cout<<"Player 1(X) - Player 2 (O)"<<endl;
  84.        cout<<endl;
  85.        cout<<"     |        |      "<<endl;
  86.        cout<<"   "<<square[1]<<"   |    "<<square[2]<<"  |     "<<square[3]<<endl;
  87.         cout<<"_____|_____|______"<<endl;
  88.         cout<<"      |         |       "<<endl;
  89.         cout<<" "<<square[4]<<" | "<<square[5]<<" | "<<square[6]<<endl;
  90.        cout<<"_____|_____|______"<<endl;
  91.         cout<<"         |          |         "<<endl;
  92.         cout<<"  "<<square[7]<<"  |  "<<square[8]<<"  |   "<<square[9]<<endl;
  93.        cout<<"        |           |           "<<endl<<endl;
  94. }

1.BOARD function:
From start, it is a void function, so it does not return anything. It simply runs and executes the statements in it. Board function is used to show the lines and numbers to give the frame of a  Tic Tac Toe. Here we used cout for giving output to our console screen. 
The lines or strings given in the double "" quotes are shown on the console as it and help the player.
Here on line 81 system("cls") is used to clear the console screen.
You can see the board by using code shown in below picture:


2.CHECKWIN function:
Checkwin function is used to check the winner or withdraw of game.
Starting at line 55, ther is int data type so it returns an integer after executing. Above the line 54, some part is commented which describe the three values returned by this function and also describe them.
Now in real Tic Tac Toe we have to make three same marks on the same line either O or X.
From the original game we have 8 combination or results to check. (three in horizontal and three in vertical and two in cross).
So, at line 57 in if statement we check the first upper horizontal line(1,2,3), if they have the same mark then we return 1 (for win).Another 7 lines also check the win position and check results.

At line 73, we have elseif statement which checks that either we filled all positions or not. If all positions are filled it returns 0(for draw condition).In this line, we compare each index position of Array named square which stores character in it. This is because of the mark we have used(O and X)are characters.

At line 76, if there is no winner and also we have some input left we have to continue our game. And for this, it returns -1. (also in main function our while loop keeps going if check function return -1).

3.MAIN function: 

So starting from the line, 1 we include a header file iostram(for input and output). and line 2 is optional needed in the most compiler(it is needed for cout and cin operations called Standard input/output operations).

At line 3, I have defined a Character Array square of size 10, but in our game, we need only 9 positions.
Here we take 10 to make convenience in indexing of Array. And also assigned values.
Line 4 and 5 declare two function checkwin and board.

At line 6, our main function starts with an int datatype in some compiler use void.
At line 8 and 9, we take some variable player for choosing player 1 or 2 and choice which is used for taking input from the user and here mark variable is a char because O and X are characters. So we make the mark on the chosen position with respect to our player 1 or 2.
At line 10, do-while loop starts.
At line 12,call board function to show the board, it clears the existing board and updates it on every iteration.
At line 13, we have an if statement or ternary operator to select player 1 or 2.
Line 14 and 15, are used to take input from the user and store into choice variable.
Line 16 select mark based on player.
At line 17, in if statement we check choice. If the choice is equal to 1 and also square[1] has initial value '1' then we add mark there. And after it, all lines check the same thing.
At line 35, we have else statement which is used for all the above lines. If square[x] is not having their initial value or they have mark already. This defines that we choose a wrong number. So we have to roll back.
So line 37, print "invalid move" and line 38 decrement our player and 39 and 40 are used to hold the screen until Enter pressed or any input.Here you can also used single getch() or_getch() in vs2017(using header file <conio.h>).

At line 42, we take the value return by checkwin in the variable i and line 43 increment our player.
At line 44, we have while loop which runs until i is equal to -1(for continue).
At line 45, we again print board with the updated value.
Now line no 47 and 49 shows the result when checkwin return 1 or 0 , so if i=1 then one player from both player wins,and it also show that which player wins.
And if i=0 then the game is a draw, no one won.

If you have any difficulty in the above description please give a comment.

Comments

Popular posts from this blog

Idea about Tic-Tac-Toe Realoded in c++

Source code of Tic-Tac-Toe Reloaded

Hidden Benefits of using Visual studio for beginners