There are more code in the game logic on the C++ side but as it's not directly in the scope of explaining ...
quitcoding.com