Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It can be optimized here #5

Open
diamont1001 opened this issue May 10, 2022 · 0 comments
Open

It can be optimized here #5

diamont1001 opened this issue May 10, 2022 · 0 comments

Comments

@diamont1001
Copy link

@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
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant