HiveBrain v1.2.0
Get Started
← Back to all entries
snippetswiftMinor

Create random way from one point to another in map

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
randommapcreatepointwayoneanotherfrom

Problem

I've coded a function which uses a map and creates a random way between two points. Startpoint is always 0/0 and the goal is somewhere else everytime on the map.

Now I'd like to know if I could change anything to make that function more efficient or use another function.

But I need to create random ways and not the fastest one like with a A* algorithm.

func createWay(){
    setGoal()

    var found = false
    var targetX:Int!
    var targetY:Int!

    for y in 0..= Int(Double(currentSize)*Double(currentSize)/2.5){
                label.fontSize = referenceNode.size.width/1.5
                label.text = (countLabels.count + 1).description
                found = true

                startLabel = XSKLabelNode(fontNamed: font?.fontName)
                startLabel.setAlignment(XSKLabelNode.AlignmentMode.Center)
                startLabel.text = "1"
                startLabel.alpha = 0
                startLabel.fontSize = referenceNode.size.width/1.5

                map[0][0].addChild(startLabel)
                break
            }
        }
    }
}

Solution

This method is way to big and there's way, way too much nesting going on.

You have

  • a massive if-else structure



  • a big switch statement



  • two big if statements



all inside a while(true) loop... which is so big it's impossible to find the break statements within, so the readability really, really suffers here.

This while loop as well as a for-in loop is also nested within another while loop.

Additionally, you have massively overloaded this with magic numbers.

When you leave comments like

//1 = left, 2 = right, 3 = up, 4 = down


It's a good sign that you need an enum:

enum Direction {
    case Left, Right, Up, Down
}


There's also a red flag here:

var targetX:Int!
var targetY:Int!

for y in 0..<currentSize{
    for x in 0..<currentSize{
        if map[x][y].name == goalItem.name{
            targetX = x
            targetY = y
        }
    }
}


What if that if never returns true? Now you're going to run into an error:


fatal error: unexpectedly found nil while unwrapping an optional

You need to decide what should happen when these values don't get set and be absolutely certain to handle that in some way that makes sense.

Beyond that, at this point, it's hard to really work out where the optimizations are. The code is too deeply nested to easily sort out what's going on. I can understand if you're having a tough time figuring out how to refactor this into more readable code--surely, that's part of why you're here. But in the meantime, your code is no where near self-documenting, and you don't have enough comments to make up for its lack of self documentation.

Code Snippets

//1 = left, 2 = right, 3 = up, 4 = down
enum Direction {
    case Left, Right, Up, Down
}
var targetX:Int!
var targetY:Int!

for y in 0..<currentSize{
    for x in 0..<currentSize{
        if map[x][y].name == goalItem.name{
            targetX = x
            targetY = y
        }
    }
}

Context

StackExchange Code Review Q#92694, answer score: 4

Revisions (0)

No revisions yet.