Skip to content

It can be optimized here #5

@diamont1001

Description

@diamont1001

@discardableResult fileprivate func generateNewBlocks() -> Bool {

This function can be optimized like this so that it can create any nums of blocks.

@discardableResult fileprivate func generateNewBlock() -> Bool {
      var blankLocations = [BlockMatrixType.Index]()
      for rowIndex in 0..<4 {
          for colIndex in 0..<4 {
              let index = (colIndex, rowIndex)
              if _blockMatrix[index] == nil {
                  blankLocations.append(index)
              }
          }
      }
      
      guard blankLocations.count >= 1 else {
          return false
      }
      
      // Don't forget to sync data.
      defer {
          objectWillChange.send(self)
      }
      
      _blockMatrix.place(IdentifiedBlock(id: newGlobalID, number: 2), to: blankLocations.randomElement()!)
      
      return true
  }
  
  @discardableResult fileprivate func generateNewBlocks(_ num: Int = 1) -> Bool {
      guard num > 0 else {
          return false
      }
      
      for _ in 0..<num {
          if !generateNewBlock() {
              return false
          }
      }
      
      return true
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions