Thread: C-program dice game

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    12

    C-program dice game

    hi,
    I have this code so far, but how do implement It so the user can roll dice and a random number gets printed on the dice faces instead of the 'xo' the squares currently print?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define ROW 9
    #define COL 9
    
    char image[2][ROW][COL];
    //       COL
    //     +----+
    //     |    |    ROW
    //     |    |
    //     +----+
    //
    // 5x5 X and O images
    char shapes[2][5][6] = { { "*   *"
                             , " * * "
                             , "  *  "
                             , " * * "
                             , "*   *" }
                           , { " *** "
                             , "*   *"
                             , "*   *"
                             , "*   *"
                             , " *** " }
                           };
    void rectange()
    {
        for(int k = 0; k < 2; k++)
        for(int r = 0; r < ROW; r++)
            for(int c = 0; c < COL; c++)
            {
                image[k][r][c] = ' ';
                int top    = (r == 0);
                int bottom = (r == ROW - 1);
                int left   = (c == 0);
                int right  = (c == COL - 1);
                // corner
                if ((top || bottom) && (left || right))
                    image[k][r][c] = '+';
                else
                // top or bottom
                if (top || bottom) image[k][r][c] = '-';
                else
                // left or right
                if (left || right) image[k][r][c] = '|';
            }
    }
    void overlay_X_or_O()
    {
        for(int k = 0; k < 2; k++)
        {
            int shape = rand() % 2;
            for(int r = 0; r < 5; r++)
                for(int c = 0; c < 5; c++)
                    image[k][r + 2][c + 2] = shapes[shape][r][c];
        }
    }
    int main()
    {
        srand(time(NULL)); // initialise random number generator
        rectange(); // initialise the image with a rectange
        overlay_X_or_O(); // overlay image of X or O on top
        // print the image
        for(int r = 0; r < ROW; r++)
        {
            for(int k = 0; k < 2; k++)
            {
                for(int c = 0; c < COL; c++)
                    printf("%c", image[k][r][c]);
                printf("  ");
            }
            printf("\n");
        }
        return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    just need to figure out how to dynamically slip it into your array at which ever element number you want it to be in so it can be printed out.
    Code:
        char roll_num = '3';
        image[0][4][4] = roll_num;
        // print the image
        for(int r = 0; r < ROW; r++)
        {
            for(int k = 0; k < 2; k++)
            {
                for(int c = 0; c < COL; c++)
                    printf("%c", image[k][r][c]);
                printf("  ");
            }
            printf("\n");
        }
        return 0;
    }
    Code:
    +-------+  +-------+  
    |       |  |       |  
    |  ***  |  |  ***  |  
    | *   * |  | *   * |  
    | * 3 * |  | *   * |  
    | *   * |  | *   * |  
    |  ***  |  |  ***  |  
    |       |  |       |  
    +-------+  +-------+
    so yeah it needs work.
    Last edited by userxbw; 11-28-2017 at 03:13 PM.

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    12
    okay so Ive done it so it can print one number at a time. how do I get it to print random numbers? not just one?

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by Santac View Post
    okay so Ive done it so it can print one number at a time. how do I get it to print random numbers? not just one?
    Please post the revised version of your code so we can see your changes.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by rstanley View Post
    Please post the revised version of your code so we can see your changes.
    he is over here now

    How do i get this program to print random numbers?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dice Game (Fifty)
    By PhantomJoe in forum C Programming
    Replies: 41
    Last Post: 10-26-2017, 09:51 PM
  2. Help With Dice Game
    By CaliJoe in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2009, 04:08 PM
  3. Help With Dice Game
    By CaliJoe in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2009, 04:35 PM
  4. Replies: 9
    Last Post: 11-11-2007, 02:31 PM
  5. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 07:58 PM

Tags for this Thread