Homework 6

Assigned: 3/9/04

Task: The game of Craps is played as follows:

1. The player rolls two 6-sided dice. 2. On the first roll

  • If the player rolls a 7 or an 11, they win.
  • If they roll a 2, 3, or 12, this is CRAPS and they lose.
  • If the first roll is anything else, the number rolled becomes the "target number.
    3. At this point, the person is trying to roll the target number again.
  • If they roll a 7 before they roll the target number, they lose.
  • If they roll the target number before they roll a seven, they win.
  • If they roll anything else, then they keep rolling.

    Write a program that lets someone play craps. As they are playing, the program should show each die roll, pause after each roll and ask the user to roll again, and should tell the person when they win or lose. After each game, the program should ask the user if they want to play again. The program should run until the user wants to stop playing.

    I am providing below the outline of a program that includes a dice rolling function. This function returns an integer, which is the result of rolling the two dice. You should delete my comments and fill in the appropriate information.


    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    // Put your function prototypes here
    int roll();
    
    int main()
    {
    	// Declare your variables here
    
    	srand((unsigned)time(NULL));
    
    	// Write your code here
    }
    
    int roll(void)
    {
    	int	number=-1;
    
    	number = rand()%36;
    
    	if(number == 0)
    		return 2;
    	else if(number==1 || number==2)
    		return 3;
    	else if(number==3 || number==4 || number==5)
    		return 4;
    	else if(number==6 || number==7 || number==8 || number==9)
    		return 5;
    	else if(number==10 || number==11 || number==12 || number==13 || number==14)
    		return 6;
    	else if(number==15 || number==16 || number==17 || number==18 || number==19 || number==20)
    		return 7;
    	else if(number==21 || number==22 || number==23 || number==24 || number==25)
    		return 8;
    	else if(number==26 || number==27 || number==28 || number==29)
    		return 9;
    	else if(number==30 || number==31 || number==32)
    		return 10;
    	else if(number==33 || number==34)
    		return 11;
    	else if(number==35)
    		return 12;
    	else
    		return -1;
    }
    

    Purpose: The primary purpose of this program is practice integrating functions, loops, and if statements.