我想添加领带功能,之后再次播放按钮会出现。 我是斯威夫特的新人。
我陷入了领带功能并且保持良好状态。
import UIKit
class ViewController: UIViewController {
@IBOutlet var winnerLable: UILabel!
@IBOutlet var playAgainButton: UIButton!
@IBAction func playAgain(_ sender: Any) {
activeGame = true
activePlayer = 1
gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0]
var _: UIButton
for i in 1..<10 {
if let button = view.viewWithTag(i) as? UIButton {
button.setImage(nil, for: [])
}
winnerLable.isHidden = true
playAgainButton.isHidden = true
winnerLable.center = CGPoint(x: winnerLable.center.x - 500, y: winnerLable.center.y)
playAgainButton.center = CGPoint(x: playAgainButton.center.x - 500, y: playAgainButton.center.y)
}
}
// 1 is nought and 2 is cross
var turnCount = 0
var activeGame = true
var activePlayer = 1
var gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0] //0 is empty
var winnerCombination = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]
@IBAction func buttonPressed(_ sender: AnyObject) {
let activePosition = sender.tag - 1
if gameState[activePosition] == 0 && activeGame {
gameState[activePosition] = activePlayer
if activePlayer == 1 {
sender.setImage(UIImage(named: "nought.png"), for: [])
activePlayer = 2
} else {
sender.setImage(UIImage(named: "cross.png"), for: [])
activePlayer = 1
}
for combination in winnerCombination {
if gameState[combination[0]] != 0 && gameState[combination[0]] == gameState[combination[1]] && gameState[combination[1]] == gameState[combination[2]] {
// we have a winner
activeGame = false
winnerLable.isHidden = false
playAgainButton.isHidden = false
if gameState[combination[0]] == 1{
winnerLable.text = "Nought has won"
} else {
winnerLable.text = "crosses has won"
}
}
UIView.animate(withDuration: 1, animations: {
self.winnerLable.center = CGPoint(x: self.winnerLable.center.x + 500, y: self.winnerLable.center.y)
self.playAgainButton.center = CGPoint(x: self.playAgainButton.center.x + 500, y: self.playAgainButton.center.y)
})
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
winnerLable.isHidden = true
playAgainButton.isHidden = true
winnerLable.center = CGPoint(x: winnerLable.center.x - 500, y: winnerLable.center.y)
playAgainButton.center = CGPoint(x: playAgainButton.center.x - 500, y: playAgainButton.center.y)
}
}
答案 0 :(得分:1)
欢迎来到SO。通常我会标记这种&#34;告诉我如何实现我的应用程序&#34;关闭的问题不合适,但你是新的,所以无论如何我都会回答。
你的buttonPressed
IBAction中有逻辑来检测获胜状态。
通过思考,什么是平局?当每个广场都被填满并且没有赢家时会发生平局,对吧?所以添加你的&#34的else子句;我们有一个胜利者&#34; if语句,遍历整个数组,寻找零个单元格。如果没有赢家,并且没有零小区,那就是平局。
看看你是否可以实现这个逻辑。如果没有,请编辑您的问题以在底部显示新代码,告诉我们它的错误,我们会帮助您解决问题。
答案 1 :(得分:1)
您可以在buttonPressed方法的末尾添加tie的测试:
else if !gameState.contains(0) {
winnerLable.text = "it's a tie"
winnerLable.isHidden = false
playAgainButton.isHidden = false
}
答案 2 :(得分:0)
这里是get的代码,如果它是一个平局....只需将此代码放在主控制器中,你就完成了......
activeGame = false
for i in gameState {
if i == 0 {
activeGame = true
break
}
}
if activeGame == false {
winnerLable.text = "its a tie"
winnerLable.isHidden = false
playAgainButton.isHidden = false
}
答案 3 :(得分:0)
所以,这是我完整的工作代码......
导入UIKit
类ViewController:UIViewController {
@IBOutlet var winnerLable: UILabel!
@IBOutlet var playAgainButton: UIButton!
@IBAction func playAgain(_ sender: Any) {
activeGame = true
activePlayer = 1
gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0]
var _: UIButton
for i in 1..<10 {
if let button = view.viewWithTag(i) as? UIButton {
button.setImage(nil, for: [])
}
winnerLable.isHidden = true
playAgainButton.isHidden = true
winnerLable.center = CGPoint(x: winnerLable.center.x - 500, y: winnerLable.center.y)
playAgainButton.center = CGPoint(x: playAgainButton.center.x - 500, y: playAgainButton.center.y)
}
}
// 1 is nought and 2 is cross
var activeGame = true
var activePlayer = 1
var gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0] //0 is empty
var winnerCombination = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]
@IBAction func buttonPressed(_ sender: AnyObject) {
let activePosition = sender.tag - 1
if gameState[activePosition] == 0 && activeGame {
gameState[activePosition] = activePlayer
if activePlayer == 1 {
sender.setImage(UIImage(named: "nought.png"), for: [])
activePlayer = 2
} else {
sender.setImage(UIImage(named: "cross.png"), for: [])
activePlayer = 1
}
for combination in winnerCombination {
if gameState[combination[0]] != 0 && gameState[combination[0]] == gameState[combination[1]] && gameState[combination[1]] == gameState[combination[2]] {
// we have a winner
activeGame = false
winnerLable.isHidden = false
playAgainButton.isHidden = false
if gameState[combination[0]] == 1{
winnerLable.text = "Nought has won"
break
} else {
winnerLable.text = "crosses has won"
break
}
}
activeGame = false
for i in gameState {
if i == 0 {
activeGame = true
break
}
}
if activeGame == false {
winnerLable.text = "its a tie"
winnerLable.isHidden = false
playAgainButton.isHidden = false
}
UIView.animate(withDuration: 1, animations: {
self.winnerLable.center = CGPoint(x: self.winnerLable.center.x + 500, y: self.winnerLable.center.y)
self.playAgainButton.center = CGPoint(x: self.playAgainButton.center.x + 500, y: self.playAgainButton.center.y)
})
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
winnerLable.isHidden = true
playAgainButton.isHidden = true
winnerLable.center = CGPoint(x: winnerLable.center.x - 500, y: winnerLable.center.y)
playAgainButton.center = CGPoint(x: playAgainButton.center.x - 500, y: playAgainButton.center.y)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}