Skip to content

Instantly share code, notes, and snippets.

@mclaughj
Last active December 24, 2015 11:19
Show Gist options
  • Save mclaughj/6789842 to your computer and use it in GitHub Desktop.
Save mclaughj/6789842 to your computer and use it in GitHub Desktop.
A little program I helped a friend write for a C++ intro to programming class he's taking. It's a basic Plinko simulator. I know there are probably better ways to do the same thing, but this was the final evolution of the program that allowed me to teach simple loops, if statements, and functions.
#include <iostream>
int simulatedrop(int slot, int printslots)
{
float location = slot;
for (int i = 0; i < 12; i++) {
int random = rand() % 10;
if (random < 5){
location -= 0.5;
}
else
{
location += 0.5;
}
if (location < 0)
{
location = 0.5;
}
else if (location > 8)
{
location = 7.5;
}
if (printslots == 1)
{
std::cout << location << std::endl;
}
}
int reward = 0;
if (location == 0){reward = 100;}
else if (location == 1){reward = 500;}
else if (location == 2){reward = 1000;}
else if (location == 3){reward = 0;}
else if (location == 4){reward = 10000;}
else if (location == 5){reward = 0;}
else if (location == 6){reward = 1000;}
else if (location == 7){reward = 500;}
else if (location == 8){reward = 100;}
return reward;
}
int main( int argc, const char* argv[] )
{
int menu = 1;
while (menu == 1)
{
std::cout << "Please enter one of the following options to continue:" << std::endl;
std::cout << "Drop One (d), Drop Multiple (m), Quit (q): ";
char input;
std::cin >> input;
if (input == 'd')
{
std::cout << "Please select a slot to drop your chip in (0-8): ";
int slot;
std::cin >> slot;
if (slot >= 0 && slot <= 8)
{
int reward = simulatedrop(slot, 1);
std::cout << "Winnings: $" << reward << std::endl;
}
}
else if (input == 'm')
{
std::cout << "Please enter the number of chips to drop: ";
int numchips;
std::cin >> numchips;
if (numchips > 0)
{
std::cout << "Please select a slot to drop all of your chips in (0-8): ";
int slot;
std::cin >> slot;
if (slot >= 0 && slot <= 8)
{
double totalwinnings = 0;
for (int i = 0; i < numchips; i++) {
totalwinnings += simulatedrop(slot, 0);
}
double average = totalwinnings / numchips;
std::cout << "Average winnings per chip: $" << average << std::endl;
std::cout << "Total winnings: $" << totalwinnings << std::endl;
}
}
}
else if (input == 'q')
{
menu = 0;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment