Need help with STT-C-Compile-Time-Snake?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

mattbierner
199 Stars 10 Forks MIT License 16 Commits 0 Opened issues

Description

Snake/Nibbler implementation using C++ template metaprogamming

Services available

!
?

Need anything else?

Contributors list

# 59,483
Swift
Objecti...
photosh...
React
12 commits
# 270,257
cpluspl...
sqlite-...
SQL
SQLite
1 commit

Compile Time C++ Snake Game

Implementation of Snake / Nibbler using C++14 template metaprogramming. All the logic is implemented at compile time. When the program is run, it prints out the result of gameplay. Here's a post covering the implementation in more detail.

Two versions are included: * An "interactive" game that saves state between compiler runs. Each compile advances one turn. Found on the

interactive
branch. * A static version that plays the entire game in a single compile. This is the version on the main branch.

Interactive

Plays one step of the game every time the game is recompiled. Uses compiler flags to control input:

$ clang++ -std=c++1y main.cpp -D COMMAND -o snake ; ./snake

Valid commands are UP, DOWN, LEFT, and RIGHT. If no command is entered, the snake will go straight. You can reset to the original state by running

./reset.sh
.
bash-3.2$ ./reset.sh 
bash-3.2$ clang++ -std=c++1y main.cpp  -o snake ; ./snake
------------------
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺▶*╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
bash-3.2$ clang++ -std=c++1y main.cpp -D RIGHT -o snake ; ./snake
------------------
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺*╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺▶▶╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
bash-3.2$ clang++ -std=c++1y main.cpp -o snake ; ./snake
------------------
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺*╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺▶▶╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
bash-3.2$ clang++ -std=c++1y main.cpp -D UP -o snake ; ./snake
------------------
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺*╺╺
╺╺╺╺╺╺╺▲╺╺
╺╺╺╺╺╺╺▶╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
bash-3.2$ clang++ -std=c++1y main.cpp -o snake ; ./snake
------------------
╺*╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺▲╺╺
╺╺╺╺╺╺╺▲╺╺
╺╺╺╺╺╺╺▶╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
bash-3.2$ clang++ -std=c++1y main.cpp -o snake ; ./snake
------------------
╺*╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺▲╺╺
╺╺╺╺╺╺╺▲╺╺
╺╺╺╺╺╺╺▲╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
bash-3.2$ clang++ -std=c++1y main.cpp -D RIGHT -o snake ; ./snake
------------------
╺*╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺▲▶╺
╺╺╺╺╺╺╺▲╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
bash-3.2$ clang++ -std=c++1y main.cpp -o snake ; ./snake
------------------
╺*╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺▲▶▶
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
bash-3.2$ clang++ -std=c++1y main.cpp -o snake ; ./snake
-- You Are Dead --
╺*╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺▲▶█
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺

Static

Plays the entire game in a single compiler run. This code is on the main branch.

int main(int argc, const char* argv[])
{
    using inputs = PlayerInput<
        Input::Right, Input::Up, Input::None, Input::Right, Input::Up,
        Input::None, Input::None, Input::Left, Input::None, Input::None,
        Input::None, Input::None, Input::None, Input::Down, Input::None,
        Input::None, Input::None, Input::None, Input::None, Input::None,
        Input::None, Input::Right, Input::Up, Input::Left>;

using state = InitialState;

using game = play_t<inputs state>;

Printer<game>::Print(std::cout);

return 0;

}

------------------
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺▶*╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺


╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺*╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺▶▶╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺*╺╺ ╺╺╺╺╺╺▲╺╺╺ ╺╺╺╺╺╺▶╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺▲*╺╺ ╺╺╺╺╺╺▲╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺*╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺▲▶╺╺ ╺╺╺╺╺╺▲╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺*╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺▲╺╺ ╺╺╺╺╺╺▲▶╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺*╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺▲╺╺ ╺╺╺╺╺╺╺▲╺╺ ╺╺╺╺╺╺╺▶╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺*╺╺╺╺╺▲╺╺ ╺╺╺╺╺╺╺▲╺╺ ╺╺╺╺╺╺╺▲╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺*╺╺╺╺◀▲╺╺ ╺╺╺╺╺╺╺▲╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺*╺╺╺◀◀▲╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺*╺╺◀◀◀╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺*╺◀◀◀╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺*◀◀◀╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺◀◀◀◀╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺*╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺◀◀◀╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺*╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺◀◀╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺*╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺◀╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺*╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺╺╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺*╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺*╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺*╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺╺*╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼*╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ *╺╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼▶╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺


╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ *╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺▼▲╺╺╺╺╺╺╺ ╺▼▶╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ------ dead ------ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ *╺╺╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ ╺▼╺╺╺╺╺╺╺╺ ╺█▲╺╺╺╺╺╺╺ ╺▼▶╺╺╺╺╺╺╺ ╺╺╺╺╺╺╺╺╺╺ Program ended with exit code: 0

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.