/// <summary> /// Resets variables that keep track of game state. /// </summary> private void initialize() { gameStarted = false; gameFinished = false; playerOneDone = false; playerTwoDone = false; gameWinner = WinnerChoice.None; if (createdWinner) { Destroy(winnerText); } createdWinner = false; boardStart = gameBoard.transform.position.x - (gameBoard.boardSize / 2); boardEnd = gameBoard.boardSize; }
void Update() { //there's probably a way to clean up this logic but if (gameStarted && !gameFinished) { //check if each player has finished if (!playerOneDone && positionOne > boardEnd) { playerOneDone = true; } if (!playerTwoDone && positionTwo > boardEnd) { playerTwoDone = true; } //check to determine the winner and if the game is finished if (playerOneDone || playerTwoDone) { if (playerOneDone && playerTwoDone) { if (gameWinner == WinnerChoice.None) { gameWinner = WinnerChoice.Tie; } gameFinished = true; } else if (gameWinner == WinnerChoice.None) { //at this point, must be the case that only one or the other is done gameWinner = playerOneDone ? WinnerChoice.One : WinnerChoice.Two; } } //if a winner was determined, create the text if (!createdWinner && gameWinner != WinnerChoice.None) { createdWinner = true; winnerText = Instantiate(winnerPrefab, this.transform) as GameObject; Text winTextComponent = winnerText.GetComponentInChildren <Text>(); switch (gameWinner) { case WinnerChoice.One: winTextComponent.text = "Player One Wins!"; break; case WinnerChoice.Two: winTextComponent.text = "Player Two Wins!"; break; case WinnerChoice.Tie: winTextComponent.text = "Tie Game!"; break; } //set up buttons to reset game Button resetButton = winnerText.transform.Find("Reset").GetComponent <Button>(); resetButton.onClick.AddListener(RestartGame); Button titleButton = winnerText.transform.Find("ToTitle").GetComponent <Button>(); titleButton.onClick.AddListener(toTitle); } } }