Jump to content

anyone can explain to me that c code pls step by step.

- - - - -

  • Please log in to reply
3 replies to this topic

#1
zeybekli

zeybekli

    Newbie

  • Members
  • Pip
  • 4 posts
this is the game:: tic tac toe
anyone can explain to me that c code pls. step by step
i need to sort it out :

#include <stdio.h>

#include <stdlib.h>

#include <time.h>


void print_game(char *a, int size, int score_player, int score_computer);

void print_result(int result, int *score_player, int *score_computer);

int check_status(char *a, int size);

void AI_Machine(char *a, int size);


int main()

{

    char a[9];

    int k, ch;

    int size = 3;

    int score_player = 0, score_computer = 0;

    int result;


    srand((unsigned int)time(0));

    

    while (score_player != 5 && score_computer != 5) {

        for (k = 0; k < size * size; k++)

            a[k] = '.';

        

        while (1) {

            //user playing

            print_game(a, size, score_player, score_computer);

            while (1) {

                printf("Secim : ");

                scanf("%d", &ch);

                getchar();

    

                if (a[ch - 1] == 'X' || a[ch - 1] == 'O')

                     printf("Girdiginiz alan dolu...\n");

                else {

                     a[ch - 1] = 'X';

                     break;

                }

            }

            print_game(a, size, score_player, score_computer);

            

            result = check_status(a, size);

            print_result(result, &score_player, &score_computer);

            if (result) {

               print_game(a, size, score_player, score_computer); 

               break;

            }

               

               

            //pc playing

            AI_Machine(a, 3);

            print_game(a, size, score_player, score_computer);

           

            result = check_status(a, size);

            print_result(result, &score_player, &score_computer);

            if (result) {

               print_game(a, size, score_player, score_computer); 

               break;

            }

        }

        

    }

    

    if (score_player == 5) {

        printf("\n\nSONUC : %d - %d kazandin...\n", score_player, score_computer);

        getchar();

        exit(EXIT_SUCCESS);

    }

    else {

        printf("\n\nSONUC : %d - %d kaybettin...\n", score_player, score_computer);

        getchar();

        exit(EXIT_SUCCESS);

    }

}


void print_game(char *a, int size, int score_player, int score_computer)

{

    int k;


    system("cls");

    printf("Tic Tac Toe\n");

    printf("===========\n");

    printf("Sen : %d - Bilgisayar : %d\n\n", score_player, score_computer);

    printf("-------------------------------------\n");


    for (k = 0; k < size * size; k++) {

        if (k % size == 0)

            printf("\n\n");


        printf("%-10c ", a[k]);

    }


    printf("\n\n-------------------------------------\n");


}


void print_result(int result, int *score_player, int *score_computer)

{

    if (result == 1) {

        printf("Tebrikler kazandiniz...\n");

        getchar();

        (*score_player)++;

    }

    else if (result == 2) {

        printf("Uzgunum kaybettiniz...\n");

        getchar();

        (*score_computer)++;

    }

    else if (result == 3) {

        printf("Oyun berabere bitti...\n");

        getchar();

    }

    else

        ;

}


int check_status(char *a, int size)

{


    int line, col;

    int counterX, counterO;

    int n_time;

    int n2_time;

    int k;



    //horizontel

    n_time = size;

    for (line = 1 ; n_time-- ; line += size) {

        counterX = 0, counterO = 0;

        n2_time = size;

        for (col = line; n2_time-- ; col++) {

            switch (a[col - 1]) {

                    case 'X' : counterX++;

                                if (counterX == size)

                                    return 1;

                                break;

                    case 'O' : counterO++;

                                if (counterO == size)

                                    return 2;

                                break;

            }

        }

    }



    //vertical

    n_time = size;

    for (col = 1 ; n_time-- ; col++) {

        counterX = 0, counterO = 0;

        n2_time = size;

        for (line = col; n2_time-- ; line += size) {

            switch (a[line - 1]) {

                    case 'X' : counterX++;

                                if (counterX == size)

                                    return 1;

                                break;

                    case 'O' : counterO++;

                                if (counterO == size)

                                    return 2;

                                break;

            }

        }

    }



    //left to right

    n_time = size;

    counterX = 0, counterO = 0;

    for (line = 1; n_time-- ; line += size + 1) {

        switch (a[line - 1]) {

                case 'X' : counterX++;

                           if (counterX == size)

                              return 1;

                           break;

                case 'O' : counterO++;

                           if (counterO == size)

                              return 2;

                           break;

        }

    }



    //right to left

    n_time = size;

    counterX = 0, counterO = 0;

    for (line = size; n_time-- ; line += size - 1) {

        switch (a[line - 1]) {

                case 'X' : counterX++;

                            if (counterX == size)

                                return 1;

                            break;

                case 'O' : counterO++;

                            if (counterO == size)

                                return 2;

                            break;

        }

    }


    //game finish ?

    for (k = 0; k < size * size; k++)

        if (a[k] == '.')

            return 0;


    //even situation

    return 3;



}


void AI_Machine(char *a, int size)

{

 int line, col;

 int n_time, n2_time;

 int counter;

 int random;


 //Ozel Stratejiler

 if (size == 3 && a[size + 1] == '.') {

     a[size + 1] = 'O';

     return ;

 }


 //strake

 //horizontel

 n_time = size;

 for (line = 1 ; n_time--; line += size) {

     counter = 0, n2_time = size;



     for (col = line; n2_time--; col++) {

         if (a[col - 1] == 'O') {

             counter++;

             continue;

         }

         if (a[col - 1] == 'X')

             counter--;

     }


     if (counter == size - 1) {

         col = line - 1;

         while (a[col] != '.')

             col++;

         a[col] = 'O';

         return ;

     }

 }


 //vertical

 n_time = size;

 for (col = 1 ; n_time-- ; col++) {

     counter = 0, n2_time = size;


     for (line = col; n2_time-- ; line += size) {

         if (a[line - 1] == 'O') {

             counter++;

             continue;

         }

         if (a[line - 1] == 'X')

             counter--;

     }


     if (counter == size - 1) {

         line = col - 1;

         while (a[line] != '.')

             line += size;

         a[line] = 'O';

         return ;

     }

 }



 //left to right

 counter = 0, n2_time = size;

 for (col = 1; n2_time--; col += size + 1) {

     if (a[col - 1] == 'O') {

         counter++;

         continue;

     }

     if (a[col - 1] == 'X')

         counter--;

 }


 if (counter == size - 1) {

     col = 1;

     while (a[col - 1] != '.')

         col += size + 1;

     a[col - 1] = 'O';

     return ;

 }


 //right to left

 counter = 0, n2_time = size;

 for (col = size; n2_time--; col += size - 1) {

     if (a[col - 1] == 'O') {

         counter++;

         continue;

     }

     if (a[col - 1] == 'X')

         counter--;

 }

 if (counter == size - 1) {

     col = size ;

     while (a[col - 1] != '.')

         col += size - 1;

     a[col - 1] = 'O';

     return ;

 }



 //defence strategy

 //horisontel

 n_time = size;

 for (line = 1 ; n_time--; line += size) {

     counter = 0, n2_time = size;


     for (col = line; n2_time--; col++) {

         if (a[col - 1] == 'X') {

             counter++;

             continue;

         }

         if (a[col - 1] == 'O')

             counter--;

     }


     if (counter == size - 1) {

         col = line - 1;

         while (a[col] != '.')

             col++;

         a[col] = 'O';

         return ;

     }

 }


 //buttom to up

 n_time = size;

 for (col = 1 ; n_time-- ; col++) {


     counter = 0, n2_time = size;


     for (line = col; n2_time-- ; line += size) {

         if (a[line - 1] == 'X') {

             counter++;

             continue;

         }

         if (a[line - 1] == 'O')

             counter--;

     }


     if (counter == size - 1) {

         line = col - 1;

         while (a[line] != '.')

             line += size;

         a[line] = 'O';

         return ;

     }

 }



 //from left to right

 counter = 0, n2_time = size;

 for (col = 1; n2_time--; col += size + 1) {

     if (a[col - 1] == 'X') {

         counter++;

         continue;

     }

     if (a[col - 1] == 'O')

         counter--;

 }


 if (counter == size - 1) {

     col = 1;

     while (a[col - 1] != '.')

         col += size + 1;

     a[col - 1] = 'O';

     return ;

 }


 //control r

 counter = 0, n2_time = size;

 for (col = size; n2_time--; col += size - 1) {

     if (a[col - 1] == 'X') {

         counter++;

         continue;

     }

     if (a[col - 1] == 'O')

         counter--;

 }


 if (counter == size - 1) {

     col = size ;

     while (a[col - 1] != '.')

         col += size - 1;

     a[col - 1] = 'O';

     return ;

 }




 //random choose

 while (a[random = rand() % (size * size)] != '.')

     ;

 a[random] = 'O';

}


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
I'm guessing you got this code from someplace else. Have you traced through it by hand? What parts don't make sense?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
psepheroth

psepheroth

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
What language is this using? I mean, not the C language, but the language inside the printf.
It would be helpful too if it's on english language. Can you translate that?

#4
CurlyBonesHopkins

CurlyBonesHopkins

    Newbie

  • Members
  • Pip
  • 7 posts
Did you eventually find out how this code works?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users