1
\$\begingroup\$

I am making a cookie clicker type game as a small project, within Xcode, written in Swift. I have a been developing a function which will turn a Int64() into a 4 digit long smaller version, to fit into text boxes.

For example, the function would turn 1,340,000 into £1.34M. The issue is, this could have to happen for a long time; the game could last long enough to the point where I go past conventional values like 100 trillion, and such. This is the function for when you click on one of the buttons to buy a building:

@IBAction func planetClicked(_ sender: Any) {
    let cost = Int64(buildingConstants.planetConstantCost)
    if moneyConstants.money >= cost {
        moneyConstants.money = moneyConstants.money - cost
        buildingLevels.planetNumber = buildingLevels.planetNumber + 1
        let originalValue = 5100000000
        let newValue = round(Double(originalValue) * pow(1.15, Double(buildingLevels.planetNumber)))
        buildingConstants.planetConstantCost = Int64(newValue)
        if String(newValue).characters.count >= 4 {
            if String(newValue).characters.count >= 7 {
                if String(newValue).characters.count >= 10 {
                    if String(newValue).characters.count >= 13 {
                        //Assume this goes on for as long as I need it to
                    } else {
                        planetCostLabel.text = turnIntoBil(newValue: String(newValue))
                    }
                } else {
                    planetCostLabel.text = turnIntoMil(newValue: String(newValue))
                }
            } else {
                planetCostLabel.text = turnIntoK(newValue: String(newValue))
            }
        } else{
            planetCostLabel.text = "£" + String(buildingConstants.planetConstantCost) + ".00"
        }

    }
}

buildingConstants is a structure:

struct buildingConstants {
    static var jitterClickConstantCost = NSInteger()
    static var macrosConstantCost = NSInteger()
    static var autoClickerConstantCost = NSInteger()
    static var anotherPCConstantCost = NSInteger()
    static var workerConstantCost = NSInteger()
    static var houseConstantCost = Int64()
    static var officeConstantCost = Int64()
    static var skyriseConstantCost = Int64()
    static var planetConstantCost = Int64()
}   

buildingLevels is also a structure:

struct buildingLevels {
    static var jitterClickNumber = NSInteger()
    static var macrosNumber = NSInteger()
    static var autoClickerNumber = NSInteger()
    static var anotherPCNumber = NSInteger()
    static var workerNumber = NSInteger()
    static var houseNumber = NSInteger()
    static var officeNumber = NSInteger()
    static var skyriseNumber = NSInteger()
    static var planetNumber = NSInteger()
}

These are my value changing functions:

func turnIntoK(newValue: String) -> String {
    let firstChar = String(newValue)[0]
    let secondChar = String(newValue)[1]
    let thirdChar = String(newValue)[2]
    let fourthChar = String(newValue)[3]
    let string: String = "£" + firstChar + "." + secondChar + thirdChar + fourthChar + "k"
    return string
}

func turnIntoMil(newValue: String) -> String {
    let firstChar = String(newValue)[0]
    let secondChar = String(newValue)[1]
    let thirdChar = String(newValue)[2]
    let fourthChar = String(newValue)[3]
    let string: String = "£" + firstChar + "." + secondChar + thirdChar + fourthChar + "M"
    return string
}

func turnIntoBil(newValue: String) -> String {
    let firstChar = String(newValue)[0]
    let secondChar = String(newValue)[1]
    let thirdChar = String(newValue)[2]
    let fourthChar = String(newValue)[3]
    let string: String = "£" + firstChar + "." + secondChar + thirdChar + fourthChar + "M"
    return string
}

Is there a loop that I could possibly make which would make my code run smoother (instead of a lot of nested ifs)? Also, does my code look like it could be improved so it doesn't take up too much RAM on someone's phone if they were to use it?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.