Thread: can you check my code....

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    7

    can you check my code....

    I'm new to c programming,and this is my first project....can you guys check out if everything is fine...???
    please give any suggestions to make it better.


    Code:
    #include <stdio.h>
    #include <time.h>
    
    
    char square[10] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    int choice,stts=0;
    int end_turn,choice_ai=0;
    int r;
    void board();
    void pc();
    void user();
    void srand();
    void win();
    int rand();
    int system();
    int level_error=1;
    void pc_easy();
    void pc_hard();
    void level_select();
    char level;
    
    
    
    
    
    
    int main()
    
    
    
    
    {
    
    
    
    
        level_select();
    
    
        while(stts==0) //loop while the game has not won or lost
        {
            board();//draw board
            user(); //human player turn
            win(); //check win
            if (level=='1')
            {
                 pc_easy();//easy computer player
            }
            else if (level=='2')
            {
                pc_hard(); //hard computer player
            }
    
    
    
    
            board(); //draw board
            win(); //check win
    
    
        }
    
    
     }
    
    
    
    
    
    
    void level_select()
        {
            printf("\n----TIC-TAC-TOE-----------------------------------------------------------------\n\nchoose level\n\n1.easy\n2.hard\n");
    while (level_error==1)
    {
        scanf("%c",&level);
        if (level=='1' || level=='2')
        {
            level_error=0;
        }
    
    
        else
        {
            printf("\ninvalid selection\n\n");
            printf("choose level\n1.easy\n2.hard\n");
        }
    
    
    }
    
    
        }
    
    
    
    
    
    
    
    
    
    
    void win() //conditions to win
    {
    
    
        if (square[1] == square[2] && square[2] == square[3])
            {
                stts=1;
                if(square[1] == 'X' && square[2] == 'X' && square[3]=='X')
                {
                    printf("you win\n");
    
    
                }
    
    
                else
                    printf("you loose\n");
            }
    
    
    
    
    
    
        else if (square[4] == square[5] && square[5] == square[6])
           {
                stts=1;
                if(square[4] == 'X' && square[5] == 'X' && square[6]=='X')
                {
                    printf("you win\n");
    
    
                }
    
    
                else
                    printf("you loose\n");
            }
    
    
    
    
    
    
        else if (square[7] == square[8] && square[8] == square[9])
            {
                stts=1;
                if(square[7] == 'X' && square[8] == 'X' && square[9]=='X')
                {
                    printf("you win\n");
    
    
                }
    
    
                else
                    printf("you loose\n");
            }
    
    
    
    
    
    
        else if (square[1] == square[4] && square[4] == square[7])
            {
                stts=1;
                if(square[1] == 'X' && square[4] == 'X' && square[7]=='X')
                {
                    printf("you win\n");
    
    
                }
    
    
                else
                    printf("you loose\n");
            }
    
    
    
    
    
    
        else if (square[2] == square[5] && square[5] == square[8])
            {
                stts=1;
                if(square[2] == 'X' && square[5] == 'X' && square[8]=='X')
                {
                    printf("you win\n");
    
    
                }
    
    
                else
                    printf("you loose\n");
            }
    
    
    
    
    
    
        else if (square[3] == square[6] && square[6] == square[9])
            {
                stts=1;
                if(square[3] == 'X' && square[6] == 'X' && square[9]=='X')
                {
                    printf("you win\n");
    
    
                }
    
    
                else
                    printf("you loose\n");
            }
    
    
    
    
    
    
        else if (square[1] == square[5] && square[5] == square[9])
            {
                stts=1;
                if(square[1] == 'X' && square[5] == 'X' && square[9]=='X')
                {
                    printf("you win\n");
    
    
                }
    
    
                else
                    printf("you loose\n");
            }
    
    
    
    
    
    
        else if (square[3] == square[5] && square[5] == square[7])
            {
                stts=1;
                if(square[3] == 'X' && square[5] == 'X' && square[7]=='X')
                {
                    printf("you win\n");
    
    
                }
    
    
                else
                    printf("you loose\n");
            }
    
    
        //condition to draw
        else if (square[1] != '1' && square[2] != '2' && square[3] != '3' &&
            square[4] != '4' && square[5] != '5' && square[6] != '6' && square[7]
            != '7' && square[8] != '8' && square[9] != '9')
               {
                   printf("draw\n");
    
    
            stts=2;
               }
        else //else play on
            stts=0;
    }
    
    
    
    
    
    
    void user() //human player input
    {
           while (end_turn==0) //loop until the turn ends
        {
    
    
            printf("Your Move\n");
            scanf("%d",&choice); //get input
    
    
            if (choice<10 && choice>0) //checks if the number is between 1-9
            {
                if (square[choice]=='X' || square[choice]=='O') //check if the square is taken or not
                    {
                        printf("invalid move\n"); //if taken print invalid move
                        end_turn=0; // turn do not end(go back to the input stage)
                    }
                else
                {
                    square[choice]='X'; //if the choice is available,assign X to the chosen square
                    end_turn=1; //end turn
                }
            }
            else
            {
    
    
                printf("invalid Move\n\n"); // if the choice is out of range,print invalid and re-enter
    
    
            }
        }
        board();
    
    
    }
    
    
    
    
    
    
    void pc_easy() //computer player moves
    {
        srand((unsigned)time(NULL)); // to get random numbers
    
    
        while (end_turn==1) //loop while the turn ends
        {
    
    
    
    
            r=rand();
            choice_ai=(r % 9)+1; //random choice of the computer player
    
    
    
    
            if (square[choice_ai]=='X' || square[choice_ai]=='O')  //checks if the choice is taken
                {
                    end_turn=1; // if taken choose again
                }
            else
            {
                square[choice_ai]='O';  //if not taken assign O to the chosen square
                end_turn=0 ;//end turn
            }
    
    
    
    
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    void pc_hard() //computer player moves
    {
        srand((unsigned)time(NULL)); // to get random numbers
    
    
    
    
        //winning moves
    
    
    //first row
        if (square[1]=='O' && square[2]=='O' && square[3]=='3')
        {
            square[3]='O';
            end_turn=0;
    
    
        }
    
    
       else if (square[2]=='O' && square[3]=='O' && square[1]=='1')
        {
            square[1]='O';
            end_turn=0;
        }
    
    
        else if (square[1]=='O' && square[3]=='O' && square[2]=='2')
        {
            square[2]='O';
            end_turn=0;
        }
    
    
    
    
    
    
    //first column
        else if (square[1]=='O' && square[4]=='O' && square[7]=='7')
        {
            square[7]='O';
            end_turn=0;
        }
    
    
        else if (square[1]=='O' && square[7]=='O' && square[4]=='4')
        {
            square[4]='O';
            end_turn=0;
        }
    
    
        else if (square[4]=='O' && square[7]=='O' && square[1]=='1')
        {
            square[1]='O';
            end_turn=0;
        }
    
    
    
    
    //second row
        else if (square[4]=='O' && square[5]=='O' && square[6]=='6')
        {
            square[6]='O';
            end_turn=0;
        }
    
    
        else if (square[4]=='O' && square[6]=='O' && square[5]=='5')
        {
            square[5]='O';
            end_turn=0;
        }
    
    
        else if (square[5]=='O' && square[6]=='O' && square[4]=='4')
        {
            square[4]='O';
            end_turn=0;
        }
    
    
    //second column
        else if (square[2]=='O' && square[5]=='O' && square[8]=='8')
        {
            square[8]='O';
            end_turn=0;
        }
    
    
        else if (square[2]=='O' && square[8]=='O' && square[5]=='5')
        {
            square[5]='O';
            end_turn=0;
        }
    
    
        else if (square[8]=='O' && square[5]=='O' && square[2]=='2')
        {
            square[2]='O';
            end_turn=0;
        }
    
    
        //third column
        else if (square[3]=='O' && square[6]=='O' && square[9]=='9')
        {
            square[9]='O';
            end_turn=0;
        }
    
    
        else if (square[3]=='O' && square[9]=='O' && square[6]=='6')
        {
            square[6]='O';
            end_turn=0;
        }
    
    
        else if (square[9]=='O' && square[6]=='O' && square[3]=='3')
        {
            square[3]='O';
            end_turn=0;
        }
    
    
    
    
        //third row
        else if (square[9]=='O' && square[8]=='O' && square[7]=='7')
        {
            square[7]='O';
            end_turn=0;
        }
    
    
        else if (square[9]=='O' && square[7]=='O' && square[8]=='8')
        {
            square[8]='O';
            end_turn=0;
        }
    
    
        else if (square[8]=='O' && square[7]=='O' && square[9]=='9')
        {
            square[9]='O';
            end_turn=0;
        }
    
    
    
    
        //across
        else if (square[1]=='O' && square[5]=='O' && square[9]=='9')
        {
            square[9]='O';
            end_turn=0;
        }
    
    
        else if (square[1]=='O' && square[9]=='O' && square[5]=='5')
        {
            square[5]='O';
            end_turn=0;
        }
    
    
        else if (square[5]=='O' && square[9]=='O' && square[1]=='3')
        {
            square[1]='O';
            end_turn=0;
        }
    
    
        //across 2
        else if (square[3]=='O' && square[5]=='O' && square[7]=='7')
        {
            square[7]='O';
            end_turn=0;
        }
    
    
        else if (square[3]=='O' && square[7]=='O' && square[5]=='5')
        {
            square[5]='O';
            end_turn=0;
        }
    
    
        else if (square[7]=='O' && square[5]=='O' && square[3]=='3')
        {
            square[3]='O';
            end_turn=0;
        }
    
    
    
    
    
    
        //defence
    
    
        //first row
        if (square[1]=='X' && square[2]=='X' && square[3]=='3')
        {
            square[3]='O';
            end_turn=0;
    
    
        }
    
    
       else if (square[2]=='X' && square[3]=='X' && square[1]=='1')
        {
            square[1]='O';
            end_turn=0;
        }
    
    
        else if (square[1]=='X' && square[3]=='X' && square[2]=='2')
        {
            square[2]='O';
            end_turn=0;
        }
    
    
    
    
    
    
    //first column
        else if (square[1]=='X' && square[4]=='X' && square[7]=='7')
        {
            square[7]='O';
            end_turn=0;
        }
    
    
        else if (square[1]=='X' && square[7]=='X' && square[4]=='4')
        {
            square[4]='O';
            end_turn=0;
        }
    
    
        else if (square[4]=='X' && square[7]=='X' && square[1]=='1')
        {
            square[1]='O';
            end_turn=0;
        }
    
    
    
    
    //second row
        else if (square[4]=='X' && square[5]=='X' && square[6]=='6')
        {
            square[6]='O';
            end_turn=0;
        }
    
    
        else if (square[4]=='X' && square[6]=='X' && square[5]=='5')
        {
            square[5]='O';
            end_turn=0;
        }
    
    
        else if (square[5]=='X' && square[6]=='X' && square[4]=='4')
        {
            square[4]='O';
            end_turn=0;
        }
    
    
    //second column
        else if (square[2]=='X' && square[5]=='X' && square[8]=='8')
        {
            square[8]='O';
            end_turn=0;
        }
    
    
        else if (square[2]=='X' && square[8]=='X' && square[5]=='5')
        {
            square[5]='O';
            end_turn=0;
        }
    
    
        else if (square[8]=='X' && square[5]=='X' && square[2]=='2')
        {
            square[2]='O';
            end_turn=0;
        }
    
    
        //third column
        else if (square[3]=='X' && square[6]=='X' && square[9]=='9')
        {
            square[9]='O';
            end_turn=0;
        }
    
    
        else if (square[3]=='X' && square[9]=='X' && square[6]=='6')
        {
            square[6]='O';
            end_turn=0;
        }
    
    
        else if (square[9]=='X' && square[6]=='X' && square[3]=='3')
        {
            square[3]='O';
            end_turn=0;
        }
    
    
    
    
        //third row
        else if (square[9]=='X' && square[8]=='X' && square[7]=='7')
        {
            square[7]='O';
            end_turn=0;
        }
    
    
        else if (square[9]=='X' && square[7]=='X' && square[8]=='8')
        {
            square[8]='O';
            end_turn=0;
        }
    
    
        else if (square[8]=='X' && square[7]=='X' && square[9]=='9')
        {
            square[9]='O';
            end_turn=0;
        }
    
    
    
    
        //across
        else if (square[1]=='X' && square[5]=='X' && square[9]=='9')
        {
            square[9]='O';
            end_turn=0;
        }
    
    
        else if (square[1]=='X' && square[9]=='X' && square[5]=='5')
        {
            square[5]='O';
            end_turn=0;
        }
    
    
        else if (square[5]=='X' && square[9]=='X' && square[1]=='3')
        {
            square[1]='O';
            end_turn=0;
        }
    
    
        //across 2
        else if (square[3]=='X' && square[5]=='X' && square[7]=='7')
        {
            square[7]='O';
            end_turn=0;
        }
    
    
        else if (square[3]=='X' && square[7]=='X' && square[5]=='5')
        {
            square[5]='O';
            end_turn=0;
        }
    
    
        else if (square[7]=='X' && square[5]=='X' && square[3]=='3')
        {
            square[3]='O';
            end_turn=0;
        }
    
    
        else
        {
    
    
            while (end_turn==1) //loop while the turn ends
            {
    
    
    
    
                r=rand();
                choice_ai=(r % 9)+1; //random choice of the computer player
    
    
    
    
                if (square[choice_ai]=='X' || square[choice_ai]=='O')  //checks if the choice is taken
                    {
                        end_turn=1; // if taken choose again
                    }
                else
                {
                    square[choice_ai]='O';  //if not taken assign O to the chosen square
                    end_turn=0 ;//end turn
                }
    
    
            }
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    void board() //drawing the board.
    {
    system("cls");//clear screen
    
    
        printf("\n----TIC-TAC-TOE-----------------------------------------------------------------\n");
        printf("\nPLAYER : X\nCOMPUTER : O\n\n");
       if(level=='1')
            printf("DIFFICULTY = EASY\n\n\n");
       else if (level=='2')
            printf("DIFFICULTY = HARD\n\n\n");
    
    
        printf("     |     |     \n");
        printf("  %c  |  %c  |  %c \n", square[1], square[2], square[3]);
    
    
        printf("_____|_____|_____\n");
        printf("     |     |     \n");
    
    
        printf("  %c  |  %c  |  %c \n", square[4], square[5], square[6]);
    
    
        printf("_____|_____|_____\n");
        printf("     |     |     \n");
    
    
        printf("  %c  |  %c  |  %c \n", square[7], square[8], square[9]);
    
    
        printf("     |     |     \n\n");
    
    
    
    
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Do not declare prototypes of standard library functions; instead, include the correct header.

    Code:
    void board();
    void pc();
    void user();
    void srand();
    void win();
    int rand();
    int system();
    void pc_easy();
    void pc_hard();
    void level_select();
    And, in your function prototypes do include the parameters.
    If no parameters, use the "void" keyword to show that.

    Tim S.
    Last edited by stahta01; 03-19-2018 at 08:43 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Mar 2018
    Posts
    7
    hi,

    you mean that I should not declare
    Code:
    charsquare[10] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9'};int choice,stts=0;
    int end_turn,choice_ai=0;
    intr;
    before int main()??

    please be kind enough to explain if you don't mind.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Please read up on what a function prototype is!

    Then remove the function prototypes for system, srand, and rand!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by achi View Post
    hi,

    you mean that I should not declare
    Code:
    char square[10] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };int choice,stts=0;
    int end_turn,choice_ai=0;
    int r;
    You should also avoid using global variables. If there is a game state, you should pass it around as a parameter to your functions. You should also avoid using non-descriptive variable names like "stts" and "r."
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    Mar 2018
    Posts
    7
    aah got it... thanks mate....thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check my code.
    By Mr.Lnx in forum C Programming
    Replies: 10
    Last Post: 10-19-2012, 03:59 PM
  2. Help! Check my code over.
    By omGeeK in forum C Programming
    Replies: 6
    Last Post: 11-04-2010, 11:34 PM
  3. Code Check
    By Dae in forum C++ Programming
    Replies: 12
    Last Post: 01-08-2009, 07:01 PM
  4. Check out my code
    By joshdick in forum C++ Programming
    Replies: 9
    Last Post: 09-17-2003, 07:41 PM
  5. check this code out
    By debuger2004 in forum C Programming
    Replies: 25
    Last Post: 06-21-2003, 12:24 PM

Tags for this Thread