2017-11-09 9 views
0

私は2人のチック・タック・トゥ・ゲームをしています。ユーザーがNew Gameボタンを押すたびに、MultiPlayerGameクラスが再び初期化されます。問題は、その内部の変数(gameMatrixfirstPlayercellsLeft)が以前と同じままであることです。私はこれらの変数の宣言の中にブレークポイントを設定しましたが、再起動されるようですが、初期化前のような変数の値を出力するたびにゲームが動作しません。クラスの変数は、再初期化後も同じままです

// 
// MultiPlayerGame.swift 
// Tic Tac Toe 
// 
// Created by Andrei Vataselu on 11/7/17. 
// Copyright © 2017 Andrei Vataselu. All rights reserved. 
// 

import Foundation 
import UIKit 



class MultiPlayerGame{ 

    enum playerTurn { 
     case firstPlayer // valoarea 0 in gameMatrix 
     case secondPlayer // valoarea 1 in gameMatrix 
    } 


    var turnToPlay : playerTurn 
    var gameMatrix : [Int] 
    var cellsLeft : Int 

    static var game = MultiPlayerGame() 

    private init(){ 
     self.turnToPlay = .firstPlayer 

     // Prepara tabla pentru joc 

     self.cellsLeft = 9 

     self.gameMatrix = [2,2,2, 
          2,2,2, 
          2,2,2] 


     for i in 0..<mpImages.count { 
      mpImages[i].isHidden = true 
      mpButtons[i].isHidden = false 
     } 

     mpNewGameButton.isHidden = true 

     mpTurnLabel.text = "X turn" 
     mpTurnLabel.isHidden = false 

     mpWinningImg.isHidden = true 
     mpWinningLabel.isHidden = true 

    } 

    static func getMultiplayerGame() -> MultiPlayerGame { 
     return game 
    } 

    static func newGame(){ 
     game = MultiPlayerGame() 
    } 

    func actionCell(senderTag: Int,gameMatrixValue: Int, imageToChange: UIImage){ 
     gameMatrix[senderTag] = gameMatrixValue 
     mpButtons[senderTag].isHidden = true 
     mpImages[senderTag].image = imageToChange 
     mpImages[senderTag].isHidden = false 
     cellsLeft -= 1 
    } 

    func showEnding(imageToShow: UIImage){ 
     mpWinningImg.image = imageToShow 
     mpWinningImg.isHidden = false 
     mpTurnLabel.isHidden = true 
     mpNewGameButton.isHidden = false 

     if turnToPlay == .firstPlayer { 
      mpWinningLabel.text = "X WINS" 
     } else { 
      mpWinningLabel.text = "O WINS" 
     } 
     mpWinningLabel.isHidden = false 

     //disableButtons 
     for i in 0..<mpImages.count { 
      mpButtons[i].isHidden = true 
     } 

    } 

    func endGame(winState: Int) { 
     switch winState { 
     case 1: 
      showEnding(imageToShow: #imageLiteral(resourceName: "winState1")) 
     case 2: 
      showEnding(imageToShow: #imageLiteral(resourceName: "winState2")) 

     case 3: 
      showEnding(imageToShow: #imageLiteral(resourceName: "winState3")) 

     case 4: 
      showEnding(imageToShow: #imageLiteral(resourceName: "winState4")) 

     case 5: 
      showEnding(imageToShow: #imageLiteral(resourceName: "winState5")) 

     case 6: 
      showEnding(imageToShow: #imageLiteral(resourceName: "winState6")) 

     case 7: 
      showEnding(imageToShow: #imageLiteral(resourceName: "winState7")) 

     case 8: 
      showEnding(imageToShow: #imageLiteral(resourceName: "winState8")) 

     case 9: 
      mpWinningLabel.text = "TIE" 
      mpWinningLabel.isHidden = false 
      mpNewGameButton.isHidden = false 


     default: break 
     } 
    } 

    func checkEndGame() { 
     if gameMatrix[0] == gameMatrix[4] && gameMatrix[4] == gameMatrix[8] && gameMatrix[0] != 2 { 
      endGame(winState: 7) 
     } else if gameMatrix[2] == gameMatrix[4] && gameMatrix[4] == gameMatrix[6] && gameMatrix[2] != 2{ 
      endGame(winState: 8) 
     } else if gameMatrix[0] == gameMatrix[3] && gameMatrix[3] == gameMatrix[6] && gameMatrix[0] != 2 { 
      endGame(winState: 1) 
     } else if gameMatrix[1] == gameMatrix[4] && gameMatrix[4] == gameMatrix[7] && gameMatrix[1] != 2{ 
      endGame(winState: 2) 
     } else if gameMatrix[2] == gameMatrix[5] && gameMatrix[5] == gameMatrix[8] && gameMatrix[2] != 2{ 
      endGame(winState: 3) 
     } else if gameMatrix[0] == gameMatrix[1] && gameMatrix[1] == gameMatrix[2] && gameMatrix[0] != 2 { 
      endGame(winState: 4) 
     } else if gameMatrix[3] == gameMatrix[4] && gameMatrix[4] == gameMatrix[5] && gameMatrix[3] != 2 { 
      endGame(winState: 5) 
     } else if gameMatrix[6] == gameMatrix[7] && gameMatrix[7] == gameMatrix[8] && gameMatrix[6] != 2{ 
      endGame(winState: 6) 
     } 
     else if cellsLeft == 0{ 
      endGame(winState: 9) 
     } 
    } 

    func clickedCell(senderTag : Int) { 
     if gameMatrix[senderTag] == 2 && cellsLeft != 0{ 
      if turnToPlay == .firstPlayer{ 
       actionCell(senderTag: senderTag, gameMatrixValue: 0, imageToChange: #imageLiteral(resourceName: "x")) 
       checkEndGame() 
       turnToPlay = .secondPlayer 
       mpTurnLabel.text = "O turn" 
      } else { 
       actionCell(senderTag: senderTag, gameMatrixValue: 1, imageToChange: #imageLiteral(resourceName: "zero")) 
       checkEndGame() 
       turnToPlay = .firstPlayer 
       mpTurnLabel.text = "X turn" 
      } 

    } 
} 
} 

これは私が私のViewController

var game = MultiPlayerGame.getMultiplayerGame() 

@IBAction func buttonPressed(_ sender: UIButton) { 
     game.clickedCell(senderTag: sender.tag) 
    } 

    @IBAction func newGameButtonPressed(_ sender: Any) { 
     MultiPlayerGame.newGame() 
    } 
+0

は、あなたがあなたのゲームとどのようにあなたがnewGameを呼び出すをインスタンス化する方法を示すことができましたか? – jvrmed

+0

私の質問 – AndreiVataselu

+0

編集しました。手動で変数を手動で変更することなく、毎回更新された静的ゲームを取得できるようにする方法を追加しました。 – jvrmed

答えて

2

でそれを使用する方法に問題があなたの変数gameがあなたのクラスの静的変数のコピーとしたときにnewGame静的変数の変更を呼び出すを持っていますが、ということですvar gameはありません。

あなたがnewGameあなたを呼び出した後game = MultiPlayerGame.getMultiplayerGame()

あなたのゲームの変数を再割り当てする必要性私はあなたが運動場でそれを再現することができ、私が何を意味するか説明するために簡単な例をした:でも

class Game { 
    var a: Int 

    static var game = Game() 

    private init() { 
     a = 0 
    } 

    static func newGame() { 
     game = Game() 
    } 
} 

var g = Game.game 
g.a = 2 

Game.newGame() 
print(g.a)     // value is 2, but you are probably expecting 0 

var newGame = Game.game 
print(newGame.a)   // value is 0, as it should 
g = Game.game 
print(g.a)     // value is 0, as it should 

コントローラー内にvar game = MultiPlayerGame.getMultiplayerGame()があることを確認します。コントローラーを最初にインスタンス化するときにのみ呼び出されます。カスタムすると、このように、常にゲームの新しいインスタンスを取得するためにゲッターを変更することができます

var game: MultiPlayerGame { 
    get { 
     return MultiPlayerGame.getMultiplayerGame() 
    } 
} 
+0

あなたは正しいです。また、MultiPlayerGameの 'game'フィールドには、getterおよび/またはcomputedプロパティがなくてもアクセスできます。 – ELO8C8

+0

はい、これが問題でした。あなたの答えをありがとう! – AndreiVataselu

関連する問題