diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml new file mode 100644 index 0000000..e52e4c4 --- /dev/null +++ b/.github/workflows/actions.yml @@ -0,0 +1,23 @@ +name: Build C++ + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + install: + runs-on: ubuntu-latest + steps: + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y -f build-essential g++ cmake + build: + needs: install + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Build project + run: g++ main.cpp -std=c++17 -o firstIO \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/GameDie.iml b/.idea/GameDie.iml new file mode 100644 index 0000000..bc2cd87 --- /dev/null +++ b/.idea/GameDie.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..6230f3b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/GameDie.cpp b/GameDie.cpp index e9fb279..1d0c760 100644 --- a/GameDie.cpp +++ b/GameDie.cpp @@ -3,6 +3,23 @@ #include #include +/* + * get_distribution() keeps a tally of how many times each face has been rolled, but some users are reporting that + * they want this same information as a percentage of total rolls. + +Add a public member function vector GameDie::get_percentages() that returns + the percentage of rolls for each face relative to the number of total rolls. + Each percentage should be a double between 0 and 1 inclusively. + For example, if we have a 4-sided die that has rolled each face 1 time and has the get_distribution() of: +{1,1,1,1} +then the get_percentages() function should return: +{0.25,0.25,0.25,0.25} + +If there are no rolls yet, percentages should report 0 for each face in the vector. + Otherwise, the percentage should be calculated by face rolls / total rolls. + */ + + //class constructor that seeds the random number generator GameDie::GameDie() { @@ -30,6 +47,21 @@ GameDie::GameDie(unsigned int num) } +GameDie::vector get_percentages(){ + vector percentages; + vector distribution = get_distribution(); + + int totalRolls = 0; + for (int i = 0; i < FACES; i++){ + totalRolls += distribution[i]; + } + for (int i = 0; i < FACES ; ++){ + percentages[i] = distribution / totalRolls; + } + + return percentages; +} + //generate a random number between 1-n where n is the counter size // (inclusive) and return it int GameDie::roll() diff --git a/GameDie.h b/GameDie.h index 939e8f7..13d8a2a 100644 --- a/GameDie.h +++ b/GameDie.h @@ -11,9 +11,11 @@ class GameDie GameDie(unsigned int); int roll(); vector get_distribution(); + vector get_percentages(); private: - vector roll_counter; + + vector counter; const static int FACES = 6; }; diff --git a/README.md b/README.md index 9a8fe2c..51e0c1b 100644 --- a/README.md +++ b/README.md @@ -23,4 +23,6 @@ Once built, run the image: ...or run it with a bind mount to the current source code: -`docker run --mount type=bind,source="$(pwd)",target=/usr/src -it cpp-container` \ No newline at end of file +`docker run --mount type=bind,source="$(pwd)",target=/usr/src -it cpp-container` + +[![Build C++](https://github.com/SloaneTribble/GameDie/actions/workflows/actions.yml/badge.svg)](https://github.com/SloaneTribble/GameDie/actions/workflows/actions.yml) \ No newline at end of file