我试图创造一个简单的井字游戏。不需要人工智能。我已经到了我可以点击我的用户界面中的按钮,他们将切换每次。我还创建了一个checkScore()函数。但是,我认为checkScore()函数在某些地方有错误的逻辑,我花了两天的时间试图解决它。如果用户连续得到三个,那么应该在任何赢的地方运行winneMessage()函数。我的变量名很简单。
我的问题是,除非我注释掉checkScore()函数中的所有“else if”语句,否则winnermessage将在第一次单击板时运行。请帮忙!谢谢。
定义变量。
var player1 = "❌"
var player2 = "⭕️"
var currentPlayer = "❌"
let originalValue = ""
let winnerMessageTopHorizontal = "We have a winner across the top!"
let winnerMessageCenterHorizontal = "We have a winner across the center!"
let winnerMessageBottomHorizontal = "We have a winner across the bottom!"
let winnerMessageLeftVertical = "We have a winner on the left side!"
let winnerMessageCenterVertical = "We have a winner down the center!"
let winnerMessageRightVertical = "We have a winner on the right side!"
let winnerMessageDiagonal1 = "We have a winner diagonally!"
let winnerMessageDiagonal2 = "We have a winner diagonally!"
这是我创建的changeplayer()函数。
/* function that changes from player1 to player2 (switching from X to O and O to X)*/
func changePlayer(){
if currentPlayer == player1{
print ("\(player1)")
currentPlayer = player2
}
else if currentPlayer == player2{
print ("\(player2)")
currentPlayer = player1
}
else{
NSLog ("Error. Please reset the game.")
}
}
这是我一直遇到问题的checkScore()函数。如果注释掉函数的所有行(除了第一行),则可以看到第一步是如何工作的。
func checkScore(){
if topLeftOutlet.currentTitle == topCenterOutlet.currentTitle && topLeftOutlet.currentTitle == topRightOutlet.currentTitle && topLeftOutlet.currentTitle != ""{
announcementArea.text = winnerMessageTopHorizontal
}
else if leftCenterOutlet.currentTitle == centerOutlet.currentTitle && leftCenterOutlet.currentTitle == rightCenterOutlet.currentTitle && leftCenterOutlet.currentTitle != ""{
announcementArea.text = winnerMessageCenterHorizontal
}
else if bottomLeftOutlet.currentTitle == bottomCenterOutlet.currentTitle && bottomLeftOutlet.currentTitle == bottomRightOutlet.currentTitle && bottomLeftOutlet.currentTitle != ""{
announcementArea.text = winnerMessageBottomHorizontal
}
else if topLeftOutlet.currentTitle == leftCenterOutlet.currentTitle && topLeftOutlet.currentTitle == bottomLeftOutlet.currentTitle && topLeftOutlet.currentTitle != ""{
announcementArea.text = winnerMessageLeftVertical
}
else if topCenterOutlet.currentTitle == centerOutlet.currentTitle && topCenterOutlet.currentTitle == bottomCenterOutlet.currentTitle && topCenterOutlet.currentTitle != ""{
announcementArea.text = winnerMessageCenterVertical
}
else if topRightOutlet.currentTitle == rightCenterOutlet.currentTitle && topRightOutlet.currentTitle == bottomRightOutlet.currentTitle && topRightOutlet.currentTitle != ""{
announcementArea.text = winnerMessageRightVertical
}
else if topLeftOutlet.currentTitle == centerOutlet.currentTitle && topLeftOutlet.currentTitle == bottomRightOutlet.currentTitle && topLeftOutlet.currentTitle != ""{
announcementArea.text = winnerMessageDiagonal1
}
else if topRightOutlet.currentTitle == centerOutlet.currentTitle && topRightOutlet.currentTitle == bottomLeftOutlet.currentTitle && topLeftOutlet.currentTitle != ""{
announcementArea.text = winnerMessageDiagonal2
}
else{
NSLog ("Keep going")
}
}
@IBOutlet weak var announcementArea: UITextField!
板上按钮的动作和插座。当用户点击按钮时,操作将变为X或O。插座用于下面的重置功能,将值重置为空。
@IBAction func topLeft(sender: UIButton) {
sender.setTitle(currentPlayer, forState: .Normal)
changePlayer()
checkScore()
}
@IBOutlet weak var topLeftOutlet: UIButton!
@IBAction func topCenter(sender: UIButton) {
sender.setTitle(currentPlayer, forState: .Normal)
changePlayer()
checkScore()
}
@IBOutlet weak var topCenterOutlet: UIButton!
@IBAction func topRight(sender: UIButton) {
sender.setTitle(currentPlayer, forState: .Normal)
changePlayer()
checkScore()
}
@IBOutlet weak var topRightOutlet: UIButton!
@IBAction func leftCenter(sender: UIButton) {
sender.setTitle(currentPlayer, forState: .Normal)
changePlayer()
checkScore()
}
@IBOutlet weak var leftCenterOutlet: UIButton!
@IBAction func center(sender: UIButton) {
sender.setTitle(currentPlayer, forState: .Normal)
changePlayer()
checkScore()
}
@IBOutlet weak var centerOutlet: UIButton!
@IBAction func rightCenter(sender: UIButton) {
sender.setTitle(currentPlayer, forState: .Normal)
changePlayer()
checkScore()
}
@IBOutlet weak var rightCenterOutlet: UIButton!
@IBAction func bottomLeft(sender: UIButton) {
sender.setTitle(currentPlayer, forState: .Normal)
changePlayer()
checkScore()
}
@IBOutlet weak var bottomLeftOutlet: UIButton!
@IBAction func bottomCenter(sender: UIButton) {
sender.setTitle(currentPlayer, forState: .Normal)
changePlayer()
checkScore()
}
@IBOutlet weak var bottomCenterOutlet: UIButton!
@IBAction func bottomRight(sender: UIButton) {
sender.setTitle(currentPlayer, forState: .Normal)
changePlayer()
checkScore()
}
@IBOutlet weak var bottomRightOutlet: UIButton!
此按钮当前无效:
@IBAction func startGame(sender: UIButton) {
NSLog ("Player Started Game")
}
resetButton()函数,当播放器单击reset按钮时重置板
@IBAction func resetGame(sender: UIButton) {
NSLog ("Player Reset Game")
topLeftOutlet.setTitle(originalValue, forState: .Normal)
topCenterOutlet.setTitle(originalValue, forState: .Normal)
topRightOutlet.setTitle(originalValue, forState: .Normal)
leftCenterOutlet.setTitle(originalValue, forState: .Normal)
centerOutlet.setTitle(originalValue, forState: .Normal)
rightCenterOutlet.setTitle(originalValue, forState: .Normal)
bottomLeftOutlet.setTitle(originalValue, forState: .Normal)
bottomCenterOutlet.setTitle(originalValue, forState: .Normal)
bottomRightOutlet.setTitle(originalValue, forState: .Normal)
}
}
非常感谢你看了一眼。我不希望它是完美的。我只需要再看一眼,看看我是否漏掉了一些显而易见的东西!
最佳答案
我发现了你的问题。问题是在changescore方法中,你的按钮在开始时包含nil值,但是你将它们与“”字符串进行比较,这就是为什么每次你的条件始终为真并显示赢家消息。你只需要替换你的所有条件!“”与!=像这样没有
if topLeftOutlet.currentTitle == topCenterOutlet.currentTitle && topLeftOutlet.currentTitle == topRightOutlet.currentTitle && topLeftOutlet.currentTitle != nil{
announcementArea.text = winnerMessageTopHorizontal
}
else if leftCenterOutlet.currentTitle == centerOutlet.currentTitle && leftCenterOutlet.currentTitle == rightCenterOutlet.currentTitle && leftCenterOutlet.currentTitle != nil{
announcementArea.text = winnerMessageCenterHorizontal
}
else if bottomLeftOutlet.currentTitle == bottomCenterOutlet.currentTitle && bottomLeftOutlet.currentTitle == bottomRightOutlet.currentTitle && bottomLeftOutlet.currentTitle != nil{
announcementArea.text = winnerMessageBottomHorizontal
}
else if topLeftOutlet.currentTitle == leftCenterOutlet.currentTitle && topLeftOutlet.currentTitle == bottomLeftOutlet.currentTitle && topLeftOutlet.currentTitle != nil{
announcementArea.text = winnerMessageLeftVertical
}
else if topCenterOutlet.currentTitle == centerOutlet.currentTitle && topCenterOutlet.currentTitle == bottomCenterOutlet.currentTitle && topCenterOutlet.currentTitle != nil{
announcementArea.text = winnerMessageCenterVertical
}
else if topRightOutlet.currentTitle == rightCenterOutlet.currentTitle && topRightOutlet.currentTitle == bottomRightOutlet.currentTitle && topRightOutlet.currentTitle != nil{
announcementArea.text = winnerMessageRightVertical
}
else if topLeftOutlet.currentTitle == centerOutlet.currentTitle && topLeftOutlet.currentTitle == bottomRightOutlet.currentTitle && topLeftOutlet.currentTitle != nil{
announcementArea.text = winnerMessageDiagonal1
}
else if topRightOutlet.currentTitle == centerOutlet.currentTitle && topRightOutlet.currentTitle == bottomLeftOutlet.currentTitle && topLeftOutlet.currentTitle != nil{
announcementArea.text = winnerMessageDiagonal2
}
else{
NSLog ("Keep going")
}
也可以在重置方法的按钮标题中设置nil。
我希望它能解决你的问题
关于swift - Swift:可能的逻辑错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39907284/