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/build.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
56 changes: 37 additions & 19 deletions GameDie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,59 @@
//class constructor that seeds the random number generator
GameDie::GameDie()
{
srand(time(NULL));
counter.resize(FACES);
srand(time(NULL));
counter.resize(FACES);

for(int i=0; i<FACES; i++)
counter[i] = 0;
for(int i=0; i<FACES; i++)
counter[i] = 0;
}

//overloaded constructor
GameDie::GameDie(unsigned int num)
{
if( num == 0 )
{
counter.resize(FACES);
}
else{
counter.resize(num);
}
for(int i=0; i<FACES; i++)
{
counter[i] = 0;
}
if( num == 0 )
{
counter.resize(FACES);
}
else{
counter.resize(num);
}
for(int i=0; i<FACES; i++)
{
counter[i] = 0;
}

}

//generate a random number between 1-n where n is the counter size
// (inclusive) and return it
int GameDie::roll()
{
int roll = rand() % counter.size();
counter[roll]++;
return roll + 1;
int roll = rand() % counter.size();
counter[roll]++;
return roll + 1;
}

// return the count of how many times each face has been rolled, as a vector
// where each face's count is at index face-1 (i.e. Face 1 is at index 0)
vector <int> GameDie::get_distribution(){
return counter;
return counter;
}

//returns percentage of rolls for each face relative to the number of total rolls
vector <double> GameDie::get_percentages() {
vector <int> rolls;
rolls = get_distribution();
vector <double> res;
int total = 0;

for(int i = 0; i < rolls.size(); i++) {
total += rolls[i];
}

res.resize(rolls.size());

for(int i = 0; i < res.size(); i++) {
res[i] = (double)rolls[i]/(double)total;
}
}
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# GameDie

[![Build C++](https://github.com/mcbillings/GameDie/actions/workflows/build.yml/badge.svg)](https://github.com/mcbillings/GameDie/actions/workflows/build.yml)


This repository provides a class that represents a game die, such as the
six-sided dice used in traditional dice game. While the die defaults to
six sides, the overloaded constructor allows the developer to customize
Expand All @@ -23,4 +26,4 @@ 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`