Thread: Craps the Dice Game

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    9

    Question Craps the Dice Game

    What is the way to have a random number from 1-6 for the game craps, and how do u output it. Hope someone can help...

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    to get a random number use the rand() function, this gives you a random integer. To specify the range you want you need to use the % operator.
    Code:
    int num;
    
    num = rand() % 6;
    The above will give you a number between 0 - 5, so you will also have to add 1 to num to get 1 - 6;
    Code:
    int num;
    
    num = rand() % 6 + 1;
    
    printf("%d", num);
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    you'll find the rand function in <stdlib.h>
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    9

    another question

    This is my code...

    int func_dice()

    { int dice1, dice2, number;
    cout<<"Let's play CRAPS!!";
    cout<<endl;
    cout<<"Let's roll the dice...";
    dice1=(Rand()%6)+1;
    dice2=(Rand()%6)+1;
    cout<<"You rolled a "<<printf("%d", dice1)<<" and a "<<printf("%d", dice2);
    cout<<endl;
    number=dice1+dice2;
    cout<<"The total was "<<number;

    It says that it wont work because printf() and rand() aren't properly called functions...any help?

  5. #5
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    as your using c++ you don't need the printf()

    just do cout << dice1 << dice2;
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    9

    more problems

    Here's my whole code so far...

    //crapsgame.cpp
    //This is a form of game called craps which uses functions, looping, arrays, switchs, and enums

    #include <iostream.h>
    #include <math.h>
    #include <string.h>
    #include <stdlib.h>

    int func_name();
    int func_dice();

    int main()

    {
    cout<<"Hello, and welcome to my program. I hope you enjoy it!";
    func_name();
    func_dice();

    return 0;

    }

    //************************************************** ***************************
    //This portion finds the name of the player

    int func_name()

    {
    char f_name[20];
    cout<<"Please input your first name. ";
    cin>>f_name;
    char l_name[30];
    cout<<"Please input your last name. ";
    cin>>l_name;
    cout<<endl;
    cout<<"Welcome "<<f_name<<" "<<l_name;
    cout<<endl<<endl<<endl;

    return 0;
    }

    //************************************************** ***************************
    //This is the actual game portion of the program

    int func_dice()

    { int dice1, dice2, number;
    cout<<"Let's play CRAPS!!";
    cout<<endl;
    cout<<"Let's roll the dice...";
    dice1=(Rand()%6)+1;
    dice2=(Rand()%6)+1;
    cout<<"You rolled a "<<dice1<<" and a "<<dice2;
    cout<<endl;
    number=dice1+dice2;
    cout<<"The total was "<<number;


    return 0;

    }


    It still says this as my error...
    "Implicit declaration of function 'int Rand(...)'

    Hope you can help, and thanx for the help so far..

  7. #7
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    dice1=(Rand()%6)+1;
    dice2=(Rand()%6)+1;


    change the capital 'R' to a lower case 'r'.
    I did this then your prog compiles and runs fine
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    9

    idea

    Again, thanx for the help. One last thing. When I do the random numbers every time i open it up the do the same numbers...
    It's always:
    6,6
    5,5
    6,5
    1,1
    5,3
    6,6
    2,4
    ..........

    Hope you have some advice...

  9. #9
    Unregistered
    Guest
    The reason your getting the same numbers is because rand generates them at compile time not when the program is run.

    http://www.cprogramming.com/boardfaq.html#random

    read here for further info on random numbers, it gives an example of how to get different numbers each time.

  10. #10
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    Mmmm for some reason i got logged out.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  11. #11
    Registered User
    Join Date
    Nov 2001
    Posts
    9

    switch statement and loop

    I have 2 questions now. 1st:
    In my switch statemnt it is only using the last number is each case. (12, and 11) it's ignoring the 7, 2, and 3 and just counting it as a default. How do i change it to more then one thing for each case?

    switch(point1)
    {
    case (7,11):
    {cout<<"You win!! Congratulations!"<<endl<<endl;
    break;}
    case (2,3,12):
    {cout<<"You lose. Sorry better luck next time."<<endl<<endl;
    break;}
    default:
    {cout<<"This is your current point. Let's keep on rolling.";
    cout<<endl<<endl<<endl;
    break;}

    2nd Question:
    After when they get they're point I need to be able to loop it. If they get a 7, 11 then they win and it's done, and if they get a 2, 3, or 12 then they're done. But if the number is a 4,5,6,8,9,10 it becomes there "point". Then i need it to remember that "point" and it needs to roll again. If it is a 7 then they automatically lose.. Also, if it's they're number then they win. But if it's not there then they have to roll again and again till they either get their number to win or a 7 to lose. Hopefully u can help me out with the 2 questions. Thanx, everyone!!!

  12. #12
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    First question : use seperate cases for each number like so
    Code:
    switch(point1)
    {
         case 7  :
         case 11 : cout << "You win!! Congratulations!";
                   break;
         case 2  :
         case 3  :
         case 12 : cout << "You lose.";
                   break;
         default : cout << "This is your current point.";
                   break;
    }
    When you have cases without break statements the program carries on down through the other cases until it encounters one.

    Second question : You obviously need to loop, so its just figuring out a control expression something like.
    Code:
    while(point1 >= 4 && point1 <= 6 || point1 >= 8 && point1 <= 10)
    {
         // put code here 
    }
    Then what you need to do is add if statements to some of your cases in the switch
    Code:
    switch(point1)
    {
         case 7  : if( This is not the first go)
                        {
                              cout << "You lose.";
                              break;
                        }
         case 11 : cout << "You win!! Congratulations!";
                   break;
         case 2  :
         case 3  :
         case 12 : cout << "You lose.";
                   break;
         default : cout << "This is your current point.";
                   break;
    }
    All spelling mistakes, syntatical errors and stupid comments are intentional.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help With Dice Game
    By CaliJoe in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2009, 04:35 PM
  2. Dice Simualtion
    By sillyman in forum C Programming
    Replies: 2
    Last Post: 04-22-2008, 01:43 PM
  3. New Project, text game, design stage.
    By Shamino in forum Game Programming
    Replies: 9
    Last Post: 05-23-2007, 06:39 AM
  4. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  5. My first C++ game (early alpha stage)
    By jdinger in forum Game Programming
    Replies: 7
    Last Post: 04-03-2002, 11:54 AM