Jump to content

Joculet X Si O In C++


Courage

Recommended Posts

  • Moderators

L-am facut azi intr-o ora.. N-am stat sa verific daca merge 100% :) postati aici daca aveti probleme.

 

REYhYWI.png

 

Download: http://www8.zippyshare.com/v/79381409/file.html

 

Codul sursa:

 

#include <iostream>
#include <cstdlib>
#define DIM 3

using std :: cin;
using std :: cout;

char TABLA[DIM][DIM] = {
    {49, 50, 51},
    {52, 53, 54},
    {55, 56, 57}
};

class tic_tac_toe
{
    public:

        /**
         * Functia principala
         * void
         */
        void main();

    private:

        /**
         * Afiseaza tabla pe ecran
         * void
         */
         void afisare_tabla();

        /**
         * Verifica daca toata tabla a fost completata cu X si O
         * bool
         * @return true | false
         */
        bool runda_terminata();

        /**
         * Verifica daca coordonata transmisa este deja completata cu X sau O
         * @param int coordonata
         * @return true | false
         */
        bool deja_completat(int);

        /**
         * Completeaza tabloul pe ordoata data
         * @param int coordonata
         * @param jucator: 1 = X ; 2 = O
         * void
        */
        void completare_tabla(int, int);

        /**
         * Returneaza daca runda a fost castigata sau e remiza
         * @return : 0 - remiza
         *           1 - a castigat jucaoturl 1(X)
         *           2 - a castigat jucatorul 2(O)
         */
        int rezultat_runda();


};

int main (void)
{
    tic_tac_toe program;
    program.main();
    system("PAUSE");
    return 0;
}


void tic_tac_toe :: main()
{
    int i, j, coordonata, jucator = 1;
    inceput_joc:
    while (!runda_terminata() and !rezultat_runda())
    {
        cout << "X si Onn";

        cout << "Jucator 1(X) - Jucator2 (O)nn";

        afisare_tabla();

        if (jucator % 2)
        {
            cout <<"nnJUCATOR 1 (X)nnIntroduceti coordonata: ";

        }
        else
        {
            cout <<"nnJUCATOR 2 (O)nnIntroduceti coordonata: ";
        }

        cin >> coordonata;

        if (coordonata < 1 or coordonata > 9)
        {
            while(coordonata < 1 or coordonata > 9)
            {
                cout << "Coordonata trebuie sa fie intre 1 si 9";
                cin >> coordonata;
            }
        }

        if (deja_completat(coordonata))
        {
            while(!deja_completat(coordonata))
            {
                cout << "Deja Completat! Introduceti o alta coordonata: ";
                cin >> coordonata;
            }
        }

        completare_tabla(coordonata, jucator % 2);

        jucator++;

        system("CLS");
    }

    cout << "RUNDA TERMINATA!nnnn";
    int rezultat = rezultat_runda();
    switch(rezultat)
    {
        case 0:
            cout << "REMIZA!nn";
            break;
        case 1:
            cout << "JUCATORUL 1(X) A CASTIGAT!nn";
            break;
        case 2:
            cout << "JUCATORUL 2(O) A CASTIGAT!nn";
            break;
        default:
            break;
    }
}


void tic_tac_toe :: afisare_tabla()
{
    int i, j;
    cout << "-------------------------n";
    for (i=0 ; i < DIM ; i++)
    {
        cout << "|       |       |       |n";
        cout << "|";
        for (j=0 ; j < DIM ; j++)
        {
            cout << "   " << TABLA[i][j] << "   |";
        }
        cout << "n";
        cout << "|       |       |       |n";
        cout << "-------------------------n";
    }

}


bool tic_tac_toe :: runda_terminata()
{
    int ordine = 48, i, j;

    for (i=0 ; i < 3 ; i++)
    {
        for (j=0 ; j < 3 ; j++)
        {
            if (TABLA[i][j] == ++ordine)
                return false;
        }
    }

    return true;
}


bool tic_tac_toe :: deja_completat(int coordonata)
{
    switch(coordonata)
    {
        case 1:
            if (TABLA[0][0] != 49)
                return false;
            break;
        case 2:
            if (TABLA[0][1] != 50)
                return false;
            break;
        case 3:
            if (TABLA[0][2] != 51)
                return false;
            break;
        case 4:
            if (TABLA[1][0] != 52)
                return false;
            break;
        case 5:
            if (TABLA[1][1] != 53)
                return false;
            break;
        case 6:
            if (TABLA[1][2] != 54)
                return false;
            break;
        case 7:
            if (TABLA[2][0] != 55)
                return false;
            break;
        case 8:
            if (TABLA[2][1] != 56)
                    return false;
            break;
        case 9:
            if (TABLA[2][2] != 57)
                return false;
            break;
    }

    return true;
}

void tic_tac_toe :: completare_tabla(int coordonata, int jucator)
{
    switch(coordonata)
    {
        case 1:
            TABLA[0][0] = (jucator == 1) ? 88 : 79;
            break;
        case 2:
            TABLA[0][1] = (jucator == 1) ? 88 : 79;
            break;
        case 3:
            TABLA[0][2] = (jucator == 1) ? 88 : 79;
            break;
        case 4:
            TABLA[1][0] = (jucator == 1) ? 88 : 79;
            break;
        case 5:
            TABLA[1][1] = (jucator == 1) ? 88 : 79;
            break;
        case 6:
            TABLA[1][2] = (jucator == 1) ? 88 : 79;
            break;
        case 7:
            TABLA[2][0] = (jucator == 1) ? 88 : 79;
            break;
        case 8:
            TABLA[2][1] = (jucator == 1) ? 88 : 79;
            break;
        case 9:
            TABLA[2][2] = (jucator == 1) ? 88 : 79;
            break;
    }
}



int tic_tac_toe :: rezultat_runda()
{
    int i, j;
    // Pe orizontala
    for (i=0 ; i < DIM ; i++)
    {
        if (TABLA[i][0] == 'X' and TABLA[i][1] == 'X' and TABLA[i][2] == 'X')
        {
            return 1;
        }
        if (TABLA[i][0] == 'O' and TABLA[i][1] == 'O' and TABLA[i][2] == 'O')
        {
            return 2;
        }
    }

    // Pe verticala
    for (j=0 ; j < DIM ; j++)
    {
        if (TABLA[0][j] == 'X' and TABLA[1][j] == 'X' and TABLA[2][j] == 'X')
        {
            return 1;
        }
        if (TABLA[0][j] == 'O' and TABLA[1][j] == 'O' and TABLA[2][j] == 'O')
        {
            return 2;
        }
    }

    // Diagonala principala
    if (TABLA[0][0] == 'X' and TABLA[1][1] == 'X' and TABLA[2][2] == 'X')
    {
        return 1;
    }
    if (TABLA[0][0] == 'O' and TABLA[1][1] == 'O' and TABLA[2][2] == 'O')
    {
        return 2;
    }

    // Diagonala secundara
    if (TABLA[0][2] == 'X' and TABLA[1][1] == 'X' and TABLA[2][0] == 'X')
    {
        return 1;
    }
    if (TABLA[0][2] == 'O' and TABLA[1][1] == 'O' and TABLA[2][0] == 'O')
    {
        return 2;
    }

    return 0;
}

  • Upvote 4
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.