Tic-Tac-Toe Game in with Artificial intelligence (Version 1.1.5)

Tic Tac toe

Tic Tac toe

Tic Tac toe




// Tic tac toe game in C++ without using 'USER DEFINE FUNCTION'
// The game is written by "Syed Ali Haider"
// Student of Havjery University
// Don't remove these line for legal use
// Upload on Let's programming by (CEO)
// For more games contact
// key_to_programming@yahoo.com
#include <iostream>
using namespace std;

void main ()
{ // body of main function start here

       // variables for each cell
       char c1=' ';
       char c2=' ';
       char c3=' ';
       char c4=' ';
       char c5=' ';
       char c6=' ';
       char c7=' ';
       char c8=' ';
       char c9=' ';

       // c=column and r=wow

       int c;
       int r;

       // for change the sign from 'o' to 'x' and form 'x' to 'o'
       int player;

       // options for check that whether the cell is already filled or not
       int o1=0;
       int o2=0;
       int o3=0;
       int o4=0;
       int o5=0;
       int o6=0;
       int o7=0;
       int o8=0;
       int o9=0;
      
       // lool N0:1 .... outer most loop for countinue the game again and again for play
       cout<<"\t\t\t    !...Tic Toc Toe...!"<<endl;
       cout<<"\t\t\t   ---------------------"<<endl;
       cout<<"\t\t\t   Syed ALi Haider Naqvi"<<endl;
for (int i=1; i<=10;i++)
       {

       cout<<endl<<endl; // for line spacing

       // pattren
       cout<<"\t\t\t\t "<<c1<<" | "<<c2<<" | "<<c3<<endl;
       cout<<"\t\t\t\t---|---|---"<<endl;
       cout<<"\t\t\t\t "<<c4<<" | "<<c5<<" | "<<c6<<endl;
       cout<<"\t\t\t\t---|---|---"<<endl;
       cout<<"\t\t\t\t "<<c7<<" | " <<c8<<" | "<<c9<<endl;
       //pattren


       // condition for win the player 1 (starts)

       if (c1=='o' && c4=='o' && c7=='o') // pattren 1st for player 1
       {
             cout<<"\n\aWow..............Player 1 is won the game..!\n";
             break;
       }
       else if (c1=='o' && c2=='o' && c3=='o') // pattren 2nd for player 1
             {
             cout<<"\n\aWow..............Player 1 is won the game..!\n";
             break;
             }

       else if (c3=='o' && c6=='o' && c9=='o') // pattren 3rd for player 1
             {
             cout<<"\n\aWow..............Player 1 is won the game..!\n";
             break;
             }

       else if (c7=='o' && c8=='o' && c9=='o') //pattren 4th for player 1
             {
             cout<<"\n\aWow..............Player 1 is won the game..!\n";
             break;
             }

       else if (c1=='o' && c5=='o' && c9=='o') //pattren 5th for player 1
             {
             cout<<"\n\aWow..............Player 1 is won the game..!\n";
             break;
             }

       else if (c7=='o' && c5=='o' && c3=='o') //pattren 6th for player 1
             {
             cout<<"\n\aWow..............Player 1 is won the game..!\n";
             break;
             }

       else if (c2=='o' && c5=='o' && c8=='o') //pattren 7th for player 1
             {
             cout<<"\n\aWow..............Player 1 is won the game..!\n";
             break;
             }

       else if (c4=='o' && c5=='o' && c6=='o') //pattren 8th for player 1
             {
             cout<<"\n\aWow..............Player 1 is won the game..!\n";
             break;
             }

       // condition for win the player 1 (ends)
       // condition for win the player 2 (starts)

       else if (c1=='x' && c4=='x' && c7=='x') // pattren 1st for player 2
       {
             cout<<"\n\aWow..............Player 2 is won the game..!\n";
             break;
       }
       else if (c1=='x' && c2=='x' && c3=='x') // pattren 2nd for player 2
             {
             cout<<"\n\aWow..............Player 2 is won the game..!\n";
             break;
             }

       else if (c3=='x' && c6=='x' && c9=='x') // pattren 3rd for player 2
             {
             cout<<"\n\aWow..............Player 2 is won the game..!\n";
             break;
             }

       else if (c7=='x' && c8=='x' && c9=='x') //pattren 4th for player 2
             {
             cout<<"\n\aWow..............Player 2 is won the game..!\n";
             break;
             }

       else if (c1=='x' && c5=='x' && c9=='x') //pattren 5th for player 2
             {
             cout<<"\n\aWow..............Player 2 is won the game..!\n";
             break;
             }

       else if (c7=='x' && c5=='x' && c3=='x') //pattren 6th for player 2
             {
             cout<<"\n\aWow..............Player 2 is won the game..!\n";
             break;
             }

       else if (c2=='x' && c5=='x' && c8=='x') //pattren 7th for player 2
             {
             cout<<"\n\aWow..............Player 2 is won the game..!\n";
             break;
             }

       else if (c4=='x' && c5=='x' && c6=='x') //pattren 8th for player 2
             {
             cout<<"\n\aWow..............Player 2 is won the game..!\n";
             break;
             }
       else // else for, no one can win
       {

                    if (i<10) //for check whether game is drwan on not  here i = the variable of loop No 1 (outer most loop)
                    {
                           ;
                    }
                    else
                    {
                           cout<<"\nGame draw......!\n";
                           break;
            
                    }
       }
       // condition for win the player 2 (ends)


                    // for change the player
                                        if (i % 2!=0)
                                        {
                                               cout<<".:Player 1:."<<endl<<endl;
                                               player = 'o';
                                        }
                                        else
                                        {
                                               cout<<".:Player 2:."<<endl<<endl;
                                               player ='x';
                                        }
                                       
                    // for change the player
      

      
       for (int i=1; i<=2;) //loop No: 2 for ask the user to enter the No of colum and No of row
             {                                                                        
                           cout<<"Row Number: ";
                           cin>>r;
                           cout<<"Colume Number:";
                           cin>>c;

                                        if (r>=1 && c>=1 && r<=3 && c<=3 ) // check only for input 1 to 3
                                       
                                        {
                           //check whether the entered no of cell is already filled or not
       if (r==1 && c==1)

             {           
              if (o1==1) // o1 has value '1' when the cell c1 is already used
                    cout<<"The cell is not empty"<<endl<<endl<<endl;
              else
                     break; // if the cell is not already used then the loop (which ask user to input again and again) break
             }

       else if (r==1 && c==2)
             {     
              if (o2==1)
                           cout<<"The cell is not empty"<<endl<<endl<<endl;
              else
                     break;
             }
       else if (r==1 && c==3)
             {
            
                    if (o3==1)
                           cout<<"The cell is not empty"<<endl<<endl<<endl;
                    else
      
                           break;
             }
       else if (r==2 && c==1)
             {
                     if (o4==1)
                            cout<<"The cell is not empty"<<endl<<endl<<endl;
                     else
                           break;
             }
       else if (r==2 && c==2)
             {
               if(o5==1)
                      cout<<"The cell is not empty"<<endl<<endl<<endl;
               else
                      break;
             }
       else if (r==2 && c==3)
             {
        if(o6==1)
                     cout<<"The cell is not empty"<<endl<<endl<<endl;
        else       
                    break;
             }
       else if (r==3 && c==1)
             {
        if (o7==1)
                     cout<<"The cell is not empty"<<endl<<endl<<endl;
        else
            
                    break;
             }
       else if (r==3 && c==2)
             {
             if (o8==1)
                            cout<<"The cell is not empty"<<endl<<endl<<endl;
             else
                    break;
             }
       else if (r==3 && c==3)
             {
                    if(o9==1 )
             cout<<"The cell is not empty"<<endl<<endl<<endl;
                    else
                                 break;
             }
                                         
             } // end of If No.
                                

                                        else // if the user input other than  1,2,3 then this message is print         
                                        cout<<"you enter invalid input"<<endl<<endl<<endl;
                                       
                                 } // loop No:2 end here (loop for ask the user to enter No of column and No of row)

       // cell fill start from here
       // condition is test for fill the cell according to given No. of column and No. of Row
       if (r==1 && c==1)

             {           
             c1=player;
             o1=1; // o1 variable has value 1 if the user enter for this cell
             }

       else if (r==1 && c==2)
             {     
             c2=player;
             o2=1;
             }
       else if (r==1 && c==3)
             {
             c3=player;
             o3=1;
             }
       else if (r==2 && c==1)
             {
                    c4=player;
                    o4=1;
             }
       else if (r==2 && c==2)
             {
             c5=player;
             o5=1;
             }
       else if (r==2 && c==3)
             {
             c6=player;
             o6=1;
             }
       else if (r==3 && c==1)
             {
             c7=player;
             o7=1;
             }
       else if (r==3 && c==2)
             {
             c8=player;
             o8=1;
             }
       else if (r==3 && c==3)
             {
             c9=player;
             o9=1;
             }
       // cell fill end here
      
 }// Loop No: 1 end here

cout<<endl<<endl<<endl;

} // body of main function here

// All rights are reserved with let's programming
// Tic tac toe game in C++ without using 'USER DEFINE FUNCTION'
// The game is written by "Syed Ali Haider"
// Student of Havjery University
// Don't remove these line for legal use
// Upload on Let's programming by (CEO)
// For more games contact
// key_to_programming@yahoo.com

Comments

Popular posts from this blog

Umbrella activities in Software Engineering

Operating System | Best Definition of Opetating System