Swift - 编码Tic Tac Toe中的一个关系函数

时间:2014-12-02 00:16:20

标签: ios xcode swift

我13岁,并试图遵循Brian Advent的Swift教程。他在教我如何做一个简单的tic tac toe游戏。经过几个小时的研究后,我试图在Swift中编写一个tie函数,我发现了很多代码,但没有什么可以帮助我。

这是我向获胜者发出的信息:

if winner != "" { //if winner is something

    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!) -> Void in
        //TODO reset the fields
        self.resetField()
     }))
     self.presentViewController(alert, animated: true, completion: nil)
 }

这是关系中的信息:

else if winner == "" {
    let alert = UIAlertController(title: "Tic Tac Toe", message: "It was a tie", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
        //TODO reset the fields
        self.resetField()
    }))
    self.presentViewController(alert, animated: true, completion: nil)
}

但是当我输入此代码并运行它时,它会显示“这是一个平局”警报,因为在开始时仍然没有赢家。

现在我需要的是一个代码,它告诉我什么时候所有空格都被填充,所以不会调用“It is a tie”块。

如果有任何帮助,我也有这个代码。它检查是否有赢家:

func checkResults(){
    var winner = ""
    if fields[0].player == "Steve" && fields[1].player == "Steve" && fields[2].player == "Steve"{
        winner = "Steve"
    }else if fields[0].player == "Creeper" && fields[1].player == "Creeper" && fields[2].player == "Creeper"{
        winner = "Creeper"
    }else if fields[3].player == "Steve" && fields[4].player == "Steve" && fields[5].player == "Steve"{
        winner = "Steve"
    }else if fields[3].player == "Creeper" && fields[4].player == "Creeper" && fields[5].player == "Creeper"{
        winner = "Creeper"
    }else if fields[6].player == "Steve" && fields[7].player == "Steve" && fields[8].player == "Steve"{
        winner = "Steve"
    }else if fields[6].player == "Creeper" && fields[7].player == "Creeper" && fields[8].player == "Creeper"{
        winner = "Creeper"
    }else if fields[0].player == "Steve" && fields[3].player == "Steve" && fields[6].player == "Steve"{
        winner = "Steve"
    }else if fields[0].player == "Creeper" && fields[3].player == "Creeper" && fields[6].player == "Creeper"{
        winner = "Creeper"
    }else if fields[1].player == "Steve" && fields[4].player == "Steve" && fields[7].player == "Steve"{
        winner = "Steve"
    }else if fields[1].player == "Creeper" && fields[4].player == "Creeper" && fields[7].player == "Creeper"{
        winner = "Creeper"
    }else if fields[2].player == "Steve" && fields[5].player == "Steve" && fields[8].player == "Steve"{
        winner = "Steve"
    }else if fields[2].player == "Creeper" && fields[5].player == "Creeper" && fields[8].player == "Creeper"{
        winner = "Creeper"
    }else if fields[0].player == "Steve" && fields[4].player == "Steve" && fields[8].player == "Steve"{
        winner = "Steve"
    }else if fields[0].player == "Creeper" && fields[4].player == "Creeper" && fields[8].player == "Creeper"{
        winner = "Creeper"
    }else if fields[2].player == "Steve" && fields[4].player == "Steve" && fields[6].player == "Steve"{
        winner = "Steve"
    }else if fields[2].player == "Creeper" && fields[4].player == "Creeper" && fields[6].player == "Creeper"{
        winner = "Creeper"
    }

编辑包括Daniel T的功能:

最终工作allFieldsFull()代码:

  else if allFieldsFull() == true && winner == "" {
        let alert = UIAlertController(title: "Tic Tac Toe", message: "It's a tie", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
            //TODO reset the fields
            self.resetField()
        }))
        self.presentViewController(alert, animated: true, completion: nil)
    }

感谢您的帮助,非常感谢:) 如果您有任何疑问,请在下面发表评论

2 个答案:

答案 0 :(得分:2)

您的代码永远不会检查游戏是否结束。它只检查是否有人赢了。

你需要一个if语句检查所有方格是否已满,如果是,并且胜利者==&#34;&#34;,那么游戏就是平局。< / p>

<强> - 编辑 -

您是否了解过循环并编写自己的函数?解决新问题的方法是创建一个遍历所有字段的函数。

func allFieldsFull() -> Bool {
    for i in 0 ..< 9 {
        if fields[i].player.isEmpty {
            return false
        }
    }
    return true
}

我会留给你弄清楚如何使用以上。祝你好运!

答案 1 :(得分:0)

我用它来检查ConnectFour的Win。 而不是if语句,在StepX和StepY中传入1,-1并相应地调整循环。

   func isLinearMatch(#column: Int, row: Int, stepX: Int, stepY: Int)->Bool{
        var startGamePieceType = gamePieceTypeOnBoard(column: column, row: row)

            for var i = 0; i < 4; ++i{
            var newX = row + i * stepY
            var newY = column + i * stepX

            if(gamePieceTypeOnBoard(column: newY, row: newX) == GamePieceType.Undefined){
                    return false
                }

            if (startGamePieceType != gamePieceTypeOnBoard(column: newY, row: newX)){
                return false
                }

        }

        return true

    }

https://github.com/ericcgu/EGSwiftConnectFour