Friday 5 June 2015

TicTacToe in C++

//Nadia baig

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

using namespace std;


void tictac(char board[][3]);
char Winner(char board[][3]);

int main(){
    char board[3][3]={{'*','*','*'},{'*','*','*'},{'*','*','*'}};
    bool win = false;
    bool in_row = false;
    bool in_col = false;
    int i;
    int j;




// run a loop:
    while (!win) {
   
        tictac(board); /// displays board
        cout << "Player 1's turn:";
        cout << "enter row to insert value\n";
        cin >>i;
        cout << "enter coloumn\n";
        cin >> j;
    // check if move is valid
        bool valid = false;
        while (!valid)
        {
            //check if the move is in range.
            if(!(i <= 3 && i >= 1 && j<= 3 && j >= 1)){
                cout << "Error: value inserted greater than size of matrix\n";
                // ask again (error: row in column not within range)
                cout << "enter row:?\n";
                cin >> i;
                cout << "enter column?\n";
                cin >> j;
            }
            else if(board[i-1][j-1] != '*')
            {
                cout << "Error: Position is occupied already\n";
                cout << "enter row:?\n";
                cin >> i;
                cout << "enter/ column?\n";
                cin >> j;
            }else {
                valid = true;
            }


        }
        board[i-1][j-1] = 'X';
        tictac(board);
    // Check if someone won or  if there is a tie
        Winner(board);
    // Ask player two to chose a location (row, column)
    cout << "Player 2's turn:";

    cout << "enter row:?\n";
                cin >> i;
                cout << "enter column?\n";
                cin >> j;
                // check if move is valid

               
                valid = false;
                while (!valid)
                {
                    //check if the move is within range.
                    if(!(i <= 3 && i >= 1 && j <= 3 && j >= 1)){
                        cout << "Error: value entered is not in range.\n";
                   
                        cout << "enter row:?\n";
                        cin >> i;
                        cout << "enter column?\n";
                        cin >> j;
                    }
                    else if(board[i-1][j-1] != '*')
                    {
                        cout << "Error: position is already occupied.\n";
                        cout << "enter row:?\n";
                        cin >> i;
                        cout << "enter column?\n";
                        cin >> j;
                    }else
                    {
                        valid = true;
                    }
                    board[i-1][j-1] = 'O';
                }
        tictac(board);
    // Check if someone won or  if there is a tie
        Winner(board);

    }

   

    return 0;
}
char Winner(char board[][3]){
    char winner = 'T';

    // horizontal:
    for (int i = 0; i < 3; i++)
        if (board[i][0] == board[i][1] && board[i][1] == board[i][2])
            winner = board[i][0];

    //  vertical:
    for (int i = 0; i < 3; i++)
        if (board[0][i] == board[1][i] && board[1][i] == board[2][i])
            winner = board[0][1];

    // diagnol:
    if ((board[0][0] == board[1][1] && board[1][1] == board[2][2]) ||
        (board[0][2] == board[1][1] && board[1][1] == board[2][0]))
        winner = board[1][1];

    //  result:
    switch (winner) {
        case 'T': cout << " TIE"; break;
        case 'X': cout << "PLAYER 1 WON! congratulations"; break;
        case 'O': cout << "PLAYER 2 WON! congratulations";break;
        default : cout << "NEXT PLAYER'S TURN...>";
    }
    return winner;
}
void tictac(char board[][3])
{

    cout << "     1   2   3" << endl;
    cout << "   +---+---+---+" << endl;

    cout << " 1" << " | " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << " | " << endl;
    cout << "   +---+---+---+" << endl;

    cout << " 2" << " | " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << " | " << endl;
    cout << "   +---+---+---+" << endl;

    cout << " 3" << " | " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << " | " << endl;
    cout << "   +---+---+---+" << endl;

}

No comments:

Post a Comment