Seven boom game by tracking each decimal digit separately instead of looking at the actual number and computing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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