我的Tic Tac Toe游戏有问题。在我的游戏中,一旦满足(或绘制)获胜条件,代码应该禁用所有被点击的x或O按钮,显示消息并增加玩家1,2或将得分除以1.
如果我在游戏结束后继续点击X / O网格中的任何按钮,那么一切都正在发挥作用,胜利者的得分继续增加,我无法解决这个问题。
这是我的代码。道歉,我对此很陌生。
var gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0]
var p1Score = 0
var p2Score = 0
var drawScore = 0
var scoreHistory = ""
var gameIsActive = true
@IBAction func action(_ sender: AnyObject)
{
if (gameState[sender.tag-1] == 0 && gameIsActive == true)
{
gameState[sender.tag-1] = activePlayer
if (activePlayer == 1)
{
sender.setImage(UIImage(named: "Cross.png"), for: UIControlState())
activePlayer = 2
}
else
{
sender.setImage(UIImage(named: "Nought.png"), for: UIControlState())
activePlayer = 1
}
}
//Rules to determine a Draw
gameIsActive = false
for i in gameState
{
if i == 0
{
gameIsActive = true
break
}
}
if gameIsActive == false
{
displayResult.text = "It was a Tie"
displayResult.isHidden = false
resetGame.isHidden = false
drawScore = drawScore + 1
drawCount.text = ""+"\(drawScore)"
resultHistory.text! += scoreHistory + "It was a Draw" + "\n"
}
for combination in winningCombinations //Win combination checks
{
if gameState[combination[0]] != 0 && gameState[combination[0]] == gameState[combination[1]] && gameState[combination[1]] == gameState[combination[2]]
{
gameIsActive = false // Game no longer playable
if gameState[combination[1]] == 1 {
//Player 1 has won and score is updated
displayResult.text = "Player 1 has won"
p1Score = p1Score + 1 //score increments on win
player1Score.text = "" + "\(p1Score)" //score added to Score Label
resultHistory.text! += scoreHistory + "" + "\n" //match outcome added to history
}
else
{
//Player 2 has won and score is updated
displayResult.text = "Player 2 has won"
p2Score = p2Score + 1 //score increments on win
player2Score.text = "" + "\(p2Score)" //score added to Score Label
resultHistory.text! += scoreHistory + "" + "\n" //match outcome added to history
}
displayResult.isHidden = false //Winner is shown on screen
}
}
}
答案 0 :(得分:0)
如果用户点击已经是action
或X
或O
的按钮,您希望立即退出gameIsActive == false
功能,因为没有任何内容可以在那些情况下。您可以通过在顶部添加两个guard
语句来修复您的程序:
@IBAction func action(_ sender: AnyObject)
{
guard gamesState[sender.tag-1] == 0 else { return }
guard gameIsActive else { return }
gameState[sender.tag-1] = activePlayer
if activePlayer == 1
{
sender.setImage(UIImage(named: "Cross.png"), for: UIControlState())
activePlayer = 2
}
else
{
sender.setImage(UIImage(named: "Nought.png"), for: UIControlState())
activePlayer = 1
}
另外,你确定平局的逻辑是有缺陷的。如果获胜者在最后一步获胜,您的代码会将其视为抽奖,因为董事会已经填补。您应首先检查胜利条件,然后如果游戏仍然有效,因为没有人赢得检查抽奖条件。
// After checking for winning combinations
if gameIsActive {
//Rules to determine a Draw
gameIsActive = false
for i in gameState
{
if i == 0
{
gameIsActive = true
break
}
}
if !gameIsActive
{
displayResult.text = "It was a Tie"
displayResult.isHidden = false
resetGame.isHidden = false
drawScore = drawScore + 1
drawCount.text = ""+"\(drawScore)"
resultHistory.text! += scoreHistory + "It was a Draw" + "\n"
}
}
虽然您可以通过以下方式检查抽奖:
if gameIsActive && !gameState.contains(0) {
// It's a draw
gameIsActive = false
displayResult.text = "It was a Tie"
...
}