aboutsummaryrefslogtreecommitdiffstats
path: root/ee1301/wk6/hw6_directory/strap012_HW6A.cpp
blob: c255ade6adda35d6dab90e3f8e9c5750a79996e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//Matthew Strapp
//EE1301
//17 April 2019
//HW 6A: Dice Class
#include <iostream>
#include <cstdlib>
using namespace std;

const int maxNumDie=50;

int* userInputParser(string s);

class Dice {
private:
  int min;
  int max;
public:
  int roll(int min, int max) {
    return rand() % (max-min+1) + min;
  };
  Dice() { //Default constructor for debugging purposes
      min=1;
      max=1;
  }
  Dice(int gotMin, int gotMax){
      min = gotMin;
      max = gotMax;
  };
};

int main() {
    int rounds;
    Dice die[maxNumDie];
    int roll[maxNumDie];
    int max=0, min=999999, sum=0, sample=0;
    double avg;
    srand(time(NULL)); // DO NOT WRITE THIS LINE AGAIN OR ANYWHERE ELSE
    cout << "What do you want to roll?  ";
    string s;
    getline(cin, s);
    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,
    //  ... }
    double numRolls=0;
    for (int j=0; j<rounds; j++) {
      int curRoll=0;
      numRolls+=1.0;
      for(int i=1; i < pairs[0]; i+=2) {
        die[i-1] = Dice(pairs[i], pairs[i + 1]);
        roll[i-1] = die[i-1].roll(pairs[i], pairs[i+1]);
        curRoll += roll[i-1];
      }
      
      if (curRoll > max) {
        max = curRoll;
      }
      if (curRoll < min) {
        min = curRoll;
      }
      sum+=curRoll;
  }
  avg = sum / numRolls;
  cout << "Sample roll: " << sample << endl
       << "Minimum roll: " << min << endl
       << "Maximum roll: " << max << endl
       << "Average roll: " << avg << endl;

}


int* userInputParser(string s) {
    static int dice[2*maxNumDie+1] = {0}; // array format:
    // {num_dice,
    //  first_die_start,
    //  first_die_end,
    //  second_die_start,
    //  ... }
    // max of maxNumDie dice supported

    string data[4*maxNumDie];  // Intermediate storage for parsing input string

    // count how many '+'s or 'd's there are...
    int parts = 0;
    for(unsigned int i=0; i < s.length(); i++)
    {
        if(s[i] == 'd' || s[i] == '+')
        {
            parts++;
        }
    }
    // ... so we know the number of times to decode values

    int index=0;
    unsigned d = s.find('d');
    unsigned p = s.find('+');
    while(d != static_cast<unsigned>(-1) || p != static_cast<unsigned>(-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<signed>(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<signed>(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;
}