title: Swift tie game in Tic Tac Toe
body: <p>I know there are a few of these Tic Tac Toe questions but the logic depends on the model used and my check for winner model seems to be different. Can I insert an else statement or if loop to check for a tie where I've commented <strong><em>at the bottom</em></strong>? It seems with my model I can simply check if winner != letterX &amp;&amp; letterO do a "Game is a Tie" alert. I have NO IDEA how to do this. Thanks</p>
  
  <pre><code>func fieldTapped(recognizer:UITapGestureRecognizer){
      let tappedField = recognizer.view as TTTImageView //
  
  
      if turn == "deviceA" || turn == "yourTurn"{
          tappedField.setPlayer(currentPlayer)
          let messageDict = ["field":tappedField.tag, "player":currentPlayer, "turnString":"deviceA"]
  
          let messageData = NSJSONSerialization.dataWithJSONObject(messageDict, options: NSJSONWritingOptions.PrettyPrinted, error: nil)
  
          let data = messageData
          // do something with the returned data
          var error: NSError?
          appDelegate.mpcHandler.session.sendData(data, toPeers: appDelegate.mpcHandler.session.connectedPeers, withMode: MCSessionSendDataMode.Unreliable, error:&amp;error)
  
          if error != nil{
              println("error:\(error?.localizedDescription)")
          }
      }
  
      checkResults()
      turn = "deviceB"
  }
  
  func setupField(){
      for index in 0...fields.count - 1{
          let gestureRecognizer = UITapGestureRecognizer(target: self, action: "fieldTapped:")
          gestureRecognizer.numberOfTapsRequired = 1
  
          fields[index].addGestureRecognizer(gestureRecognizer)
  
      }
  }
  
  func resetField(){
      for index in 0...fields.count - 1 {
          fields[index].image = nil
          fields[index].activated = false
          fields[index].player = ""
      }
  
      currentPlayer = "letterX"
  }
  
  @IBAction func newGame(sender: AnyObject) {
      resetField()
  
      let messageDict = ["string":"New Game"]
  
      let messageData = NSJSONSerialization.dataWithJSONObject(messageDict, options: NSJSONWritingOptions.PrettyPrinted, error: nil)
  
      var error:NSError?
  
      appDelegate.mpcHandler.session.sendData(messageData, toPeers: appDelegate.mpcHandler.session.connectedPeers, withMode: MCSessionSendDataMode.Reliable, error: &amp;error)
  
      if error != nil{
          println("error: \(error?.localizedDescription)")
      }
  
  
  }
  
  
  
  
  func checkResults(){
      var winner = ""
  
      if fields[0].player == "letterX" &amp;&amp; fields[1].player == "letterX" &amp;&amp; fields[2].player == "letterX"{
          winner = "letterX"
      }else if fields[0].player == "letterO" &amp;&amp; fields[1].player == "letterO" &amp;&amp; fields[2].player == "letterO"{
          winner = "letterO"
      }else if fields[3].player == "letterX" &amp;&amp; fields[4].player == "letterX" &amp;&amp; fields[5].player == "letterX"{
          winner = "letterX"
      }else if fields[3].player == "letterO" &amp;&amp; fields[4].player == "letterO" &amp;&amp; fields[5].player == "letterO"{
          winner = "letterO"
      }else if fields[6].player == "letterX" &amp;&amp; fields[7].player == "letterX" &amp;&amp; fields[8].player == "letterX"{
          winner = "letterX"
      }else if fields[6].player == "letterO" &amp;&amp; fields[7].player == "letterO" &amp;&amp; fields[8].player == "letterO"{
          winner = "letterO"
      }else if fields[0].player == "letterX" &amp;&amp; fields[3].player == "letterX" &amp;&amp; fields[6].player == "letterX"{
          winner = "letterX"
      }else if fields[0].player == "letterO" &amp;&amp; fields[3].player == "letterO" &amp;&amp; fields[6].player == "letterO"{
          winner = "letterO"
      }else if fields[1].player == "letterX" &amp;&amp; fields[4].player == "letterX" &amp;&amp; fields[7].player == "letterX"{
          winner = "letterX"
      }else if fields[1].player == "letterO" &amp;&amp; fields[4].player == "letterO" &amp;&amp; fields[7].player == "letterO"{
          winner = "letterO"
      }else if fields[2].player == "letterX" &amp;&amp; fields[5].player == "letterX" &amp;&amp; fields[8].player == "letterX"{
          winner = "letterX"
      }else if fields[2].player == "letterO" &amp;&amp; fields[5].player == "letterO" &amp;&amp; fields[8].player == "letterO"{
          winner = "letterO"
      }else if fields[0].player == "letterX" &amp;&amp; fields[4].player == "letterX" &amp;&amp; fields[8].player == "letterX"{
          winner = "letterX"
      }else if fields[0].player == "letterO" &amp;&amp; fields[4].player == "letterO" &amp;&amp; fields[8].player == "letterO"{
          winner = "letterO"
      }else if fields[2].player == "letterX" &amp;&amp; fields[4].player == "letterX" &amp;&amp; fields[6].player == "letterX"{
          winner = "letterX"
      }else if fields[2].player == "letterO" &amp;&amp; fields[4].player == "letterO" &amp;&amp; fields[6].player == "letterO"{
          winner = "letterO"
      }
  
  
  
  
      if winner != ""{
          let alert = UIAlertController(title: "Tic Tac Toe", message: "The winner is \(winner)", preferredStyle: UIAlertControllerStyle.Alert)
          alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -&gt; Void in
              self.resetField()
          }))
  
          self.presentViewController(alert, animated: true, completion: nil)
  
      }
  
  if winner != "letterX" &amp;&amp; "letterO"  {
      let alert = UIAlertController(title: "Tic Tac Toe", message: "Tie Game", preferredStyle: UIAlertControllerStyle.Alert)
      alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -&gt; Void in
      self.resetField()
      }))
  
      self.presentViewController(alert, animated: true, completion: nil)
  
      }
  </code></pre>