2

I wanted to implement the game of Tic Tac Toe using a functional language (in my case, Scala) but I'm unsure how to go about managing the board game state after each player makes their move.

I understand that FP avoids mutating global state, but how does this work in the case of a board game?

jcm
  • 229

1 Answers1

3

State + move = new state

Both old state and new state are immutable. Move doesn't mutate board state, but creates a copy of the old state with move added.

Imagine an inefficient implementation where board state is just a linked list of moves taken and each new move just adds another head to the linked list. For instance if I am holding a reference to a specific head of linked list of 5 moves (representing board state), adding more moves doesn't change anything as far as that value is concerned, it still points of a list of 5 moves even if the whole list is now 9 moves long. Immutability.

GitRDun
  • 41