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++ GameDie.cpp -std=c++17 -Wall -Werror
24 changes: 24 additions & 0 deletions GameDie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,27 @@ int GameDie::roll()
vector <int> GameDie::get_distribution(){
return counter;
}

// 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.
vector <double> GameDie::get_percentages(){
vector <double> result;
int total = 0;
result.resize(counter.size());
for(unsigned int i = 0; i < counter.size(); i++) {
total += counter[i];
}
if(total == 0) {
return result;
}
for(unsigned int i = 0; i < counter.size(); i++) {
result[i] = counter[i]/(double)total;
}
return result;
}
1 change: 1 addition & 0 deletions GameDie.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class GameDie
GameDie(unsigned int);
int roll();
vector <int> get_distribution();
vector <double> get_percentages();

private:
vector <int> counter;
Expand Down