Skip to content

Instantly share code, notes, and snippets.

@guss77
Last active Aug 7, 2021
Embed
What would you like to do?
Seven boom game by tracking each decimal digit separately instead of looking at the actual number and computing
#include <memory>
#include <ranges>
#include <iostream>
class Digit {
unsigned char val, sevenCount;
std::unique_ptr<Digit> moreSignificant;
public:
Digit(unsigned char startVal) : val(startVal), sevenCount(startVal % 7) {}
void inc() {
if (++sevenCount >= 7)
sevenCount = 0;
if (++val > 9) {
val = 0;
if (moreSignificant)
moreSignificant->inc();
else
moreSignificant = std::make_unique<Digit>(1);
}
}
bool hasSeven() {
return val == 7 || (moreSignificant ? moreSignificant->hasSeven() : false);
}
bool isDivisibleBySeven() {
return sevenCount == 0;
}
};
int main() {
Digit num(0);
for (unsigned long long i : std::ranges::views::iota(1ULL, 1000ULL)) { /* while (true) */
num.inc();
if (num.hasSeven() || num.isDivisibleBySeven())
std::cout << "BOOM" << std::endl;
else
std::cout << i << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment