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?


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.

  • Could you expand upon this more? – user40980 Mar 2 '15 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.