User:Akrabbim/C++/craps.cpp

From Wikipedia, the free encyclopedia

This is the text of a C++ source code, meant to have a ".cpp" file extension. To run the program, one will need a program to compile and link the code.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <time.h>
using namespace std;

void play();
        // Pre: none
        // Post: none
void autoplay();
        // Pre: none
        // Post: none
int dierand();
        // Pre: none
        // Post: Random number from 1 - 6
void win();
        // Pre: none
        // Post: none
void lose();
        // Pre: none
        // Post: none
void success();
        // Pre: none
        // Post: none

float GAMECOUNT = 0;
float WINCOUNT = 0;

void main()
{
        char choice;
        srand(time(NULL));              // Necessary for number generator in dierand()
        cout << "Welcome to the game of craps. Would you like to play manually, or would\n"
                << "\tyou like to simulate a number of games? (Press 'M' for manual\n"
                << "\t or 'S' for the simulated option) -> ";
        cin >> choice;
        if((choice == 'M') || (choice == 'm'))
                play();
        if((choice == 'S') || (choice == 's'))
                autoplay();
}

void play()
{
        int die1, die2, sum, point;
        char choice;
        cout << "\nAre you ready to begin? (Y/N) -> ";
        cin >> choice;
        while((choice == 'Y') || (choice == 'y'))
        {
                point = 0;
                sum = 0;
                die1 = dierand();
                die2 = dierand();
                cout << "\nFirst die = " << die1 << "\tSecond Die = " << die2;
                point = die1 + die2;
                if((point == 7) || (point ==11))
                {
                        cout << "\tThe sum is " << point;
                        win();
                }
                else if((point == 2) || (point == 3) || (point == 12))
                {
                        cout << "\tThe sum is " << point;
                        lose();
                }
                else
                {
                        cout << "\tThe POINT sum is " << point;
                        while(!((sum == point) || (sum == 7)))
                        {
                                cout << '\n';
                                system("PAUSE");      // Freezes screen and waits for user
                                die1 = dierand();
                                die2 = dierand();
                                cout << "\nFirst die = " << die1 << "\tSecond Die = " << die2;
                                sum = die1 + die2;
                                cout << "\tThe sum is " << sum;
                        }
                        if(sum == point)
                                win();
                        if(sum == 7)
                                lose();
                }
                success();
                cout << "\nWould you like to play again? (Y/N) -> ";
                cin >> choice;
        }
}

void autoplay()
{  
        int die1, die2, point, sum, simcount, simnum;
        cout << "\nHow many games would you like simulated?  ";
        cin >> simnum;
        for(simcount = 1; simcount <= simnum; simcount++)
        {
                point = 0;
                sum = 0;
                die1 = dierand();
                die2 = dierand();
                point = die1 + die2;
                if((point == 7) || (point ==11))
                {
                        win();
                }
                else if((point == 2) || (point == 3) || (point == 12))
                {
                        lose();
                }
                else
                {
                        while(!((sum == point) || (sum == 7)))
                        {
                                die1 = dierand();
                                die2 = dierand();
                                sum = die1 + die2;
                        }
                        if(sum == point)
                                win();
                        if(sum == 7)
                                lose();
                }
        }
        success();
}

int dierand()
{  return (1 + rand()%6);  }

void win()
{
        cout << "\n\n***YOU WIN!!!***\n";
        GAMECOUNT++;
        WINCOUNT++;
}

void lose()
{
        cout << "\n\n***YOU LOSE!!!***\n";
        GAMECOUNT++;
}

void success()
{
        float x = (WINCOUNT / GAMECOUNT) * 100;
        cout << setiosflags(ios::fixed);
        cout << "\nYou have played " << setprecision(0) <<GAMECOUNT << " games, and won "
                << WINCOUNT << " of them.\nThat gives you a success rate of " << setprecision(1)
                << x << "%.\n";
}