I've been trying to build a simple little random fighting game. The premise is to keep it simple as I try to expand and understand swift.
What I have been trying to do is have warriorAction/mageAction value be a random number each time the action is pressed. I feel that I have approached this wrong as despite declaring the key and values I go ahead and change them in the switch down below.
Is there anyway to create a dictionary so that each time the value is called that it will generate a random number or is there a better and less verbose way I could be doing this?
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var warriorHealthBar: UIProgressView!
@IBOutlet weak var warriorMaxHitPoints: UILabel!
@IBOutlet weak var warriorCurrentHitPoints: UILabel!
@IBOutlet weak var mageHealthBar: UIProgressView!
@IBOutlet weak var mageMaxHitPoints: UILabel!
@IBOutlet weak var mageCurrentHitPoints: UILabel!
@IBOutlet weak var fighterMessageUpdate: UILabel!
let warriorAction: [String : Int] = ["Punch": 10, "Kick": 12, "Special": 25, "Heal": 15]
let warriorMaxHealth = Int.random(in: 99...151)
var mageAction: [String : Int] = ["Punch": 10, "Kick": 12, "Special": 25, "Heal": 15]
let mageMaxHealth = Int.random(in: 89...121)
var actionRandomizer = Int.random(in: 0...15)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
warriorMaxHitPoints.text = String(warriorMaxHealth)
warriorCurrentHitPoints.text = warriorMaxHitPoints.text
mageMaxHitPoints.text = String(mageMaxHealth)
mageCurrentHitPoints.text = mageMaxHitPoints.text
}
@IBAction func warriorActionPressed(_ sender: UIButton) {
var warriorActionDamage = warriorAction[sender.currentTitle!]
switch sender.currentTitle {
case "Punch":
warriorActionDamage = Int.random(in: 8...11)
case "Kick":
warriorActionDamage = Int.random(in: 10...14)
case "Special":
warriorActionDamage = Int.random(in: 5...20)
case "Heal":
warriorActionDamage = Int.random(in: 4...10)
default:
print ("Error")
}
print("You used \(String(describing: sender.currentTitle!)) and did \(String(describing: warriorActionDamage!)) damage to the mage")
fighterMessageUpdate.text = "You used \(String(describing: sender.currentTitle!)) and did \(String(describing: warriorActionDamage!)) damage to the mage"
}
@IBAction func mageActionPressed(_ sender: UIButton) {
var mageActionDamage = mageAction[sender.currentTitle!]
switch sender.currentTitle {
case "Punch":
mageActionDamage = Int.random(in: 8...11)
case "Kick":
mageActionDamage = Int.random(in: 10...14)
case "Special":
mageActionDamage = Int.random(in: 5...20)
case "Heal":
mageActionDamage = Int.random(in: 4...10)
default:
print ("Error")
}
print("You used \(String(describing: sender.currentTitle!)) and did \(String(describing: mageActionDamage!)) damage to the warrior")
fighterMessageUpdate.text = "You used \(String(describing: sender.currentTitle!)) and did \(String(describing: mageActionDamage!)) damage to the warrior"
}
}