[CoLoCo] dice rolls in C
TJ Heaney
tjheaney at gmail.com
Wed Feb 6 18:16:15 GMT 2008
I actually found one of my old C++ projects. Sucks that I was required by
the instructor to use Visual Studio .NET.
It was specifically for dice rolling.
Source is as follows:
/*****************************
*Name: Tom Heaney *
*Purpose: Dice Roller *
*Lab 6 CS-115 CSA *
*Using VS.net *
*Die.h *
*****************************/
#include <cstdlib> // For the library rand() function
using namespace std;
class Die //class that represents a single die
{
public:
Die(); // Initializes Face data member (REMEMBER: a valid die value
is 1 to 6)
void Roll(); // Sets Face equal to a randomly-generated number
between 1 and 6
int GetFace(); // returns the current value of Face
private:
int Face;
};
/*****************************
*Name: Tom Heaney *
*Purpose: Dice Roller *
*Lab 6 CS-115 CSA *
*Using VS.net *
*DiceRoll.h *
*****************************/
#include "die.h"
class DiceRoll //class that specifies a collection of 3 contained Die
objects
{
public:
void RollDice(); // Calls the Roll() member function on each contained
Die
int GetRollFaces(); // Returns the sum of the current Face values of
Die1, Die2, & Die3
private:
Die Die1; // The three die contained in this class – i.e.,
Die Die2; // objects of this class automatically contain
Die Die3; // three dice!
};
/*****************************
*Name: Tom Heaney *
*Purpose: Dice Roller *
*Lab 6 CS-115 CSA *
*Using VS.net *
*DiceRoll.cpp *
*****************************/
#include "DiceRoll.h"
void DiceRoll::RollDice() //call function to roll dice 3 times
{
DiceRoll::Die1.Roll();
DiceRoll::Die2.Roll();
DiceRoll::Die3.Roll();
}
int DiceRoll::GetRollFaces()
{
return (DiceRoll::Die1.GetFace() + DiceRoll::Die2.GetFace() + DiceRoll::
Die3.GetFace()); //gets total value
}
/*****************************
*Name: Tom Heaney *
*Purpose: Dice Roller *
*Lab 6 CS-115 CSA *
*Using VS.net *
*Die.cpp *
*****************************/
#include "die.h"
#include <cstdlib>
Die::Die()
{
Roll();
}
void Die::Roll()
{
Die::Face = rand()%6; // Generate remainder between 0 and 5
Die::Face++; // Scoot up remainder to 1 to 6
}
int Die::GetFace()
{
return Die::Face;
}
/*****************************
*Name: Tom Heaney *
*Purpose: Dice Roller *
*Lab 6 CS-115 CSA *
*Using VS.net *
*main.cpp *
*****************************/
#include <iostream>
#include "DiceRoll.h"
#include <time.h> // using for counter
using namespace std;
void GatherStats(int RollsArray[ ], int RollsArraySize, int ResultsArray[
]) // prototype
{
int iLoopCount = 0;
while(iLoopCount < RollsArraySize)
{
ResultsArray[ RollsArray[iLoopCount] - 3 ]++;
iLoopCount++;
}
}
void DisplayResults(int ResultsArray[ ], int ResultsArraySize ) //prototype
{
int iLoopCount = 0;
while(iLoopCount < ResultsArraySize)
{
cout << "The Dice rolled " << iLoopCount + 3 << " : " <<
ResultsArray[iLoopCount] << " number of times." << endl;
iLoopCount++;
}
}
int main()
{
int RollsArray[200];
int ResultsArray[16];
int iLoopCount = 0;
DiceRoll ThemBones; //call function to roll dice
srand(time(0)); //add random
memset(ResultsArray, 0, sizeof(ResultsArray)); // set bytes in memory
while(iLoopCount < 200) //keeping the dice rolling
{
ThemBones.RollDice();
RollsArray[iLoopCount] = ThemBones.GetRollFaces();
iLoopCount++;
}
GatherStats(RollsArray, 200, ResultsArray);
DisplayResults(ResultsArray, 16);
return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.ubuntu.com/archives/ubuntu-us-co/attachments/20080206/4ea1b54d/attachment-0001.htm
More information about the Ubuntu-us-co
mailing list