Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Amber Kommavongsa
- //test cases
- //input: 3 output: INVALID, MENU
- // single chip: (input was 1,2) Output: [2.0 1.5 2.0 1.5 1.0 0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0] WINNINGS: $0 MENU// the path never goes below zero
- // single chip: (input 1,7) output: [7.0 7.5 8.0 7.5 6.0 6.5 7.0 7.5 7.0 6.5 6.0 5.5 5.0] WINNINGS: $0 MENU //path never goes above eight
- //multiple chips: (input was 2, 100, 1) output: WINNINGS: $29600.00 Average: $296.00 MENU
- //input: 3 output: GOODBYE
- #include <iostream>
- #include <iomanip>
- #include <ctime>
- #include <cstdlib>
- #include <limits>
- using namespace std;
- //FUNCTIONS
- int falling_through_slots(int slot, bool output)//simulates the chip falling through the twelve slots
- {
- double finalslot = slot;
- double const SHIFT = .5;
- int r = rand() % 2;
- for (int peg_counter = 0; peg_counter < 12; peg_counter++)//simulates where the chip falls for each chip
- {
- int r = rand() % 2;
- if (finalslot < 8 && r == 1 || finalslot == 0)//moves right
- {
- finalslot += SHIFT;
- }
- else if (finalslot > 0 && r == 0 || finalslot == 8)//moves left
- {
- finalslot -= SHIFT;
- }
- if (output == false)
- {
- }
- if (output == true)
- {
- cout << fixed << setprecision(1) << " " << finalslot;
- }
- }
- return finalslot;
- }
- double prize_moneyTotal(int finalslot)//computes total prize money
- {
- const double PRIZE0 = 100.00;
- const double PRIZE1 = 500.00;
- const double PRIZE2 = 1000.00;
- const double PRIZE3 = 0;
- const double PRIZE4 = 10000.00;
- const double PRIZE5 = 0;
- const double PRIZE6 = 1000.00;
- const double PRIZE7 = 500.00;
- const double PRIZE8 = 100.00;
- int slotfinal = finalslot;
- double prize = 0;
- switch (slotfinal)
- {
- case 0: prize += PRIZE0; break;
- case 1: prize += PRIZE1; break;
- case 2: prize += PRIZE2; break;
- case 3: prize += PRIZE3; break;
- case 4: prize += PRIZE4; break;
- case 5: prize += PRIZE5; break;
- case 6: prize += PRIZE6; break;
- case 7: prize += PRIZE7; break;
- case 8: prize += PRIZE8; break;
- }
- return prize;
- }
- //function that tests the validity of the user input, must reject the user input if string or invalid number *hardest part
- bool TEST_userinput(int min, int max, int userinput)
- {
- int result = userinput;
- if (min < userinput && userinput < max)
- return true;
- else if (userinput == min || userinput == max)
- return true;
- else if (userinput < min || userinput > max)
- {
- cout << "Invalid character, please input a valid number:\n";
- return false;
- }
- else if ('a' < userinput && userinput < 'z')
- {
- cout << "Invalid character, please input a valid number:\n";
- return false;
- }
- else if ('A' < userinput && userinput < 'Z')
- {
- cout << "Invalid character, please input a valid number:\n";
- return false;
- }
- else if ('A' == userinput || 'Z' == userinput || 'a' == userinput || 'z' == userinput)
- {
- cout << "Invalid character, please input a valid number:\n";
- return false;
- }
- }
- int main()
- {
- const int QUIT = 4;
- const bool VALID = true;
- const bool INVALID = false;
- const bool SHOW_OUTPUT = true;
- const bool NO_OUTPUT = false;
- const int SLOT_MIN = 0;
- const int SLOT_MAX = 8;
- const int max_chips = std::numeric_limits<int>::max();
- int userchoice = 0;
- double select_slot;
- double total_prize;
- int chips;
- bool valid_response = true;
- srand(time(0));
- while (userchoice != QUIT)
- {
- cout << "MENU: Please select one of the following options:\n";
- cout << "1 - Drop a single chip into one slot\n";//first game that can be played
- cout << "2 - Drop Multiple chips into one slot\n";//2nd game that can be played
- cout << "3 - Drop Multiple chips into multiple slots\n";
- cout << "4 - Quit the Program\n";
- int userchoice;
- cin >> userchoice;
- valid_response = TEST_userinput(1, 4, userchoice);
- if (userchoice == 1)
- {
- cout << "***SINGLE CHIP***\n";
- cout << "Pick a slot numbers 0 - 8 to drop the chip:";
- cin >> select_slot;
- valid_response = TEST_userinput(SLOT_MIN, SLOT_MAX, select_slot);//validates the user's input
- if (valid_response == true)//ONly continues on if user input is true;
- {
- cout << "\[ " << setprecision(1) << select_slot;
- select_slot = falling_through_slots(select_slot, SHOW_OUTPUT);//prints out the location of the chip
- cout << "\]\n";
- total_prize = prize_moneyTotal(select_slot);//computes total prize money
- cout << "Money Won: $" << fixed << setprecision(2) << total_prize << endl;
- }
- }
- if (userchoice == 2)
- {
- double prize;
- double total_prize = 0;
- cout << "***MULTIPLE CHIPS***\n";
- cout << "How many chips would you like to drop:";
- cin >> chips;
- valid_response = TEST_userinput(1, max_chips, chips);
- if (valid_response == VALID)
- {
- cout << "Select a slot 0 - 8 to drop the chips:";
- cin >> select_slot;
- valid_response = TEST_userinput(SLOT_MIN, SLOT_MAX, select_slot);
- if (valid_response == VALID)
- {
- for (int i = 0; i < chips; i++)//drops multiple chips
- {
- double final_slot = select_slot;//resets the selected slot each time to the number the user input
- final_slot = falling_through_slots(select_slot, NO_OUTPUT);
- prize = prize_moneyTotal(final_slot);
- total_prize += prize;
- }
- double prize_average = total_prize / chips;//computes average
- cout << "Total won: $" << fixed << setprecision(2) << total_prize << endl;//outputs the total money earned
- cout << "Average won per chip: $" << fixed << setprecision(2) << prize_average << endl;
- }
- }
- }
- if (userchoice == 3)
- {
- cout << "**MULTIPLE CHIPS IN MULTIPLE SLOTS**\n";
- cout << "Indicate how many chips would you like to drop into each slot:";
- cin >> chips;
- valid_response = TEST_userinput(1, max_chips, chips);
- if (valid_response == VALID)
- {
- for (int slot_number = SLOT_MIN; slot_number <= SLOT_MAX; slot_number++)//moves the slot number up one for each time it goes through
- {
- double total_prize = 0;
- int final_slot = slot_number;
- for (int i = 0; i < chips; i++)
- {
- final_slot = falling_through_slots(slot_number, NO_OUTPUT);
- double prize = prize_moneyTotal(final_slot);
- total_prize += prize;
- }
- double prize_average = total_prize / chips;
- cout << "For SLOT " << slot_number << " you won: $" << fixed << setprecision(2) << total_prize << "\nand per chip: $" << fixed << setprecision(2) << prize_average << endl;
- cout << "\n";
- }
- }
- }
- while (userchoice == 4)
- {
- cout << "GOODBYE" << endl;
- system("pause");
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement