title: How to build a 2D board using Swift?
body: <p>I want to create a 2D game board (like a chess board) and each square has a value inside (Int = 1). This board has initially 9 squares (3x3).</p>
  
  <p>Then I need a player that starts at the center of the bard and when I move the player to the edge of the board, it expands.</p>
  
  <p>Example: The player reaches the [3,3] square and the board grows to a 4x4 board.</p>
  
  <p>Also, when as the player moves it will change the value inside each square, from 0 to 1.</p>
  
  <p>This is what I have so far</p>
  
  <pre><code>//Variables to get the player position and expand the board
  let initialSize = 3
  var newValueX = 0
  var newValueY = 0
  var newPlayerX = 0
  var newPlayerY = 0
  //Board
  var board= [[Int]]()
  
  struct Player {
      var X = 0
      var Y = 0
  }
  
  func expandBoard (){
      for k in 0..&lt;9{
          for i in 0..&lt;initialSize {
              for j in 0..&lt;initialSize {
                  array[k].append([i,j])
              }
          }
      }
  }
  
  func movePlayer(){
      var player = Player() //this goes to viewDidLoad
      player.X = newPlayerX
      player.Y = newPlayerY
  }
  </code></pre>
  
  <p>An example of the pretended output:
  Player moved to [0,3] and changed the square value from 1 to 0.</p>
  
  <p>How can I do this correctly? </p>
  
  <p>EDIT:</p>
  
  <p>I have</p>
  
  <pre><code>class Array2D&lt;T&gt; {
      var columns = 1//: Int
      var rows = 1//: Int
  
      var array: Array&lt;T?&gt;
  
       init(columns: Int, rows: Int) {
          self.columns = columns
          self.rows = rows
  
          array = Array&lt;T?&gt;(repeating: nil, count:rows * columns)
      }
  
      subscript(column: Int, row: Int) -&gt; T? {
          get {
              return array[(row * columns) + column]
           }
          set(newValue) {
              array[(row * columns) + column] = newValue
          }
       }
  }
  
  func move(PX: Int, PY: Int){
      if (board[PX, PY] == true){
          moveCount += 1
          board[PX, PY] = false
      }
      if (PX == board.rows.magnitude){
          board.rows += 1
      }
      if (PY == board.columns.magnitude){
          board.columns += 1
      }
  }
  </code></pre>
  
  <p>And when I run i get "Thread 1: Fatal error: Index out of range" on line (return array[(row * columns) + column]) from the subscript.
  Any Idea how can I fix this?</p>