title: Problems designing Bejeweled game
body: <p>I am trying to do the design of a <a href="http://en.wikipedia.org/wiki/Bejeweled" rel="nofollow noreferrer">Bejeweled</a> game. I have basically 3 classes. The <code>Game</code> class, which is what is going to be used by the player, a <code>Board</code> class, that represents the board, and a <code>SwitchController</code> class that is responsible for checking if the wanted switch on the board is valid, making the switch, counting the number of possible switches available (so I can know when the game is over, etc).</p>
  
  <p>My current design is something like the following:</p>
  
  <pre><code>Game:
  isGameOver()
  isSwitchValid(coord1, coord2)
  makeSwitch(coord1, coord2)
  getPieceAt(coord)
  getBoardLength()
  
  IBoard:
  getPieceAt(coord)
  setPieceAt(coord, piece)
  getLength()
  </code></pre>
  
  <p>My idea would then to have a <code>ISwitchController</code>:</p>
  
  <pre><code>ISwitchController:
  isSwitchValid(coord1, coord2)
  makeSwitch(coord1, coord2)
  getAllValidSwitches()
  </code></pre>
  
  <p>Here is a little diagram of how the classes are to be organized:
  <img src="https://yuml.me/diagram/scruffy/class/%5BGame%5D-%3E%5BISwitchController%5D,%20%5BISwitchController%5D-%3E%5BIBoard%5D,%20%5BGame%5D-%3E%5BIBoard%5D" alt="alt text"></p>
  
  <p>I would have 2 different concrete classes of <code>IBoard</code> available for use (and for each one of them, I'd have to have an <code>ISwitchController</code> implementation).</p>
  
  <h2>The problem:</h2>
  
  <p>My program is to have 2 IBoard implementations:</p>
  
  <p>The first, <code>ArrayBoard</code>, will have all the pieces of the board stored in a 2D Array. There is nothing special about it. I will define an <code>ArrayBoardSwitchController</code> for managing this class.</p>
  
  <p>The second, <code>ListBoard</code>, will have for each color of pieces, a List/Set with all the coordinates of the pieces of that color. I will define a <code>ListBoardSwitchController</code> for managing this class.</p>
  
  <p>The main issue here is that the implementation of <code>SwitchController</code> will be totally different on <code>ArrayBoard</code> and on <code>ListBoard</code>. For example, while for implementing <code>getAllValidSwitches()</code> <code>ArrayBoardSwitchController</code> only needs the <code>getPieceAt()</code> method, that would not be a good idea to do with <code>ListBoardSwitchController</code>(in that class I use internally lists because it's easier to check if the move is valid that way). </p>
  
  <p>From what I can see, there are 2 different possible solutions:</p>
  
  <ol>
  <li><p>I could either merge together the
  <code>ISwitchController</code> and <code>IBoard</code>
  interfaces. That way I'd only have
  two classes, Game and Board (while
  basically Game would just be a
  controller for the Board, as it
  would be the Board that had all the
  game logic). It wouldn't be that nice
  because the classes wouldn't be
  as cohese as they could be if I had
  3 distinct classes.</p></li>
  <li><p>Let the interfaces as they are and put
  all the methods I need to work with public
  in the concrete classes. For example, if I need
  a <code>getYellowPiecesList()</code> method, I'd put it public
  on <code>ListBoard</code> so <code>ListBoardSwitchController</code> could
  use it. <code>ListBoardSwitchController</code> would only
  know about it because it knows it only works
  against <code>ListBoards</code>.</p></li>
  </ol>
  
  <p>What's your opinion on the matter? <em>The focus here is not so much on how to design the Bejeweled game, but how to solve this problem, that is recurrent when you try to implement algorithms: on one hand you want to have a clear and good OOP design, and in the other sometimes that gets in the way of having a sound and effective algorithm implementation.</em></p>