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
24 changes: 24 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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: make
18 changes: 18 additions & 0 deletions GameDie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
#include <vector>
#include <cstdlib>
#include <ctime>
#include<iostream>

int main() {
return 0;
}

//class constructor that seeds the random number generator
GameDie::GameDie()
{
srand(time(NULL));
roll_counter.resize(FACES);
roll_percentages.resize(FACES);

for(int i=0; i<FACES; i++)
roll_counter[i] = 0;
Expand All @@ -19,13 +25,16 @@ GameDie::GameDie(unsigned int num)
if( num == 0 )
{
roll_counter.resize(FACES);
roll_percentages.resize(FACES);
}
else{
roll_counter.resize(num);
roll_percentages.resize(num);
}
for(int i=0; i<FACES; i++)
{
roll_counter[i] = 0;
roll_percentages[i] = 0.0;
}

}
Expand All @@ -36,6 +45,7 @@ int GameDie::roll()
{
int roll = rand() % roll_counter.size();
roll_counter[roll]++;
num_rolls++;
return roll + 1;
}

Expand All @@ -44,3 +54,11 @@ int GameDie::roll()
vector <int> GameDie::get_distribution(){
return roll_counter;
}

vector<double> GameDie::get_percentages() {
for (int i = 0; i < (int) roll_counter.size(); i++) {
roll_percentages[i] = roll_counter[i]/(double) num_rolls;
}
return roll_percentages;
}

3 changes: 3 additions & 0 deletions GameDie.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ class GameDie
GameDie(unsigned int);
int roll();
vector <int> get_distribution();
vector<double> get_percentages();

private:
vector <int> roll_counter;
vector <double> roll_percentages;
const static int FACES = 6;
int num_rolls = 0;
};

#endif
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/no-bugs-only-features/GameDie/actions/workflows/build.yml/badge.svg)](https://github.com/no-bugs-only-features/GameDie/actions/workflows/build.yml)