Make Your Search Easy ! :) Use me

Monday, October 16, 2017

Tic Tac Toe

C++ program to create game of Tic-Tac-Toe ( X-0 game).

#include<iostream>
#include<conio.h>
using namespace std;

int grid[9]={49,50,51,52,53,54,55,56,57},turn=1,gp=-1,total=0;

int board()
{
      clrscr();
      std::cout<<"\n\t\t\tTic Tac Toe\n\n";
std::cout<< "\t"<<(char)grid[0]<<"  |  "<<(char)grid[1]<<"  |  "<<(char)grid[2]<<endl;
std::cout<<"\t"<<"----------------"<< "\n";
std::cout<<"\t"<<(char)grid[3]<<"  |  "<<(char)grid[4]<<"  |  "<<(char)grid[5]<<endl;
std::cout <<"\t"<<"----------------"<< "\n";
std::cout<< "\t"<<(char)grid[6]<<"  |  "<<(char)grid[7]<<"  |  "<<(char)grid[8]<<endl;
return 0;
}

int input()
{
int option,mark;
if (turn)
{
cout<<"PLAYER 1 ";
mark=88;
turn=0;
}
else
{
cout<<"PLAYER 2 ";
mark=79;
turn=1;
}
inp:
cout<<"Enter number grid number :";
cin>>option;
switch (option) {
case 1:
if(grid[0]==88 || grid[0]==79)
goto inp;
grid[0]=mark; break;
case 2:
if(grid[1]==88 || grid[1]==79)
goto inp;
grid[1]=mark; break;
case 3:
if(grid[2]==88 || grid[2]==79)
goto inp;
grid[2]=mark; break;
case 4:
if(grid[3]==88 || grid[3]==79)
goto inp;
grid[3]=mark; break;
case 5:
if(grid[4]==88 || grid[4]==79)
goto inp;
grid[4]=mark; break;
case 6:
if(grid[5]==88 || grid[5]==79)
goto inp;
grid[5]=mark; break;
case 7:
if(grid[6]==88 || grid[6]==79)
goto inp;
grid[6]=mark; break;
case 8:
if(grid[7]==88 || grid[7]==79)
goto inp;
grid[7]=mark; break;
case 9:
if(grid[8]==88 || grid[8]==79)
goto inp;
grid[8]=mark; break;
default:
goto inp;
}
total++;
return(0);
}

int check()
{
if((grid[0]==grid[1]&&grid[1]==grid[2]) || (grid[3]==grid[4]&&grid[4]==grid[5]) || (grid[6]==grid[7]&&grid[7]==grid[8]))
{
cout<<(char)grid[0]<<" WON THE GAME";
return(1);
}
else if((grid[0]==grid[3]&&grid[3]==grid[6]) || (grid[1]==grid[4]&&grid[4]==grid[7]) || (grid[2]==grid[5]&&grid[5]==grid[8]))
{
cout<<(char)grid[0]<<" WON THE GAME";
return(1);
}
else if((grid[0]==grid[4]&&grid[4]==grid[8]) || (grid[2]==grid[4]&&grid[4]==grid[6]))
{
cout<<(char)grid[0]<<" WON THE GAME ";
return(1);
}
if(total==9)
{
cout<<"GAME DRAWN";
return(0);
}
return(-1);
}
int main()
{
while(check()<0)
{
board();
input();
}
  }

Common Multiple

C++ program to find the nth common multiple of two given numbers.


#include <iostream>
#include <conio.h>
using namespace std;

int main()
  {
    int a,b,n=0,num;
    std::cout << "Enter two numbers to find common multiple : "<< "\n";
    std::cin>>a>>b;
    std::cout<<"Enter the the common multiple required : ";
    std::cin>>num;
    for (size_t i = 4; ; i++)
    {
      if(i%a==0 && i%b==0)
          n++;
      if (n==num) {
        std::cout<<i;
        break;
      }
    }
    getch();
    return 0;
  }

Matrix

C++ program to find the sum of diagonals of a matrix.



#include<iostream>
#include<conio.h>
#include<vector>

using namespace std;
std::vector<vector<int>> matrix;
int r;

int getarray()
  {
    cout<<"Enter the number of rows and colums of array : ";
    cin>>r;
    for (int i = 0; i < r; i++)
    {
      std::vector<int> row;
      for (int j = 0; j < r; j++)
      {
        int value;
        std::cout <<"enter row "<<i+1<<" element "<<j+1<<endl;
        std::cin >>value;
        row.push_back(value);
      }
      matrix.push_back(row);
    }
    return 0;
  }

int showarray()
  {
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < r; j++) {
          std::cout <<matrix[i][j]<<'\t';
          }
        std::cout << '\n';
      }
    return 0;
  }

int diagsum()
  {
      int sum1=0,sum2=0;
      for(int i=0;i<r;i++)
        {
          sum1+=matrix[i][i];
          sum2+=matrix[i][(r-1)-i];
        }
    std::cout<<"\n\nSum of 1st diagonal : "<<sum1;
    std::cout<<"\nSum of 2nd diagonal : "<<sum2;
    return 0;
  }
int main()
  {
    std::cout << "Enter the array :" << '\n';
    getarray();
    std::cout << "\n\nThe array is :\n\n" << '\n';
    showarray();
    diagsum();
    getch();
    return 0;
  }