Wednesday, 11 February 2015

C++ Program for "TIC TAC TOE" game for 2-Players using 2D Array

/*
Program for simple "TIC TAC TOE" game for 2-Players using 2-D Array.
=>Also displays "Invalid Input" message if entered place is already used.
=>Also ask Players for choosing their character\symbol in game.
*/
 
#include<iostream>
#include<iomanip>
using namespace std;
 
//Function Declarations
void displayBoard(char[][3]);
void inputCord(char[][3], char, char);
int checkresult(char[][3], char, char);
 
int main()
{
    char board[3][3] = { '*', '*', '*', '*', '*', '*', '*', '*', '*' };            
    //2-D Array for gameboard
    int result = 0;
    char p1, p2;
 
    //for getting Player symbols
    //for player-1
    do
    {
        cout << "Enter character\\symbol for Player-1 (Except '.'):\t";
        cin >> p1;
        if (p1 == '.')
            cout << "Invalid Input........\n";
    } while (p1 == '.');
    //for player-2
    do
    {
        cout << "Enter character\\symbol for Player-2 (Except '.'):\t";
        cin >> p2; if (p2 == '.')
            cout << "Invalid Input........\n";
    } while (p2 == '.');
 
    //For getting 9 inputs
    for (int i = 1; i <= 9; i++)
    {
        system("cls");                        //Clear's screen after every input
        cout << "Player-1 = " << p1 << "\nPlayer-2 = " << p2 << "\nEnter x and y coordinates
        for choosing cell:\n";    //Displays message
 
        //Displays GameBoard
        displayBoard(board);
 
        //For getting Players input
        inputCord(board, p1, p2);
 
        if (i >= 5)                    //for checking Game Result after 4 - first turns
            result = checkresult(board, p1, p2);
 
        //For checking that either player-1 has won or not
        if (result == 1 || result == -1)
            break;
    }
 
    //for separating RESULT
    for (int i = 0; i < 80; i++)
        cout << "=";
 
    //Displays result
    cout << endl << setw(46) << "GAME RESULT\n";
 
    if (result == 1)
        cout << "\n" << setw(50) << "****Player-1 WON****\n";
    else if (result == -1)
        cout << "\n" << setw(50) << "****Player-2 WON****\n";
    else
        cout << "\n" << setw(50) << "********DRAW********\n";
 
    //Displays Final Board
    displayBoard(board);
 
    //For formatting output
    for (int i = 0; i < 80; i++)
        cout << "~";
 
    cout << endl;
    return 0;
}
 
//For displaying GameBoard
void displayBoard(char board[][3])
{
    cout << endl;
    for (int i = 0; i < 3; i++)
    {
        cout << setw(34);
        for (int j = 0; j < 3; j++)
        {
            cout << board[i][j];
            if(j!=2)
                cout << "  |  ";
        }
        cout << "\n";
        
        if (i!=2)
            cout << setw(34);
 
        for (int k = 1; k <= 13 && i!=2; k++)
            cout << "_";
 
        cout << "\n\n";
    }
}
 
//For getting player input
void inputCord(char board[][3], char p1, char p2)
{
    static int turn = 1;
    static char comp[3][3];
    int x, y;
 
    //For Player-1
    if (turn % 2 == 1)
    {
        do
        {
            cout << "Player-1 turn:\t";
            cin >> x >> y;
            if (x < 0 || y < 0 || x>3 || y>3 || comp[x][y]=='.')
                cout << "Invalid Input......\n";
        } while (x < 0 || y < 0 || x>3 || y>3 || comp[x][y] == '.');
        comp[x][y] = '.';
        board[x][y] = p1;
    }
 
    //For Player-2
    else
    {
        do
        {
            cout << "Player-2 turn:\t";
            cin >> x >> y;
            if (x < 0 || y < 0 || x>3 || y>3 || comp[x][y] == '.')
                cout << "Invalid Input......\n";
        } while (x < 0 || y < 0 || x>3 || y>3 || comp[x][y] == '.');
        comp[x][y] = '.';
        board[x][y] = p2;
    }
 
    turn++;
}
 
//For checking game-result
int checkresult(char board[][3], char p1, char p2)
{
    //For checking that either player-1 has won or not
    if (
        board[0][0] == p1 && board[0][0] == board[0][1] && board[0][1] == board[0][2] ||
        board[1][0] == p1 && board[1][0] == board[1][1] && board[1][1] == board[1][2] ||
        board[2][0] == p1 && board[2][0] == board[2][1] && board[2][1] == board[2][2] ||
        board[0][0] == p1 && board[0][0] == board[1][0] && board[1][0] == board[2][0] ||
        board[0][1] == p1 && board[0][1] == board[1][1] && board[1][1] == board[2][1] ||
        board[0][2] == p1 && board[0][2] == board[1][2] && board[1][2] == board[2][2] ||
        board[0][0] == p1 && board[0][0] == board[1][1] && board[1][1] == board[2][2] ||
        board[0][2] == p1 && board[0][2] == board[1][1] && board[1][1] == board[2][0]
        )
        return 1;            //returns '1' if Player-1 Won
 
    //For checking that either player-2 has won or not if above conditions are false
    else if (
        board[0][0] == p2 && board[0][0] == board[0][1] && board[0][1] == board[0][2] ||
        board[1][0] == p2 && board[1][0] == board[1][1] && board[1][1] == board[1][2] ||
        board[2][0] == p2 && board[2][0] == board[2][1] && board[2][1] == board[2][2] ||
        board[0][0] == p2 && board[0][0] == board[1][0] && board[1][0] == board[2][0] ||
        board[0][1] == p2 && board[0][1] == board[1][1] && board[1][1] == board[2][1] ||
        board[0][2] == p2 && board[0][2] == board[1][2] && board[1][2] == board[2][2] ||
        board[0][0] == p2 && board[0][0] == board[1][1] && board[1][1] == board[2][2] ||
        board[0][2] == p2 && board[0][2] == board[1][1] && board[1][1] == board[2][0]
        )
        return -1;            //returns '-1' if Player-2 Won
 
    else
        return 0;            //returns '0' if GAME DRAW
}
 
OUTPUT

C++ Program for displaying that how many digits of entered number are ODD and how many are EVEN


/*
Program for displaying that how many digits of entered number are ODD and how 
many are EVEN.
*/
 
#include <iostream>
using namespace std;
 
int main()
{
    int num, countO = 0, countE = 0, rem, odd[10], even[10];
    cout << "Enter Number?\t";
    cin >> num;
 
    while (num != 0)
    {
        rem = num % 10;
        if (rem % 2 == 1)
        {
            odd[countO] = rem;
            countO++;
        }
        else
        {
            even[countE] = rem;
            countE++;
        }
        num = num / 10;                        //or n/=10;
    }
 
    cout << "Total ODD digits are " << countO << " which are:\n";
 
    for (int i = countO - 1; i >= 0; i--)
        cout << odd[i] << "\t";
 
    cout << "\nTotal EVEN digits are " << countE << " which are:\n";
 
    for (int i = countE - 1; i >= 0; i--)
        cout << even[i] << "\t";
 
    cout << endl;
    return 0;
}
  OUTPUT



/*
Program for displaying that how many digits of entered number are ODD and how 
many are EVEN.
*/
 
#include <iostream>
using namespace std;
 
int main()
{
    int num, countO = 0, countE = 0, rem;
    cout << "Enter Number?\t";
    cin >> num;
 
    while (num != 0)
    {
        rem = num % 10;
        if (rem % 2 == 1)
        {
            cout << rem << " is an ODD number\n";
            countO++;
        }
        else
        {
            cout << rem << " is an EVEN number\n";
            countE++;
        }
        num = num / 10;                        //or n/=10;
    }
 
    cout << "\nTotal ODD digits are " << countO;
    cout << "\nTotal EVEN digits are " << countE << "\n";
 
    return 0;
}
 OUTPUT
 

C++ Program for Matrix Multiplication

/*
Program for Matrix Multiplication (Max order of matrix is 10x10).
*/
 
#include<iostream>
#include<iomanip>
using namespace std;
 
void inputMatrix(int[][10], int, int);        //For inputting matrix
void displayMatrix(int[][10], int, int);    //For displaying matrix
void size(int &, int &);                    //Gets matrix size
void line();                                //For displaying Horizontal line
 
int main()
{
    int row1, row2, column1, column2, sum = 0;
    int matrix1[10][10], matrix2[10][10], result[10][10];
 
    //For size of Matrix-1
    cout << "Enter number of rows and columns in Matrix-1:\n";
    size(row1, column1);
    
    do
    {
        //For size of Matrix-2
        cout << "\n\nEnter number of rows and columns in Matrix-2:\n";
        size(row2, column2);
 
        if (column1 != row2)
            cout << "Invalid Input........\nNumber of columns of Matrix-1 is not equal to number of rows of Matrix-2\n";
    } while (column1 != row2);
 
    line();
 
    //For Input of Matrix-1
    cout << "Enter elements of Matrix-1:\n\n";
    inputMatrix(matrix1,row1,column1);
 
    line();
 
    //For Input of Matrix-2
    cout << "Enter elements of Matrix-2:\n\n";
    inputMatrix(matrix2, row2, column2);
    
    //For Matrix Multiplication
    for (int r = 0; r < row1; r++)
    {
        for (int c = 0; c < column2; c++)
        {
            sum = 0;
            for (int i = 0; i < column1; i++)
                sum += (matrix1[r][i] * matrix2[i][c]);
            result[r][c] = sum;
        }
    }
    
    system("cls");        //Clears Console
 
    //displays matrix-1
    cout << "Matrix - 1:\n";
    displayMatrix(matrix1, row1, column1);
 
    line();
 
    //displays matrix-2
    cout << "Matrix - 2:\n";
    displayMatrix(matrix2, row2, column2);
 
    //for formatting output
    line();
    for (int i = 0; i < 40; i++)
        cout << "**";
 
    //Displays Multiplication
    cout << "MULTIPLICATION of Matrix - 1 and Matrix - 2 is:\n";
    displayMatrix(result, row1, column2);
 
    cout << endl;
    return 0;
}
 
void size(int &row,int &column)
{
    do
    {
        cout << "Rows (Max 10):\t\t";
        cin >> row;
        if (row > 10 || row < 0)
            cout << "Invalid input.......\n";
    } while (row > 10 || row < 0);
    do
    {
        cout << "Columns (Max 10):\t";
        cin >> column;
        if (column > 10 || column < 0)
            cout << "Invalid input.......\n";
    } while (column > 10 || column < 0);
}
 
//For inputting matrix
void inputMatrix(int matrix[][10], int row, int column)
{
    for (int r = 0; r < row; r++)
    {
        for (int c = 0; c < column; c++)
        {
            cout << "Enter element at index [" << r << "][" << c << "]?\t";
            cin >> matrix[r][c];
        }
    }
}
 
void line()            //Displays horizontal Line
{
    cout << endl;
 
    //for formatting output
    for (int i = 0; i < 80; i++)
        cout << "_";
}
 
void displayMatrix(int matrix[][10], int row, int column)
{
    cout << endl;
 
    for (int r = 0; r < row; r++)
    {
        cout << "\t";
        for (int c = 0; c < column; c++)
            cout << matrix[r][c] << "\t";
        cout << endl<<endl;
    }
}
 
OUTPUT-1
 OUTPUT-2