在Swift中创建Schrodinger's Cat程序时遇到问题

时间:2019-03-26 23:52:27

标签: swift4.2

我正在尝试创建一些代码,通过一些更改来模拟薛定inger的猫实验。我对Swift和编码一般都不陌生,所以我在基础方面苦苦挣扎。

我尝试了一些事情,但感觉就像是转圈。

   struct Cat: Explosive, Bunker {
    let name: String
    var isAlive: Bool?
    var bombExploded: Bool?
    init(name:String, isAlive: Bool?, bombExploded: Bool?){
     self.name = "Jessica"
     self.isAlive = nil
     self.bombExploded = bombExploded
   }

    func detonate () -> Bool {
        let halfTime = Bool.random()
        if halfTime {
      return halfTime == bombExploded
      }
        else {
      return halfTime == bombExploded
      }
    }

    func open() {
      let explosive = bombExploded
      if bombExploded == nil {
      print("Cat ded")
      }else { 
      print("Live cat live")
      }
    }
}


protocol Explosive {
    func detonate () -> Bool
}

protocol Bunker {
     //: - TODO: when initialising cat's `isAlive` should be set to nil, to represent that cat is in a super-position
    var occupent: Cat { get set }
    var explosive: Explosive { get }
        //: - TODO: By the mere action of opening the bunker door, determines the state of the cat's life.
    func open()
}

在开始实施Bunker协议之前,我一直处于正确的轨道。 我知道我缺少一些代码,可能有很多错误,但是我不确定如何修复所做的事情。

0 个答案:

没有答案