Thread: Dice Function..

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    34

    Dice Function..

    int getDice()

    I am trying to write a function to return the value of a thrown dice. the function will return the dice value, a number between 1 and 6.

    i know you use the srand and the rand and make the function output it but i don't really know how to write that.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I will offer ye' one method:

    first.. create pair of dice:
    Code:
    struct PairOfDice
    {
         int dice_1;
         int dice_2;
    }


    let's prototype a dice roll function that will handle this new 'PairOfDice' data type..
    Code:
    PairOfDice* roll_the_dice(PairOfDice &DiceStruct) ;

    in your int main( ) function, declare an object of type PairOfDice:
    Code:
    PairOfDice *myDice ;

    then, when you are ready.. roll the dice..!
    Code:
    myDice = roll_the_dice(myDice) ;

    and now you have.. a pair o' dice
    Code:
    cout << "Roll 1 is:  "  <<  myDice->dice_1 ;
    cout << "Roll 2 is:  "  <<  myDice->dice_2 ;

    Now.. all you have to do is write up the function definition.. not too hard.. just randomly set the two attributes of the 'PairOfDice' object.

    OOP goodness
    Last edited by The Brain; 11-29-2005 at 06:37 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by The Brain
    I will offer ye' one method:

    Now.. all you have to do is write up the function definition.. not too hard.. just randomly set the two attributes of the 'PairOfDice' object.. include a provision to make sure that dice_1 != dice_2
    what? you've never rolled 2 dice and got the same result on each die?

    besides that wasn't what the OP was asking at all.

    OP, I could tell you how to write a function which uses rand to return a value between 1 and 6, but I'd prefer you to try yourself first.

    Hint: you'll need to use the % modulo operator (aka the remainder operator).
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    my bad.. i misread that

    my posts are like a work in progress usually
    Last edited by The Brain; 11-29-2005 at 06:37 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    o thanks for that man. i get it now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM