Thread: C Programming Dice Display Function?

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    4

    C Programming Dice Display Function?

    I need a function (for part of a much bigger program) that displays two dice side by side. such as

    @ @ @
    @
    @ @ @

    But the function cannot be extensive, meaning i cannot just write out 36 instances. The function would intake an Int variables die1, die2 for the number to be rolled. Which comes from a random number generater in the main function.

    The function does not have to be like below, but if you know how thatd be awesome too. A hint the question gives me is:

    ? ? ? ? ? ?
    ? ?
    ? ? ? ? ? ?

    Your program can fill in each '?' appropriately with a '@' or a ' ' by using the conditional operator, ... ? ... : ... . (Or maybe use arrays of characters and sprintf().) And/or do further extensions that you devise — use your imagination! (;-)



    What ive been working on is this, but it is too confusing and extensive.

    Code:
    double dice(int die1, int die2)
    {
    int roll, total;
    double average;
    printf("+-------+ +-------+\n");
    //then you can go into if statements that print for common rolls
    if(die1 == 5 || die1 == 6 || die1 == 4)
    {
    printf("| @ @ | ");
    if(die2 == 5 || die2 == 6 || die2 == 4)
    {
    printf("| @ @ |\n");
    }
    if(die2 == 2 || die2 == 3)
    {
    printf("| @ |\n");
    }
    if(die2 == 1)
    {
    printf("| |\n");
    }
    }
    if(die1 == 2 || die1 == 3)
    {
    printf("| @ | ");
    if(die2 == 6)
    {
    printf("| @ @ |\n");
    }
    if(die2 == 2 || die2 == 3)
    {
    printf("| @ |\n");
    }
    if(die2 == 1)
    {
    printf("| |\n");
    }
    }
    if(die1 == 1)
    {
    }
    
    return(roll);
    }

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    4
    The first dice show 3 and 4, the second 2 dice show question marks in the shape on an H

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Do the dice have to be in standard configurations?

    Can a 2 be represented as
    o o o
    o x x
    o o o (or anything else)

    Or, does it have to be the standard
    o o x
    o o o
    x o o

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    4
    Quote Originally Posted by memcpy View Post
    Do the dice have to be in standard configurations?

    Can a 2 be represented as
    o o o
    o x x
    o o o (or anything else)

    Or, does it have to be the standard
    o o x
    o o o
    x o o
    I believe it has to be the standard look

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    One idea might be if you use an array die[3][3] you can study the dots: Dice - Wikipedia, the free encyclopedia
    So the centre dot die[1][1] would be on for odd numbers.
    Top-left corner die[0][0] appears to be on for numbers >= 4
    Top-right corner die[0][2] appears to be on for numbers >=2
    and so on.
    I don't want to do your homework for you but you should be able to make similar conditionals for each of the 3x3 dot positions.
    Maybe use die2 for the second die. Then print out the two die beside each other one row at-a-time.

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    4
    Can you explain your array idea in a more dumbed down version, Im very bad with C, and our teacher hasn't yet taught us arrays, but expects us to use them.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Take a look at a normal standard die.
    Divide the face into nine sections -- these are your array elements.
    Then you just have to store all of the possible faces in a way obvious to you.
    When someone rolls the die, you pick the right face.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Although an array would be nice, you could use 5 functions for the same result. Say the die face is 3, so you call show3(), and it prints up the face of a die with the proper arrangement of "dots". The tricky part of it is that you have to either print from the top row of BOTH die faces, to the bottom row, since you need the faces to show up, side by side (instead of one above the other).

    The alternative to this "print both die faces row by row", is to use a gotoxy() to move the cursor position up. On windows, you can use the Windows api SetConsoleCursorPosition(), to do the same thing as gotoxy().

    An example: dice are 2 and 3. You first print out the two die face:

    Code:
    @   
    
        @
    But now your cursor is 3 rows down from where you need it to be, to print the top row of the three face
    Code:
    @       @
            @
        @   @
    So now you have three ways to go:

    1) use an array
    2) use a separate function, working from the top row of both faces, to the bottom row
    3) use a cursor move function like gotoxy() (part of conio.h), or SetConsoleCursorPosition (part of windows.h).

    You could also use very low level explicit logic for this, but I wouldn't recommend it. Even #2 gets pretty involved.
    Last edited by Adak; 02-20-2012 at 09:18 PM.

  9. #9
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    I think what you're after (and if I'm wrong, it's probably too much work for such a simple program, and outside the scope of your exact problem, but if you're after extra credit, when you've got everything working, have a shot):

    The dots on a standard die are actually incredibly well chosen from a binary perspective. I noticed this once when I was building a circuit for a random dice thrower.

    Sit down and write out the standard dice patterns on a piece of paper (7 "dots" / "lights" positions in total). Then write out their binary representations (you only need 3 bits, don't bother doing 0 or 7). Then see if there are any *logical* (cough) connections between the two. Be careful of the 2 and 3 (the only non-symmetric layouts).

    When I did it years ago, it came out to a quite nice (but obscure) *logical* relationship between what dots are activated for which binary combinations. As in, you could literally write a single line of Boolean arithmetic for whether each light is on or not, and some of the lights have the same output.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to display full name in C Programming?
    By L3ir4 in forum C Programming
    Replies: 3
    Last Post: 06-30-2011, 09:59 PM
  2. Replies: 3
    Last Post: 01-29-2011, 04:54 AM
  3. Music Programming - Serial Matrix Display
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 04:16 PM
  4. Dice Function..
    By findme in forum C++ Programming
    Replies: 4
    Last Post: 11-29-2005, 10:56 PM
  5. Random function's application:Dice Roller
    By angeljicu in forum C Programming
    Replies: 0
    Last Post: 11-02-2002, 05:29 AM