feat: implemented dice rolling

This commit is contained in:
Sara Gerretsen 2025-10-28 11:59:27 +01:00
parent ddf9936f58
commit ae334c395a
4 changed files with 127 additions and 17 deletions

View file

@ -1,15 +1,22 @@
#include "dice_data.h"
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
namespace active_dice {
struct DieState {
Die type;
uint8_t value;
};
constexpr size_t maxActiveDice{16};
Die activeDice[maxActiveDice];
DieState activeDice[maxActiveDice];
size_t numActiveDice{0};
void Add(Die die) {
if(numActiveDice < maxActiveDice) {
activeDice[numActiveDice] = die;
activeDice[numActiveDice].type = die;
activeDice[numActiveDice].value = RollDie(die);
++numActiveDice;
}
}
@ -19,18 +26,37 @@ void Remove(size_t at) {
return;
}
if(at < numActiveDice - 1) {
std::memmove(activeDice + at, activeDice + at + 1, numActiveDice - at);
std::memmove(activeDice + at, activeDice + at + 1, sizeof(DieState) * (numActiveDice - at));
}
--numActiveDice;
}
Die Get(size_t at) {
if(at < numActiveDice) {
return activeDice[at];
return activeDice[at].type;
} else return D_COIN;
}
uint8_t GetValue(size_t at) {
if(at < numActiveDice) {
return activeDice[at].value;
} else return 0;
}
size_t Count() {
return numActiveDice;
}
void ReRoll() {
for(size_t i{0}; i < numActiveDice; ++i) {
activeDice[i].value = RollDie(activeDice[i].type);
}
}
}
uint8_t RollDie(Die die) {
if(die == D_COIN) {
return std::rand() % 2;
}
return 1 + std::rand() % (die-1);
}