From b84ee7663cdd4afbfd575068bdd8e124d58959f8 Mon Sep 17 00:00:00 2001 From: Matthew Strapp Date: Sun, 14 Apr 2019 11:35:35 -0500 Subject: Start 6A --- ee1301/wk6/dice.cpp | 178 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 ee1301/wk6/dice.cpp diff --git a/ee1301/wk6/dice.cpp b/ee1301/wk6/dice.cpp new file mode 100644 index 0000000..ebdb7d6 --- /dev/null +++ b/ee1301/wk6/dice.cpp @@ -0,0 +1,178 @@ +#include +#include +using namespace std; + +const int maxNumDie=50; + +int* userInputParser(string s); + +class Dice { +private: + int min; + int max; + +public: +// void roll(); +}; + +int main() +{ + srand(time(NULL)); // DO NOT WRITE THIS LINE AGAIN OR ANYWHERE ELSE + cout << "What do you want to roll? "; + string s; + getline(cin, s); + cin.ignore(); + cout << "How many rounds do you want to roll?"; + cin >> rounds; + int* pairs = userInputParser(s); + + // This will display the array of die information retrieved from user. + // Replace the following code when you submit your solution. + + // pairs is an array with the following format: + // {num_dice, + // first_die_start, + // first_die_end, + // second_die_start, + // ... } + for(int i=1; i < pairs[0]; i+=2) + { + cout << "["<(-1) || p != static_cast(-1)) + { + bool dFirst = d < p; + if(dFirst) + { + string before = s.substr(0,d); // part before the 'd' (should be just one number) + // figure out what number is after 'd' + int count = 0; + bool foundDigit=false; + for(int i=0; i< static_cast(s.length()-d-1); i++) + { + if(isdigit(s[count+d+1])) + { + foundDigit=true; + } + if(!isdigit(s[count+d+1]) && foundDigit) + { + break; + } + count++; + } + string after = s.substr(d+1,count); //should be just the number after 'd' + + // store these two parts + data[index] = before; + data[index+1] = after; + index+=2; + + + // remove this part from the string s + s = s.substr(d+count+1); // discard these two parts + } + else // same idea for the '+' + { + // figure out what number is after '+' + int count = 0; + bool foundDigit=false; + for(int i=0; i< static_cast(s.length()-p-1); i++) + { + if(isdigit(s[count+p+1])) + { + foundDigit=true; + } + if(!isdigit(s[count+p+1]) && foundDigit) + { + break; + } + count++; + } + string after = s.substr(p+1,count); //should be just the number after '+' + + // store this part + data[index] = "+"; + data[index+1] = after; + index+=2; + + + // remove this part from the string s + s = s.substr(p+count+1); // discard these two parts + } + + // update d and p for next loop interation + d = s.find('d'); + p = s.find('+'); + + } + + // now we need to figure out how many dice there are (as 2d4 is 2 dice) + // we will treat "+2" as a die that rolls [2,2] + int diceCount = 0; + for(int i=0; i < parts*2; i+=2) + { + if(data[i][0] == '+') + { + diceCount++; + } + else + { + diceCount+=atoi(data[i].c_str()); + } + } + + dice[0] = diceCount*2+1; // put size in first index + + int ind=1; // index for the "dice" array (as not same as data array) + for(int i=0; i < parts*2; i+=2) + { + // if we have a +, add a "Dice" that has a range of 0 + if(data[i][0] == '+') + { + dice[ind] = atoi(data[i+1].c_str()); + dice[ind+1] = atoi(data[i+1].c_str()); + + ind+=2; + } + else // otherwise add however many of the dice requested + { + for(int j=0; j < atoi(data[i].c_str()); j++) + { + dice[ind] = 1; + dice[ind+1] = atoi(data[i+1].c_str()); + + ind += 2; + } + } + } + + return dice; +} -- cgit v1.2.3