Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/GameDie.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions GameDie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
#include <cstdlib>
#include <ctime>

/*
* 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<double> 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()
{
Expand Down Expand Up @@ -30,6 +47,21 @@ GameDie::GameDie(unsigned int num)

}

GameDie::vector<double> get_percentages(){
vector<double> percentages;
vector<int> 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()
Expand Down
4 changes: 3 additions & 1 deletion GameDie.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ class GameDie
GameDie(unsigned int);
int roll();
vector <int> get_distribution();
vector<double> get_percentages();

private:
vector <int> roll_counter;

vector <int> counter;
const static int FACES = 6;
};

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
`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)