Damage or Heal based on the buttons you press











up vote
0
down vote

favorite












If you press the damage button, it will damage armor before health. If you press the heal button, it will heal armor before health.



The project is here: https://github.com/austingaee/DamageHealth-Armor



Are there any improvements I can make?



import UIKit

class ViewController: UIViewController {


@IBOutlet weak var armorBarView: UIView!

@IBOutlet weak var healthBarView: UIView!

var damageAmount : Float = 0.0
var healAmount : Float = 0.0

//Maximum Health
var barAmount : Float = 0.0

override func viewDidLoad() {
super.viewDidLoad()

damageAmount = Float(self.armorBarView.frame.size.width) * 0.10
healAmount = Float(self.armorBarView.frame.size.width) * 0.05
barAmount = Float(self.armorBarView.frame.size.width)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func damageBar(_ sender: UIButton) {
//7.63 came from how much damage was left after 10 hits
if self.armorBarView.frame.size.width > 7.63 {
self.armorBarView.frame.size.width -= CGFloat(damageAmount)
} else if self.healthBarView.frame.size.width > 7.63 {
self.healthBarView.frame.size.width -= CGFloat(damageAmount)
}
}

@IBAction func healBar(_ sender: Any) {
if self.armorBarView.frame.size.width < CGFloat(barAmount) {
self.armorBarView.frame.size.width += CGFloat(healAmount)
print(self.armorBarView.frame.size.width)
} else if self.healthBarView.frame.size.width < CGFloat(barAmount) {
self.healthBarView.frame.size.width += CGFloat(healAmount)
}
}

}









share|improve this question
























  • Is it okay to post a link to my github?
    – austingae
    Aug 7 at 23:04










  • Yes it is fine.
    – Badhan Ganesh
    Aug 9 at 10:48















up vote
0
down vote

favorite












If you press the damage button, it will damage armor before health. If you press the heal button, it will heal armor before health.



The project is here: https://github.com/austingaee/DamageHealth-Armor



Are there any improvements I can make?



import UIKit

class ViewController: UIViewController {


@IBOutlet weak var armorBarView: UIView!

@IBOutlet weak var healthBarView: UIView!

var damageAmount : Float = 0.0
var healAmount : Float = 0.0

//Maximum Health
var barAmount : Float = 0.0

override func viewDidLoad() {
super.viewDidLoad()

damageAmount = Float(self.armorBarView.frame.size.width) * 0.10
healAmount = Float(self.armorBarView.frame.size.width) * 0.05
barAmount = Float(self.armorBarView.frame.size.width)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func damageBar(_ sender: UIButton) {
//7.63 came from how much damage was left after 10 hits
if self.armorBarView.frame.size.width > 7.63 {
self.armorBarView.frame.size.width -= CGFloat(damageAmount)
} else if self.healthBarView.frame.size.width > 7.63 {
self.healthBarView.frame.size.width -= CGFloat(damageAmount)
}
}

@IBAction func healBar(_ sender: Any) {
if self.armorBarView.frame.size.width < CGFloat(barAmount) {
self.armorBarView.frame.size.width += CGFloat(healAmount)
print(self.armorBarView.frame.size.width)
} else if self.healthBarView.frame.size.width < CGFloat(barAmount) {
self.healthBarView.frame.size.width += CGFloat(healAmount)
}
}

}









share|improve this question
























  • Is it okay to post a link to my github?
    – austingae
    Aug 7 at 23:04










  • Yes it is fine.
    – Badhan Ganesh
    Aug 9 at 10:48













up vote
0
down vote

favorite









up vote
0
down vote

favorite











If you press the damage button, it will damage armor before health. If you press the heal button, it will heal armor before health.



The project is here: https://github.com/austingaee/DamageHealth-Armor



Are there any improvements I can make?



import UIKit

class ViewController: UIViewController {


@IBOutlet weak var armorBarView: UIView!

@IBOutlet weak var healthBarView: UIView!

var damageAmount : Float = 0.0
var healAmount : Float = 0.0

//Maximum Health
var barAmount : Float = 0.0

override func viewDidLoad() {
super.viewDidLoad()

damageAmount = Float(self.armorBarView.frame.size.width) * 0.10
healAmount = Float(self.armorBarView.frame.size.width) * 0.05
barAmount = Float(self.armorBarView.frame.size.width)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func damageBar(_ sender: UIButton) {
//7.63 came from how much damage was left after 10 hits
if self.armorBarView.frame.size.width > 7.63 {
self.armorBarView.frame.size.width -= CGFloat(damageAmount)
} else if self.healthBarView.frame.size.width > 7.63 {
self.healthBarView.frame.size.width -= CGFloat(damageAmount)
}
}

@IBAction func healBar(_ sender: Any) {
if self.armorBarView.frame.size.width < CGFloat(barAmount) {
self.armorBarView.frame.size.width += CGFloat(healAmount)
print(self.armorBarView.frame.size.width)
} else if self.healthBarView.frame.size.width < CGFloat(barAmount) {
self.healthBarView.frame.size.width += CGFloat(healAmount)
}
}

}









share|improve this question















If you press the damage button, it will damage armor before health. If you press the heal button, it will heal armor before health.



The project is here: https://github.com/austingaee/DamageHealth-Armor



Are there any improvements I can make?



import UIKit

class ViewController: UIViewController {


@IBOutlet weak var armorBarView: UIView!

@IBOutlet weak var healthBarView: UIView!

var damageAmount : Float = 0.0
var healAmount : Float = 0.0

//Maximum Health
var barAmount : Float = 0.0

override func viewDidLoad() {
super.viewDidLoad()

damageAmount = Float(self.armorBarView.frame.size.width) * 0.10
healAmount = Float(self.armorBarView.frame.size.width) * 0.05
barAmount = Float(self.armorBarView.frame.size.width)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func damageBar(_ sender: UIButton) {
//7.63 came from how much damage was left after 10 hits
if self.armorBarView.frame.size.width > 7.63 {
self.armorBarView.frame.size.width -= CGFloat(damageAmount)
} else if self.healthBarView.frame.size.width > 7.63 {
self.healthBarView.frame.size.width -= CGFloat(damageAmount)
}
}

@IBAction func healBar(_ sender: Any) {
if self.armorBarView.frame.size.width < CGFloat(barAmount) {
self.armorBarView.frame.size.width += CGFloat(healAmount)
print(self.armorBarView.frame.size.width)
} else if self.healthBarView.frame.size.width < CGFloat(barAmount) {
self.healthBarView.frame.size.width += CGFloat(healAmount)
}
}

}






swift ios cocoa






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 8 at 0:27









200_success

127k15148412




127k15148412










asked Aug 7 at 23:03









austingae

41613




41613












  • Is it okay to post a link to my github?
    – austingae
    Aug 7 at 23:04










  • Yes it is fine.
    – Badhan Ganesh
    Aug 9 at 10:48


















  • Is it okay to post a link to my github?
    – austingae
    Aug 7 at 23:04










  • Yes it is fine.
    – Badhan Ganesh
    Aug 9 at 10:48
















Is it okay to post a link to my github?
– austingae
Aug 7 at 23:04




Is it okay to post a link to my github?
– austingae
Aug 7 at 23:04












Yes it is fine.
– Badhan Ganesh
Aug 9 at 10:48




Yes it is fine.
– Badhan Ganesh
Aug 9 at 10:48










1 Answer
1






active

oldest

votes

















up vote
0
down vote













not so much but some tiny changes:



DRY



i just would avoid all the self.healthBarView.frame.size.width



and make methods:




  • getWidth


  • setWidth(or updateWidth because you set the diff)


Constants



the magic numbers like 0.1 0.05 10 i would define as constants so it can changed only at one place. 7.63 i would calculate



Naming



rename barAmount to maxAmount



create minAmount



Float/CGFloat



the Float / CGFloat is realy needed? just put them also into getWidth and updateWidth



else if



the else if block is not needed (same condition)



7.63



this is an value depending on your setup - it should also calculated so it not changes when you init it with different width



Resulting Code



import UIKit

class ViewController: UIViewController {


@IBOutlet weak var armorBarView: UIView!

@IBOutlet weak var healthBarView: UIView!

var damageAmount : Float = 0.0
var healAmount : Float = 0.0

// Min/Maximum Health
var minAmount: Float = 0.00
var maxAmount : Float = 0.0

// how much damage was left after this number of hits
let minAmountHits:Float = 10

let demageFactor: Float = 0.10
let healFactor: Float = 0.05

override func viewDidLoad() {
super.viewDidLoad()

initAmount()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

func initAmount(){
damageAmount = getWidth() * demageFactor
healAmount = getWidth() * healFactor
maxAmount = getWidth()
minAmount = maxAmount - minAmountHits * damageAmount
}

@IBAction func damageBar(_ sender: UIButton) {
if getWidth() > minAmount {
updateWidth(-damageAmount)
}
}

@IBAction func healBar(_ sender: Any) {
if getWidth() < maxAmount {
updateWidth(healAmount)
}
}

func getWidth() -> Float {
return Float(self.armorBarView.frame.size.width)
}

func updateWidth(_ amount: Float) {
self.healthBarView.frame.size.width += CGFloat(amount)
print(self.armorBarView.frame.size.width)
}
}





share|improve this answer























    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f201168%2fdamage-or-heal-based-on-the-buttons-you-press%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    not so much but some tiny changes:



    DRY



    i just would avoid all the self.healthBarView.frame.size.width



    and make methods:




    • getWidth


    • setWidth(or updateWidth because you set the diff)


    Constants



    the magic numbers like 0.1 0.05 10 i would define as constants so it can changed only at one place. 7.63 i would calculate



    Naming



    rename barAmount to maxAmount



    create minAmount



    Float/CGFloat



    the Float / CGFloat is realy needed? just put them also into getWidth and updateWidth



    else if



    the else if block is not needed (same condition)



    7.63



    this is an value depending on your setup - it should also calculated so it not changes when you init it with different width



    Resulting Code



    import UIKit

    class ViewController: UIViewController {


    @IBOutlet weak var armorBarView: UIView!

    @IBOutlet weak var healthBarView: UIView!

    var damageAmount : Float = 0.0
    var healAmount : Float = 0.0

    // Min/Maximum Health
    var minAmount: Float = 0.00
    var maxAmount : Float = 0.0

    // how much damage was left after this number of hits
    let minAmountHits:Float = 10

    let demageFactor: Float = 0.10
    let healFactor: Float = 0.05

    override func viewDidLoad() {
    super.viewDidLoad()

    initAmount()
    }

    override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    }

    func initAmount(){
    damageAmount = getWidth() * demageFactor
    healAmount = getWidth() * healFactor
    maxAmount = getWidth()
    minAmount = maxAmount - minAmountHits * damageAmount
    }

    @IBAction func damageBar(_ sender: UIButton) {
    if getWidth() > minAmount {
    updateWidth(-damageAmount)
    }
    }

    @IBAction func healBar(_ sender: Any) {
    if getWidth() < maxAmount {
    updateWidth(healAmount)
    }
    }

    func getWidth() -> Float {
    return Float(self.armorBarView.frame.size.width)
    }

    func updateWidth(_ amount: Float) {
    self.healthBarView.frame.size.width += CGFloat(amount)
    print(self.armorBarView.frame.size.width)
    }
    }





    share|improve this answer



























      up vote
      0
      down vote













      not so much but some tiny changes:



      DRY



      i just would avoid all the self.healthBarView.frame.size.width



      and make methods:




      • getWidth


      • setWidth(or updateWidth because you set the diff)


      Constants



      the magic numbers like 0.1 0.05 10 i would define as constants so it can changed only at one place. 7.63 i would calculate



      Naming



      rename barAmount to maxAmount



      create minAmount



      Float/CGFloat



      the Float / CGFloat is realy needed? just put them also into getWidth and updateWidth



      else if



      the else if block is not needed (same condition)



      7.63



      this is an value depending on your setup - it should also calculated so it not changes when you init it with different width



      Resulting Code



      import UIKit

      class ViewController: UIViewController {


      @IBOutlet weak var armorBarView: UIView!

      @IBOutlet weak var healthBarView: UIView!

      var damageAmount : Float = 0.0
      var healAmount : Float = 0.0

      // Min/Maximum Health
      var minAmount: Float = 0.00
      var maxAmount : Float = 0.0

      // how much damage was left after this number of hits
      let minAmountHits:Float = 10

      let demageFactor: Float = 0.10
      let healFactor: Float = 0.05

      override func viewDidLoad() {
      super.viewDidLoad()

      initAmount()
      }

      override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      }

      func initAmount(){
      damageAmount = getWidth() * demageFactor
      healAmount = getWidth() * healFactor
      maxAmount = getWidth()
      minAmount = maxAmount - minAmountHits * damageAmount
      }

      @IBAction func damageBar(_ sender: UIButton) {
      if getWidth() > minAmount {
      updateWidth(-damageAmount)
      }
      }

      @IBAction func healBar(_ sender: Any) {
      if getWidth() < maxAmount {
      updateWidth(healAmount)
      }
      }

      func getWidth() -> Float {
      return Float(self.armorBarView.frame.size.width)
      }

      func updateWidth(_ amount: Float) {
      self.healthBarView.frame.size.width += CGFloat(amount)
      print(self.armorBarView.frame.size.width)
      }
      }





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        not so much but some tiny changes:



        DRY



        i just would avoid all the self.healthBarView.frame.size.width



        and make methods:




        • getWidth


        • setWidth(or updateWidth because you set the diff)


        Constants



        the magic numbers like 0.1 0.05 10 i would define as constants so it can changed only at one place. 7.63 i would calculate



        Naming



        rename barAmount to maxAmount



        create minAmount



        Float/CGFloat



        the Float / CGFloat is realy needed? just put them also into getWidth and updateWidth



        else if



        the else if block is not needed (same condition)



        7.63



        this is an value depending on your setup - it should also calculated so it not changes when you init it with different width



        Resulting Code



        import UIKit

        class ViewController: UIViewController {


        @IBOutlet weak var armorBarView: UIView!

        @IBOutlet weak var healthBarView: UIView!

        var damageAmount : Float = 0.0
        var healAmount : Float = 0.0

        // Min/Maximum Health
        var minAmount: Float = 0.00
        var maxAmount : Float = 0.0

        // how much damage was left after this number of hits
        let minAmountHits:Float = 10

        let demageFactor: Float = 0.10
        let healFactor: Float = 0.05

        override func viewDidLoad() {
        super.viewDidLoad()

        initAmount()
        }

        override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        }

        func initAmount(){
        damageAmount = getWidth() * demageFactor
        healAmount = getWidth() * healFactor
        maxAmount = getWidth()
        minAmount = maxAmount - minAmountHits * damageAmount
        }

        @IBAction func damageBar(_ sender: UIButton) {
        if getWidth() > minAmount {
        updateWidth(-damageAmount)
        }
        }

        @IBAction func healBar(_ sender: Any) {
        if getWidth() < maxAmount {
        updateWidth(healAmount)
        }
        }

        func getWidth() -> Float {
        return Float(self.armorBarView.frame.size.width)
        }

        func updateWidth(_ amount: Float) {
        self.healthBarView.frame.size.width += CGFloat(amount)
        print(self.armorBarView.frame.size.width)
        }
        }





        share|improve this answer














        not so much but some tiny changes:



        DRY



        i just would avoid all the self.healthBarView.frame.size.width



        and make methods:




        • getWidth


        • setWidth(or updateWidth because you set the diff)


        Constants



        the magic numbers like 0.1 0.05 10 i would define as constants so it can changed only at one place. 7.63 i would calculate



        Naming



        rename barAmount to maxAmount



        create minAmount



        Float/CGFloat



        the Float / CGFloat is realy needed? just put them also into getWidth and updateWidth



        else if



        the else if block is not needed (same condition)



        7.63



        this is an value depending on your setup - it should also calculated so it not changes when you init it with different width



        Resulting Code



        import UIKit

        class ViewController: UIViewController {


        @IBOutlet weak var armorBarView: UIView!

        @IBOutlet weak var healthBarView: UIView!

        var damageAmount : Float = 0.0
        var healAmount : Float = 0.0

        // Min/Maximum Health
        var minAmount: Float = 0.00
        var maxAmount : Float = 0.0

        // how much damage was left after this number of hits
        let minAmountHits:Float = 10

        let demageFactor: Float = 0.10
        let healFactor: Float = 0.05

        override func viewDidLoad() {
        super.viewDidLoad()

        initAmount()
        }

        override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        }

        func initAmount(){
        damageAmount = getWidth() * demageFactor
        healAmount = getWidth() * healFactor
        maxAmount = getWidth()
        minAmount = maxAmount - minAmountHits * damageAmount
        }

        @IBAction func damageBar(_ sender: UIButton) {
        if getWidth() > minAmount {
        updateWidth(-damageAmount)
        }
        }

        @IBAction func healBar(_ sender: Any) {
        if getWidth() < maxAmount {
        updateWidth(healAmount)
        }
        }

        func getWidth() -> Float {
        return Float(self.armorBarView.frame.size.width)
        }

        func updateWidth(_ amount: Float) {
        self.healthBarView.frame.size.width += CGFloat(amount)
        print(self.armorBarView.frame.size.width)
        }
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 22 at 20:18

























        answered Aug 22 at 20:04









        muescha

        1765




        1765






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f201168%2fdamage-or-heal-based-on-the-buttons-you-press%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Сан-Квентин

            8-я гвардейская общевойсковая армия

            Алькесар