title: How to keep model & controller separate from a CALayer based UI?
body: <p>I'm trying to re-implement an old <a href="http://code.brautaset.org/Desdemona/" rel="nofollow noreferrer">Reversi board game</a> I wrote with a bit more of a snazzy UI. I've looked at Jens Alfke's GeekGameBoard code for inspiration, and CALayers looks like the way to go for implementing the UI.</p>
  
  <p>However, there is no clean separation of model and view in the GeekGameBoard code; the model <strong>is</strong> the view, which makes it hard to, for example, make a copy of the game state in order to perform game-tree search for the AI player. However, I don't seem to be able to come up with an alternative way to structure that allows a separation of model and view that doesn't involve a constant battle to keep two parallel grids (on for the model, one for the view) in synch. This, of course, has its own problems.</p>
  
  <p>How do I best best implement the relationship between an AI search-friendly model structure and a display-friendly view? Any suggestions / experiences would be appreciated. I'm dreading / half expecting an answer along the lines of "there is no good answer: deal with it as best you can" but I'm prepared to be surprised!</p>
  
  <hr>
  
  <p>Thanks for the answer Peter. I'm not entirely sure I understand it fully, however. I can see how this works if you just have an initial set of pieces that are moved around, and even removed, but what happens when a person puts a new piece down? Would it work like this:</p>
  
  <ol>
  <li>User clicks in the view.</li>
  <li>View click is translated to a board location and controller is notified.</li>
  <li>Controller creates a new Board with the successor state (if appropriate, i.e. it was a legal move).</li>
  <li>The view picks up the new board via its bindings, tears down the existing view/layer hierarchy and replaces it with the current state.</li>
  </ol>
  
  <p>Does that sound right?</p>
  
  <p>PS: Sorry for failing to specify whether it was for the iPhone or Mac. I'm most interested in something that works for the iPhone, but if I can get it to work nicely on the Mac first I'm sure I can adapt the solution to work on the iPhone myself. (Or post a new question!)</p>