Like Tree3Likes

Rate this code? what could i have done better?

This is a discussion on Rate this code? what could i have done better? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> char setup_array(); void print_layout(char *numbers); void start_game(int players, char *numbers); int who_turn(int count); int play(); ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    56

    Rate this code? what could i have done better?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    char setup_array();
    void print_layout(char *numbers);
    void start_game(int players, char *numbers);
    int who_turn(int count);
    int play();
    void reset(char *numbers);
    
    
    
    
    
    
    int main ()
    {
        int players;
        char array [] = {'0','1','2','3','4','5','6','7','8','9'}; 
        char *numbers;
        numbers = array;
        printf ("Please enter number of players: ");
        scanf("%d", &players);
        print_layout(numbers);
        start_game(players, numbers);
        return 0;
    }
    
    
    void print_layout(char *numbers)
    {
        printf ("---+---+---\n");
        printf (" %c | %c | %c |\n",numbers[1], numbers[2], numbers[3] );
        printf ("---+---+---\n");
        printf (" %c | %c | %c |\n",numbers[4], numbers[5], numbers[6] );
        printf ("---+---+---\n");
        printf (" %c | %c | %c |\n",numbers[7], numbers[8], numbers[9] );
        printf ("---+---+---\n");
    }
    
    
    char setup_array()
    {
        return 0;
    }
    
    
    int play()
    {
        int play;
        printf("Please enter the number you would like to play: ");
        scanf("%d", &play);
        system("clear");
        return play;
    }
    
    
    void start_game(int players, char *numbers)
    {
        char turn1 = 'X';
        char play_again;
        int turn;
        int win = 0;
        int count = 1; 
        if (players == 2)
        {
            while (win < 1){
                if (who_turn(count) == 1)
                    turn1 = 'X';
                else
                    turn1 = 'O';
                numbers[play()] = turn1;
                print_layout(numbers);
                count ++;
                if ((numbers[1] == 'X' && numbers[2] == 'X' && numbers[3] == 'X') ||
                    (numbers[4] == 'X' && numbers[5] == 'X' && numbers[6] == 'X') ||
                    (numbers[7] == 'X' && numbers[8] == 'X' && numbers[9] == 'X') ||
                    (numbers[1] == 'X' && numbers[4] == 'X' && numbers[7] == 'X') ||
                    (numbers[2] == 'X' && numbers[5] == 'X' && numbers[8] == 'X') ||
                    (numbers[3] == 'X' && numbers[6] == 'X' && numbers[9] == 'X') ||
                    (numbers[1] == 'X' && numbers[5] == 'X' && numbers[9] == 'X') ||
                    (numbers[3] == 'X' && numbers[5] == 'X' && numbers[7] == 'X') )
                {
                    win = 1;
                }
                else if ((numbers[1] == 'O' && numbers[2] == 'O' && numbers[3] == 'O') ||
                    (numbers[4] == 'O' && numbers[5] == 'O' && numbers[6] == 'O') ||
                    (numbers[7] == 'O' && numbers[8] == 'O' && numbers[9] == 'O') ||
                    (numbers[1] == 'O' && numbers[4] == 'O' && numbers[7] == 'O') ||
                    (numbers[2] == 'O' && numbers[5] == 'O' && numbers[8] == 'O') ||
                    (numbers[3] == 'O' && numbers[6] == 'O' && numbers[9] == 'O') ||
                    (numbers[1] == 'O' && numbers[5] == 'O' && numbers[9] == 'O') ||
                    (numbers[3] == 'O' && numbers[5] == 'O' && numbers[7] == 'O') )
                {
                    win = 2;
                }
     
    
    
            }
            if (win == 1)
                printf("\n\nPlayer 1 Wins!!!!!!!!!!!!!!!!!\n\n");
            else if (win == 2)
                printf("\n\nPlayer 2 Wins!!!!!!!!!!!!!!!!!\n\n");
        }
        getchar();
        printf("Do you want to play again (Y or N): ");
        scanf("%c", &play_again);
        if (play_again == 'Y' || play_again == 'y')
        {   
            win = 0;
            count = 0;   
            reset(numbers);
        }
        else
            exit(0);
    
    
    }
    
    
    int who_turn(int count)
    {
        if (count % 2 == 0)
            return 2;
        else
            return 1;
    }
    
    
    void reset(char *numbers)
    {
        char array [] = {'0','1','2','3','4','5','6','7','8','9'}; 
        numbers = array;
        print_layout(numbers);
        start_game(2, numbers);
    }

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    I know i havent implemented the 1 player mode, but ill do that later..

    Im just learning C, and just want to know what i could do better

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    You need input validation.
    Code:
    numbers[play()] = turn1;
    This is clever and brittle code. If play() returns an integer that exceeds the bounds of numbers, the behavior of the code is undefined.

    Why is setup_array() a stub function?

    You shouldn't worry about wins until there have been at least three turns for both players. And what if the game ends in a tie?

    reset() should also be rewritten completely. I don't like how start_game() and reset() call each other.
    Last edited by whiteflags; 07-07-2012 at 07:49 PM.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    Quote Originally Posted by whiteflags View Post
    You need input validation.
    Code:
    numbers[play()] = turn1;
    This is clever and brittle code. If play() returns an integer that exceeds the bounds of numbers, the behavior of the code is undefined.

    Why is setup_array() a stub function?

    You shouldn't worry about wins until there have been at least three turns for both players. And what if the game ends in a tie?

    reset() should also be rewritten completely. I don't like how start_game() and reset() call each other.
    Cool thanks for your help, setup_array was a mistake, i deleted it completely.. yes ill have to add some input verification to make sure a player cant play what other player did, also ill have to add tie to it, how else would i do reset? since i have to remake the array ;(

    Will make few changes and update soon

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    You can break a string literal into adjacent parts. This can be used to make it easier to see your grid, e.g.,
    Code:
    void print_layout(const char *numbers)
    {
        printf("+----+----+----+\n"
               "| %c | %c | %c |\n"
               "+----+----+----+\n"
               "| %c | %c | %c |\n"
               "+----+----+----+\n"
               "| %c | %c | %c |\n"
               "+----+----+----+\n",
            numbers[1], numbers[2], numbers[3],
            numbers[4], numbers[5], numbers[6],
            numbers[7], numbers[8], numbers[9]);
    }
    I have taken the liberty of changing the output format slightly
    I have also changed numbers to point to a const char since print_layout does not change anything that numbers points to.

    Also, I notice that you are effectively using 1-based array indices. While this can work, it is not a very good idea because it may be confusing since 0-based indices are the norm.
    Last edited by laserlight; 07-07-2012 at 11:26 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    Ok here is the code updated:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void print_layout(const char *numbers);
    void start_game(int players, char *numbers);
    int who_turn(int count);
    int play();
    void reset(char *numbers);
    
    
    
    
    
    
    int main ()
    {
        int players;
        char array [] = {'0','1','2','3','4','5','6','7','8','9'}; 
        char *numbers;
        numbers = array;
        printf ("Please enter number of players: ");
        scanf("%d", &players);
        print_layout(numbers);
        start_game(players, numbers);
        return 0;
    }
    
    
    void print_layout(const char *numbers)
    {    
        printf("+---+---+---+\n"
               "| %c | %c | %c |\n"
               "+---+---+--+\n"
               "| %c | %c | %c |\n"
               "+---+---+---+\n"
               "| %c | %c | %c |\n"
               "+---+---+---+\n",
            numbers[1], numbers[2], numbers[3],
            numbers[4], numbers[5], numbers[6],
            numbers[7], numbers[8], numbers[9]);
    }
    
    
    int play()
    {
        int play;
        printf("Please enter the number you would like to play: ");
        scanf("%d", &play);
        system("clear");
        return play;
    }
    
    
    void start_game(int players, char *numbers)
    {
        int goodplay;
        char turn1 = 'X';
        char play_again;
        int turn;
        int win = 0;
        int count = 1; 
        if (players == 2)
        {
            while (count <= 9 && win < 1){
                if (who_turn(count) == 1)
                    turn1 = 'X';
                else
                    turn1 = 'O';
                goodplay = play();
                while (numbers[goodplay] == 'X' || numbers[goodplay] == 'O')
                {
                    print_layout(numbers);
                    printf("\n"); 
                    printf ("This move has already been made, please pick another one: ");                
                    scanf("%d", &goodplay);
                    system("clear");
                }
                numbers[goodplay] = turn1;           
                print_layout(numbers);
                count ++;
                if (count >= 3)
                    if ((numbers[1] == 'X' && numbers[2] == 'X' && numbers[3] == 'X') ||
                        (numbers[4] == 'X' && numbers[5] == 'X' && numbers[6] == 'X') ||
                        (numbers[7] == 'X' && numbers[8] == 'X' && numbers[9] == 'X') ||
                        (numbers[1] == 'X' && numbers[4] == 'X' && numbers[7] == 'X') ||
                        (numbers[2] == 'X' && numbers[5] == 'X' && numbers[8] == 'X') ||
                        (numbers[3] == 'X' && numbers[6] == 'X' && numbers[9] == 'X') ||
                        (numbers[1] == 'X' && numbers[5] == 'X' && numbers[9] == 'X') ||
                        (numbers[3] == 'X' && numbers[5] == 'X' && numbers[7] == 'X') )
                    {
                        win = 1;
                    }
                    else if ((numbers[1] == 'O' && numbers[2] == 'O' && numbers[3] == 'O') ||
                        (numbers[4] == 'O' && numbers[5] == 'O' && numbers[6] == 'O') ||
                        (numbers[7] == 'O' && numbers[8] == 'O' && numbers[9] == 'O') ||
                        (numbers[1] == 'O' && numbers[4] == 'O' && numbers[7] == 'O') ||
                        (numbers[2] == 'O' && numbers[5] == 'O' && numbers[8] == 'O') ||
                        (numbers[3] == 'O' && numbers[6] == 'O' && numbers[9] == 'O') ||
                        (numbers[1] == 'O' && numbers[5] == 'O' && numbers[9] == 'O') ||
                        (numbers[3] == 'O' && numbers[5] == 'O' && numbers[7] == 'O') )
                    {
                        win = 2;
                    }
     
    
    
            }
            if (win == 0)
                printf("\n\n TIIEEE!!!!\n\n");
            if (win == 1)
                printf("\n\nPlayer 1 Wins!!!!!!!!!!!!!!!!!\n\n");
            else if (win == 2)
                printf("\n\nPlayer 2 Wins!!!!!!!!!!!!!!!!!\n\n");
        }
        getchar();
        printf("Do you want to play again (Y or N): ");
        scanf("%c", &play_again);
        if (play_again == 'Y' || play_again == 'y')
        {   
            win = 0;
            count = 0;   
            reset(numbers);
        }
        else
            exit(0);
    
    
    }
    
    
    int who_turn(int count)
    {
        if (count % 2 == 0)
            return 2;
        else
            return 1;
    }
    
    
    void reset(char *numbers)
    {
        char array [] = {'0','1','2','3','4','5','6','7','8','9'}; 
        numbers = array;
        print_layout(numbers);
        start_game(2, numbers);
    }
    Last edited by SuperMiguel; 07-07-2012 at 11:46 PM.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    Quote Originally Posted by laserlight View Post
    You can break a string literal into adjacent parts. This can be used to make it easier to see your grid, e.g.,
    Code:
    void print_layout(const char *numbers)
    {
        printf("+----+----+----+\n"
               "| %c | %c | %c |\n"
               "+----+----+----+\n"
               "| %c | %c | %c |\n"
               "+----+----+----+\n"
               "| %c | %c | %c |\n"
               "+----+----+----+\n",
            numbers[1], numbers[2], numbers[3],
            numbers[4], numbers[5], numbers[6],
            numbers[7], numbers[8], numbers[9]);
    }
    I have taken the liberty of changing the output format slightly
    I have also changed numbers to point to a const char since print_layout does not change anything that numbers points to.

    Also, I notice that you are effectively using 1-based array indices. While this can work, it is not a very good idea because it may be confusing since 0-based indices are the norm.
    Cool i like that better, i have changed it a bit since %c takes a bit more space than the single letter/number so i removed few things. not sure what you mean by using 1-based array indices?

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Quote Originally Posted by SuperMiguel View Post
    how else would i do reset? since i have to remake the array ;(
    The way you are doing it now, it is technically problematic. The function start_game() is called first, which calls reset(), which calls start_game(), which calls reset(), which calls start_game()... you give the user the option to stop, but you shouldn't program a game this way. You will be able to play a lot of games, until you run out of stack memory. If and when you do run out of stack memory, the program will crash.

    You will have to rewrite reset() to refill numbers with initial values only. I want you to do it this way, or any code equivalent way:

    Code:
    for (char i = '0'; i <= '9'; i++) {
       numbers[i - '0'] = i;
    }
    Because assigning an array filled with values to numbers in this function is also problematic. The array itself will be released from memory when the function returns, making the pointer invalid. If you then use the pointer, the behavior of the code is undefined.

    The way you had written reset() protected you a bit from this issue, but what the game really needs is a sensible redesign. If you need help with that: Use main() to decide when to stop playing the game and end the program.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    There's a lot of copy and paste between lines 76 and 96.

    Consider
    Code:
    int checkWin ( char *numbers, char player ) {
       if ((numbers[1] == player && numbers[2] == player && numbers[3] == player)
       // and so on
    }
    Then your main is just
    Code:
    if ( checkWin(numbers,'X') ) {
    }
    stahta01 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    Quote Originally Posted by whiteflags View Post
    The way you are doing it now, it is technically problematic. The function start_game() is called first, which calls reset(), which calls start_game(), which calls reset(), which calls start_game()... you give the user the option to stop, but you shouldn't program a game this way. You will be able to play a lot of games, until you run out of stack memory. If and when you do run out of stack memory, the program will crash.

    You will have to rewrite reset() to refill numbers with initial values only. I want you to do it this way, or any code equivalent way:

    Code:
    for (char i = '0'; i <= '9'; i++) {
       numbers[i - '0'] = i;
    }
    Because assigning an array filled with values to numbers in this function is also problematic. The array itself will be released from memory when the function returns, making the pointer invalid. If you then use the pointer, the behavior of the code is undefined.

    The way you had written reset() protected you a bit from this issue, but what the game really needs is a sensible redesign. If you need help with that: Use main() to decide when to stop playing the game and end the program.
    Ok make sense, will update code son

    Quote Originally Posted by Salem View Post
    There's a lot of copy and paste between lines 76 and 96.

    Consider
    Code:
    int checkWin ( char *numbers, char player ) {
       if ((numbers[1] == player && numbers[2] == player && numbers[3] == player)
       // and so on
    }
    Then your main is just
    Code:
    if ( checkWin(numbers,'X') ) {
    }
    will try this as well

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    ok I have updated my code, i also added a very simple(very) for the computer to play (will update soon)...
    note: I guess i can also simplify line 58 to 94 they are about the same

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    void print_layout(const char *numbers);
    void start_game(int players, char *numbers, char *playagain);
    int who_turn(int count);
    int play();
    int win(char *numbers, char sign);
    
    int main ()
    {
        int players;
        char array [] = {'0','1','2','3','4','5','6','7','8','9'}; 
        char *numbers = array;
        char play_again = 'Y';
        char *playagain = &play_again;
        //numbers = array;
        printf ("Please enter number of players: ");
        scanf("%d", &players);
        print_layout(numbers);
        while (play_again = 'Y')
            start_game(players, numbers, playagain);
        return 0;
    }
    
    
    void print_layout(const char *numbers)
    {    
        printf("+---+---+---+\n"
               "| %c | %c | %c |\n"
               "+---+---+--+\n"
               "| %c | %c | %c |\n"
               "+---+---+---+\n"
               "| %c | %c | %c |\n"
               "+---+---+---+\n",
            numbers[1], numbers[2], numbers[3],
            numbers[4], numbers[5], numbers[6],
            numbers[7], numbers[8], numbers[9]);
    }
    
    
    int play()
    {
        int play;
        printf("Please enter the number you would like to play: ");
        scanf("%d", &play);
        system("clear");
        return play;
    }
    
    
    void start_game(int players, char *numbers, char *playagain)
    {
        int goodplay;
        char turn1 = 'X';
        int count = 1; 
        int comp;
        int winstat = 0;
        while (count <= 9 && winstat < 1)
        {   
            if (players == 2)
            {
                if (who_turn(count) == 1)
                    turn1 = 'X';
                else
                    turn1 = 'O';
                goodplay = play();
                while (numbers[goodplay] == 'X' || numbers[goodplay] == 'O')
                {
                    print_layout(numbers);
                    printf("\n"); 
                    printf ("This move has already been made, please pick another one: ");                
                    scanf("%d", &goodplay);
                    system("clear");
                }
                numbers[goodplay] = turn1;           
                print_layout(numbers);
                count ++;                    
            }
            
            if (players == 1)
            {
                if (who_turn(count) == 1)
                {
                    turn1 = 'X';
                            goodplay = play();
                    while (numbers[goodplay] == 'X' || numbers[goodplay] == 'O')
                    {
                        print_layout(numbers);
                        printf("\n"); 
                        printf ("This move has already been made, please pick another one: ");                
                        scanf("%d", &goodplay);
                        system("clear");
                    }
                    numbers[goodplay] = turn1;           
                    print_layout(numbers);
                    count ++;
                }
                else
                {
                    turn1 = 'O';
                    comp = (rand() % 9 + 1);
                    while (numbers[comp] == 'X' || numbers[comp] == 'O')
                        comp = (rand() % 9 + 1);
                    numbers[comp] = turn1;
                    system("clear");
                    print_layout(numbers);
                    count ++;
                }
            }
            
        if (count >= 3)
        {
            winstat = win(numbers, turn1);
        }   
    
    
        }
    
    
        if (winstat == 0)
            printf("\n\n TIIEEE!!!!\n\n");
        else if (winstat == 1)
            printf("\n\nPlayer 1 Wins!!!!!!!!!!!!!!!!!\n\n");
        else if (winstat == 2 && players == 2)
            printf("\n\nPlayer 2 Wins!!!!!!!!!!!!!!!!!\n\n");
        else if (winstat == 2 && players == 1)
            printf("\n\n Computer has WON!!!\n\n");
    
    
        getchar();
        printf("Do you want to play again (Y or N): ");
        scanf("%c", playagain);
        if (*playagain == 'Y' || *playagain == 'y')
        {   
            win(numbers, 'W');
            count = 0;
            char i;
            for (i = '0'; i <= '9'; i++)
                numbers[i - '0'] = i;
            print_layout(numbers);
        }
        else
            exit(0);
    }
    
    
    int who_turn(int count)
    {
        if (count % 2 == 0)
            return 2;
        else
            return 1;
    }
    
    
    int win(char *numbers, char sign)
    {
        
        if ((numbers[1] == sign && numbers[2] == sign && numbers[3] == sign) ||
            (numbers[4] == sign && numbers[5] == sign && numbers[6] == sign) ||
            (numbers[7] == sign && numbers[8] == sign && numbers[9] == sign) ||
            (numbers[1] == sign && numbers[4] == sign && numbers[7] == sign) ||
            (numbers[2] == sign && numbers[5] == sign && numbers[8] == sign) ||
            (numbers[3] == sign && numbers[6] == sign && numbers[9] == sign) ||
            (numbers[1] == sign && numbers[5] == sign && numbers[9] == sign) ||
            (numbers[3] == sign && numbers[5] == sign && numbers[7] == sign) )
        {
            if (sign == 'X')
                return 1;
            else if (sign == 'O') 
                return 2;
        }
       else
           return 0;        
    }

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Code:
    goodplay = play();
                   while (numbers[goodplay] == 'X' || numbers[goodplay] == 'O')
    And what if goodplay isn't in the acceptable range of subscripts for numbers? The behavior is still undefined in that case.

  13. #13
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    Quote Originally Posted by whiteflags View Post
    Code:
    goodplay = play();
                   while (numbers[goodplay] == 'X' || numbers[goodplay] == 'O')
    And what if goodplay isn't in the acceptable range of subscripts for numbers? The behavior is still undefined in that case.
    Code:
    goodplay = play();
                while (numbers[goodplay] == 'X' || numbers[goodplay] == 'O' || goodplay > 9)

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Now test it.

  15. #15
    Registered User
    Join Date
    Mar 2012
    Posts
    56
    Ok made few small changes, still need to add more to the computer AI (to play a bit more offensively instead of 100% def).., so far seems to be working fine

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void print_layout(const char *numbers);
    void start_game(int players, char *numbers, char *playagain);
    int who_turn(int count);
    int play();
    int win(char *numbers, char sign);
    void valid(int *pcount, char *pturn1, int *pgoodplay, char *numbers);
    
    
    
    
    int main ()
    {
        int players;
        char array [] = {'0','1','2','3','4','5','6','7','8','9'}; 
        char *numbers = array;
        char play_again = 'Y';
        char *playagain = &play_again; 
        printf ("Please enter number of players: ");
        scanf("%d", &players);
        print_layout(numbers);
        while (play_again = 'Y')
            start_game(players, numbers, playagain);
        return 0;
    }
    
    
    void print_layout(const char *numbers)
    {    
        printf("+---+---+---+\n"
               "| %c | %c | %c |\n"
               "+---+---+--+\n"
               "| %c | %c | %c |\n"
               "+---+---+---+\n"
               "| %c | %c | %c |\n"
               "+---+---+---+\n",
            numbers[1], numbers[2], numbers[3],
            numbers[4], numbers[5], numbers[6],
            numbers[7], numbers[8], numbers[9]);
    }
    
    
    int play()
    {
        int play;
        printf("Please enter the number you would like to play: ");
        scanf("%d", &play);
        system("clear");
        return play;
    }
    
    
    void valid(int *pcount, char *pturn1, int *pgoodplay, char *numbers)
    {    
        if (who_turn(*pcount) == 1)
            *pturn1 = 'X';
        else
            *pturn1 = 'O';
        *pgoodplay = play();
        while (numbers[*pgoodplay] == 'X' || numbers[*pgoodplay] == 'O' || *pgoodplay > 9)
        {
            print_layout(numbers);
            printf("\n"); 
            printf ("This move has already been made, or number out of range\nPlease pick another one: ");                
            scanf("%d", pgoodplay);
            system("clear");
        }
        numbers[*pgoodplay] = *pturn1;           
        print_layout(numbers);
        *pcount = (*pcount) + 1;
    }
    
    
    void start_game(int players, char *numbers, char *playagain)
    {
        int goodplay;
        int *pgoodplay = &goodplay;
        char turn1 = 'X';
        char *pturn1 = &turn1;
        int count = 1;
        int *pcount = &count;
    
    
        int comp;
        int winstat = 0;
        while (count <= 9 && winstat < 1)
        {   
            
            if (players == 2)
            {
                valid(pcount, pturn1, pgoodplay, numbers);
            }
              
            else if (players == 1)
            {
                if (who_turn(count) == 1)
                {
                    valid(pcount, pturn1, pgoodplay, numbers);      
                }
                else
                {
                    turn1 = 'O';
                    comp = (rand() % 9 + 1);
                    while (numbers[comp] == 'X' || numbers[comp] == 'O')
                        comp = (rand() % 9 + 1);
                    if ((numbers[1] == 'X' && numbers[2] == 'X') && (numbers[3] != 'X' && numbers[3] != 'O'))
                            numbers[3] = turn1;
                    else if ((numbers[1] == 'X' && numbers[3] == 'X') && (numbers[2] != 'X' && numbers[2] != 'O'))
                            numbers[2] = turn1;
                    else if ((numbers[2] == 'X' && numbers[3] == 'X') && (numbers[1] != 'X' && numbers[1] != 'O'))
                            numbers[1] = turn1;     
                    else if ((numbers[4] == 'X' && numbers[5] == 'X') && (numbers[6] != 'X' && numbers[6] != 'O'))
                            numbers[6] = turn1;
                    else if ((numbers[4] == 'X' && numbers[6] == 'X') && (numbers[5] != 'X' && numbers[5] != 'O'))
                            numbers[5] = turn1; 
                    else if ((numbers[5] == 'X' && numbers[6] == 'X') && (numbers[4] != 'X' && numbers[4] != 'O'))
                            numbers[4] = turn1;
                    else if ((numbers[7] == 'X' && numbers[8] == 'X') && (numbers[9] != 'X' && numbers[9] != 'O'))
                            numbers[9] = turn1; 
                    else if ((numbers[7] == 'X' && numbers[9] == 'X') && (numbers[8] != 'X' && numbers[8] != 'O'))
                            numbers[8] = turn1;
                    else if ((numbers[8] == 'X' && numbers[9] == 'X') && (numbers[7] != 'X' && numbers[7] != 'O'))
                            numbers[7] = turn1; 
                    else if ((numbers[1] == 'X' && numbers[4] == 'X') && (numbers[7] != 'X' && numbers[7] != 'O'))
                            numbers[7] = turn1;
                    else if ((numbers[1] == 'X' && numbers[7] == 'X') && (numbers[4] != 'X' && numbers[4] != 'O'))
                            numbers[4] = turn1;     
                    else if ((numbers[4] == 'X' && numbers[7] == 'X') && (numbers[1] != 'X' && numbers[1] != 'O'))
                            numbers[1] = turn1;
                    else if ((numbers[2] == 'X' && numbers[5] == 'X') && (numbers[8] != 'X' && numbers[8] != 'O'))
                            numbers[8] = turn1; 
                    else if ((numbers[2] == 'X' && numbers[8] == 'X') && (numbers[5] != 'X' && numbers[5] != 'O'))
                            numbers[5] = turn1;
                    else if ((numbers[5] == 'X' && numbers[8] == 'X') && (numbers[2] != 'X' && numbers[2] != 'O'))
                            numbers[2] = turn1; 
                    else if ((numbers[3] == 'X' && numbers[6] == 'X') && (numbers[9] != 'X' && numbers[9] != 'O'))
                            numbers[9] = turn1;
                    else if ((numbers[3] == 'X' && numbers[9] == 'X') && (numbers[6] != 'X' && numbers[6] != 'O'))
                            numbers[6] = turn1; 
                    else if ((numbers[6] == 'X' && numbers[9] == 'X') && (numbers[3] != 'X' && numbers[3] != 'O'))
                            numbers[3] = turn1;
                    else if ((numbers[1] == 'X' && numbers[5] == 'X') && (numbers[9] != 'X' && numbers[9] != 'O'))
                            numbers[9] = turn1;     
                    else if ((numbers[1] == 'X' && numbers[9] == 'X') && (numbers[5] != 'X' && numbers[5] != 'O'))
                            numbers[5] = turn1;
                    else if ((numbers[5] == 'X' && numbers[9] == 'X') && (numbers[1] != 'X' && numbers[1] != 'O'))
                            numbers[1] = turn1; 
                    else if ((numbers[3] == 'X' && numbers[5] == 'X') && (numbers[7] != 'X' && numbers[7] != 'O'))
                            numbers[7] = turn1;
                    else if ((numbers[3] == 'X' && numbers[7] == 'X') && (numbers[5] != 'X' && numbers[5] != 'O'))
                            numbers[5] = turn1; 
                    else if ((numbers[5] == 'X' && numbers[7] == 'X') && (numbers[3] != 'X' && numbers[3] != 'O'))
                            numbers[3] = turn1;       
                    else
                        numbers[comp] = turn1;
                    
                    system("clear");
                    print_layout(numbers);
                    count ++;
                }
            }
            
            if (count >= 3)
            {
                winstat = win(numbers, turn1);
            }   
    
    
        }
    
    
        if (winstat == 0)
            printf("\n\n TIIEEE!!!!\n\n");
        else if (winstat == 1)
            printf("\n\nPlayer 1 Wins!!!!!!!!!!!!!!!!!\n\n");
        else if (winstat == 2 && players == 2)
            printf("\n\nPlayer 2 Wins!!!!!!!!!!!!!!!!!\n\n");
        else if (winstat == 2 && players == 1)
            printf("\n\n Computer has WON!!!\n\n");
    
    
        getchar();
        printf("Do you want to play again (Y or N): ");
        scanf("%c", playagain);
        if (*playagain == 'Y' || *playagain == 'y')
        {   
            win(numbers, 'W');
            count = 0;
            char i;
            for (i = '0'; i <= '9'; i++)
                numbers[i - '0'] = i;
            print_layout(numbers);
        }
        else
            exit(0);
    }
    
    
    int who_turn(int count)
    {
        if (count % 2 == 0)
            return 2;
        else
            return 1;
    }
    
    
    int win(char *numbers, char sign)
    {
        
        if ((numbers[1] == sign && numbers[2] == sign && numbers[3] == sign) ||
            (numbers[4] == sign && numbers[5] == sign && numbers[6] == sign) ||
            (numbers[7] == sign && numbers[8] == sign && numbers[9] == sign) ||
            (numbers[1] == sign && numbers[4] == sign && numbers[7] == sign) ||
            (numbers[2] == sign && numbers[5] == sign && numbers[8] == sign) ||
            (numbers[3] == sign && numbers[6] == sign && numbers[9] == sign) ||
            (numbers[1] == sign && numbers[5] == sign && numbers[9] == sign) ||
            (numbers[3] == sign && numbers[5] == sign && numbers[7] == sign) )
        {
            if (sign == 'X')
                return 1;
            else if (sign == 'O') 
                return 2;
        }
       else
           return 0;        
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rate my code
    By Baard in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2004, 08:19 AM
  2. rate my directx code
    By revelation437 in forum Game Programming
    Replies: 9
    Last Post: 01-02-2004, 05:21 PM
  3. Rate My Code: Function Usage & File Streaming
    By KingZoolerius66 in forum C++ Programming
    Replies: 9
    Last Post: 12-28-2003, 09:23 PM
  4. Rate my source code :)
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-17-2003, 01:09 PM
  5. rate my code
    By kashifk in forum C Programming
    Replies: 1
    Last Post: 06-07-2003, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21