Thread: can you gut

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

    can you gut

    I'm new to C programming and this is my first project,can you guys point out if there are any mistakes??




    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
    Jun 2009
    Posts
    120
    Quote Originally Posted by achi View Post
    Code:
    void srand();
    int rand();
    int system();
    You should include stdlib.h instead of declaring standard library functions on your own.

  3. #3
    Guest
    Guest
    You also shouldn't call srand() more than once (usually at the start of your program). It's supposed to seed your random number generator for the rand() calls that follow.

    Lastly, your indentation and spacing could be a lot cleaner. There is no point in leaving huge empty spaces between functions and blocks of code. It makes things harder to read, not easier.
    Last edited by Guest; 03-20-2018 at 06:24 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I thought this thread was about fish preparation
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    It almost works. But there is an infinite input loop if the game ends in a draw. In main(), you can move the first call to board() before the while loop. And you should skip over the computer player's turn if the user has already won. You can improve this program a lot by learning about scope of variables. The user interface would have been better if you had made the numbers match a numeric keypad with the 7 8 9 on top.

  6. #6
    Registered User
    Join Date
    Mar 2018
    Posts
    7
    Quote Originally Posted by Salem View Post
    I thought this thread was about fish preparation
    Lol,that was a mistake....so i made another post with the correct topic

  7. #7
    Registered User
    Join Date
    Mar 2018
    Posts
    7
    Quote Originally Posted by christophergray View Post
    It almost works. But there is an infinite input loop if the game ends in a draw. In main(), you can move the first call to board() before the while loop. And you should skip over the computer player's turn if the user has already won. You can improve this program a lot by learning about scope of variables. The user interface would have been better if you had made the numbers match a numeric keypad with the 7 8 9 on top.
    It ends well if it draws...but it goes to infinite loop when you insert a character.....
    how can I fix it??
    and sometimes it asks to press any key to exit when won,lost or drawn...but sometimes it just says you won,lost,draw and it wont quit....

  8. #8
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    I just did some general cleanup and it now works for me. I think making "level" into a local variable did it.
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    static char square[10] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    static int end_turn;
    static void board();
    static void user();
    static void pc_easy();
    static void pc_hard();
    static void pc(_Bool);
    static _Bool level_select();
    enum game_status {in_play, won_or_lost, is_draw};
    static enum game_status win();
    
    int main(void) {
      _Bool is_hard = level_select();
      enum game_status stts = in_play;
      srand((unsigned)time(NULL)); // to get random numbers
      board(); //draw board
      while(in_play == stts) { //loop while the game has not won or lost
        if (is_hard)
          printf("DIFFICULTY = HARD\n");
        else
          printf("DIFFICULTY = EASY\n");
        user(); //human player turn
        if (in_play == (stts=win())) //check win
          pc(is_hard);
        board(); //draw board
        stts=win(); //check win
      }
    }
    
    void pc(_Bool is_difficult) {
      if (is_difficult)
        pc_hard(); //hard computer player
      else
        pc_easy(); //easy computer player
    }
    Last edited by christophergray; 03-22-2018 at 08:17 PM.

  9. #9
    Registered User
    Join Date
    Mar 2018
    Posts
    7
    thanks mate

Popular pages Recent additions subscribe to a feed

Tags for this Thread