I'm trying to program a formal system that has these rules:
- You start with the string "MI" (which is the text of the label at the beginning)
There are four rules of inference : 1) If the last character of the string is "I", you can add a "U" to the string (ex: MI --> MIU)
2) Being x a sequence of letters, Mx can be replaced with Mxx (ex: MIU --> MIUIU)
3) If the string contains "III", you can replace "III" with "U" (ex: MUIII --> MUU)
4) If the string contains "UU", you can delete the "UU" part (ex: MIIUU --> MII)
In my project, rule 1 corresponds to button1, rule 2 to button2, rule 3 to button3, rule 4 to button 4
Also, since a user may end up in a infinite loop sometimes (ex: if you get to "MIU", from then on you can only use rule 2 and get "MIUIU" and so on) I added a "clear" button. I want the clear button to just turn the Label.text into "MI" so the user can just go back to the starting point and retry
The point is, for some reason when I press the "clear" button the Label.text does not turn into "MI" as I would want it to, but it turns into "MIU" instead!
What am I missing?
PS: I know this might is probably something extremely easy that I'm stuck on, but I don't have much programming experience
This is my code: import UIKit
class ViewController: UIViewController {
@IBOutlet weak var Label: UILabel!
@IBOutlet weak var rule1Button: UIButton!
@IBOutlet weak var rule2Button: UIButton!
@IBOutlet weak var rule3Button: UIButton!
@IBOutlet weak var rule4Button: UIButton!
@IBOutlet weak var clearButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
clear()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
var string : String = "MI"
var string1 : String = ""
func rule1() {
if string.characters.last == "I" {
string.append("U")
Label.text = string
}
}
func rule2() {
string1 = string.replacingOccurrences(of: "M", with: "")
string = string + string1
Label.text = string
}
func rule3() {
string = self.string.replacingOccurrences(of: "III", with: "U")
Label.text = string
}
func rule4() {
string = self.string.replacingOccurrences(of: "UU", with: "")
Label.text = string
}
func clear() {
string = "MI"
Label.text = string
}
@IBAction func rule1Button(_ sender: Any) {
rule1()
}
@IBAction func rul2Button(_ sender: Any) {
rule2()
}
@IBAction func rule3Button(_ sender: Any) {
rule3()
}
@IBAction func rule4Button(_ sender: Any) {
rule4()
}
@IBAction func clearButton(_ sender: Any) {
clear()
}
}