4 ms·
You just have to change the way you think about the game. Rather than viewing the game as a wholistic "Game" object which changes state as the game progresses,
by gamegoblin 12y ago
You just have to change the way you think about the game. Rather than viewing the game as a wholistic "Game" object which changes state as the game progresses, view it instead as a series of game states or frames which are immutable.
Then you define a function that is like (in C-ish):
GameState tick(GameState current, Action playerMove) {
... set up the next frame and return it ...
}
And a function to play like:
void play() {
play(initialState());
}
void play(GameState current) {
if(current.gameOver()) return;
Action playerMove = getPlayerMove();
GameState next = tick(current, playerMove);
play(next); // In Haskell and C on higher optimizations, this tail recursive call will get compiled to a loop
}
Of course it can get a bit more complicated depending on the context, but you get the idea.
So I'd say functional programming is neither better nor worse. A lot of people get hung up on the idea of immutability, but really I've found it to be a non-issue.