For a job in company I had to do a code assignment. My assignment performs all the required tasks needed to be done. But after one month the company provided me a feedback and said that we have reviewed your exercise, very sorry for the delay. Unfortunately, while working, the structure and readability of the code are not sufficient, and we will not be proceeding further with your application at this time. The assignment was not to make a big application with a lot of modules. I am not able to figure out how can I improve it. Below is the assignment and my code, please review it and provide me your suggestions. Thanks
import SwiftUI
struct ContentView : View {
@State private var selectedBlocks = [[String:Int]]()
@State private var scoreArray = [[String:Int]]()
@State private var finalScore = ""
@State private var isUserInteractionDisabled = false
private let emptyNeighborScore = 10
private let filledNeighborScore = 5
private let intialBlockScore = 5
private let numberOfColumns = 5
private let numberOfRows = 5
private var resultLength = 10
private var columns: [GridItem] {
Array(repeating: .init(.adaptive(minimum: 60)), count: numberOfColumns)
}
var body: some View {
HStack(spacing: 0){
Text("Score: \(finalScore )")
}.frame(height: 100)
ScrollView(.vertical, showsIndicators: false) {
ForEach(0..<numberOfRows) { rowIndex in
LazyVGrid(columns: columns, spacing: 20) {
ForEach(0..<numberOfColumns) { columnIndex in
let cellIndex: [String:Int] = ["rowIndex":rowIndex, "columnIndex":columnIndex
]
let results = self.scoreArray.filter { $0["rowIndex"] == rowIndex && $0["columnIndex"] == columnIndex
}
Button(action: {
self.updateSelectBlock(rowIndex:rowIndex, columnIndex:columnIndex) // <- on tap update selection
}) {
Text(results.count>0 ? "\(results[0]["score"] ?? 0 )" : " ")
}
.frame(width:50)
.border(Color.black, width: 2)
.background(self.selectedBlocks.contains(cellIndex) ? Color.blue : Color.white)
}
.buttonStyle(PlainButtonStyle())
}
}
}.disabled(isUserInteractionDisabled)
}
func updateSelectBlock(rowIndex:Int, columnIndex:Int) {
isUserInteractionDisabled = true
var cellIndex: [String:Int] = ["rowIndex":rowIndex, "columnIndex":columnIndex
]
if(!self.selectedBlocks.contains(cellIndex) && self.selectedBlocks.count < resultLength) {
self.selectedBlocks.append(cellIndex)
}
else {
return
}
if(rowIndex < numberOfRows-1) {
let rowDifference = (numberOfRows - rowIndex - 2)
var currentRowIndex = rowIndex
var hasNeigbhor = false
var count = 0
_ = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true){ t in
if count == (rowDifference) {
t.invalidate()
isUserInteractionDisabled = false
}
count += 1
let bottomNeighborCheck = checkNeigbhor(rowIndex:currentRowIndex+1, columnIndex:columnIndex)
let horizontalNeigborsCheck = checkHorizontalNeigbhors(rowIndex:currentRowIndex, columnIndex:columnIndex)
let activeNeighborsCheck = horizontalNeigborsCheck || bottomNeighborCheck
if(!activeNeighborsCheck) {
if let index = self.selectedBlocks.firstIndex(of: cellIndex) {
self.selectedBlocks.remove(at: index)
}
currentRowIndex = currentRowIndex + 1
cellIndex = ["rowIndex":currentRowIndex, "columnIndex":columnIndex]
if(!self.selectedBlocks.contains(cellIndex)) {
self.selectedBlocks.append(cellIndex)
}
}
else {
if(!self.selectedBlocks.contains(cellIndex) && hasNeigbhor == false) {
if let index = self.selectedBlocks.firstIndex(of: cellIndex) {
self.selectedBlocks.remove(at: index)
}
hasNeigbhor = true
self.selectedBlocks.append(cellIndex)
}
}
showScore(checkActiveNeighbors:activeNeighborsCheck, currentRowIndex:currentRowIndex)
}
}
}
func checkNeigbhor(rowIndex:Int, columnIndex:Int) -> Bool {
let leftNeighborIndex : [String:Int] = ["rowIndex":rowIndex, "columnIndex":columnIndex]
return self.selectedBlocks.contains(leftNeighborIndex) ? true :false
}
func checkHorizontalNeigbhors(rowIndex:Int, columnIndex:Int) -> Bool {
let leftNeighborCheck = checkNeigbhor(rowIndex:rowIndex, columnIndex:columnIndex-1)
let rightNeighborCheck = checkNeigbhor(rowIndex:rowIndex, columnIndex:columnIndex+1)
return leftNeighborCheck && rightNeighborCheck
}
func showScore(checkActiveNeighbors:Bool, currentRowIndex:Int) {
if( self.selectedBlocks.count == resultLength && (checkActiveNeighbors || currentRowIndex == numberOfRows-1)) {
self.calculateScore()
return
}
}
func calculateScore() {
for blockIndex in 0..<self.selectedBlocks.count {
var score = intialBlockScore
var rowIndexOfBlock : Int = self.selectedBlocks[blockIndex]["rowIndex"] ?? 0
let columnIndexOfBlock : Int = self.selectedBlocks[blockIndex]["columnIndex"] ?? 0
if(rowIndexOfBlock < numberOfRows) {
let rowDifference = (numberOfRows - rowIndexOfBlock - 1)
for n in 0..<rowDifference {
let localIndex = rowIndexOfBlock+n
let bottomNeighborCheck = checkNeigbhor(rowIndex:localIndex+1, columnIndex:columnIndexOfBlock)
if(!bottomNeighborCheck && localIndex+1<numberOfRows){
let cellIndex = ["rowIndex":localIndex+1, "columnIndex":columnIndexOfBlock, "score":emptyNeighborScore]
if(!self.scoreArray.contains(cellIndex)) {
self.scoreArray.append(cellIndex)
}
}
else if (bottomNeighborCheck && rowIndexOfBlock+1<numberOfRows) {
score = score + filledNeighborScore
}
}
let cellIndex = ["rowIndex":rowIndexOfBlock, "columnIndex":columnIndexOfBlock, "score":score]
if(!self.scoreArray.contains(cellIndex)) {
self.scoreArray.append(cellIndex)
}
rowIndexOfBlock = rowIndexOfBlock + 1
}
calculateFinalScore()
}
}
func calculateFinalScore() {
var totalScore = 0
for inde in 0..<self.scoreArray.count {
let blockScore : Int = self.scoreArray[inde]["score"] ?? 0
totalScore = totalScore+blockScore
}
finalScore = " \(totalScore )"
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}