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?

2
  • 1
    programmers.stackexchange.com/a/164002/31260 Commented Mar 2, 2015 at 12:42
  • Instead of mutating a variable holding the state, you construct a list in which each element represents the state at one point in time. Commented Mar 2, 2015 at 12:50

1 Answer 1

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.

1
  • Could you expand upon this more? Commented Mar 2, 2015 at 12:44

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.