I made a number guessing game where the computer has to guess what number your thinking of from 1 to 10. Part of my assignment is to make the game restart when the computer wins. I tried putting all the code i wanted to repeat each game into a while loop but that just causes the entire game to not work for some reason. I figured it has to be either be the placement of the while statement or i'm using the wrong condition for the loop. The demo i followed for class created the computer wins bool but never used it so i think it has to have something to do with the loop. Any help or adivce on a different approach is greatly aprreicated.
code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
publicclass GuessingGame : MonoBehaviour
{
//Global Public Variables'
public string confirmKey = "y", rejectKey = "n";
publicbool gameStarted = false, guessMade = false, computerWins = false;
//key codes for plater accepting or rejecting a guess
//the lowest and highest possible numbers the computer can guess
//Global Private Variables
//Stores the list of the numbers already guessed
List<int> numbersGuessed = new List<int>();
//check for player starting the game
//check for computer having made a guess
// Start is called before the first frame update
void Start()
{
//initializing the numbers list
//welcome message added to console
Debug.Log("Think of a number between 1 and 10. Press S on your keyboard when ready");
}
// Update is called once per frame
void Update()
{
//Local variables for storing the computer's current guess
int currentGuess;
//check if player has started the game by pressing S
if (Input.GetKeyDown(KeyCode.S))
startGame();
//check if the game has started, but the computer still needs to make a guess
if(gameStarted == true && guessMade == false)
{
currentGuess = Random.Range(1, 11);
Debug.Log("Is your number" + currentGuess + "?");
bool newNumber = true;
for(int i = 0; i < numbersGuessed.Count; i++)
{
if (currentGuess == numbersGuessed[i])
{
newNumber = false;
break;
}
else
{
newNumber = true;
}
}
if(newNumber == true)
{
numbersGuessed.Add(currentGuess);
guessMade = true;
}
}
elseif (gameStarted == true && guessMade == true)
{
if (Input.GetKeyDown(confirmKey))
{
Debug.Log("Yay I got it right!");
computerWins = true;
}
elseif (Input.GetKeyDown(rejectKey))
{
Debug.Log("I'll guess again");
guessMade = false;
}
}
if (computerWins == true)
{
Debug.Log("Lets play again!");
restartGame();
}
//only run this if game has started, and the computer made a guess
}
//function that handles the game starting
void startGame()
{
gameStarted = true;
}
//function that handles the game ending
void restartGame()
{
gameStarted = false;
}
if (Input.GetKeyDown(KeyCode.S))
startGame();
//check if the game has started, but the computer still needs to make a guess
while (gameStarted == true)
if(gameStarted == true && guessMade == false)
{
currentGuess = Random.Range(1, 11);
Debug.Log("Is your number" + currentGuess + "?");
bool newNumber = true;
for(int i = 0; i < numbersGuessed.Count; i++)
{
if (currentGuess == numbersGuessed[i])
{
newNumber = false;
break;
}
else
{
newNumber = true;
}
}
if(newNumber == true)
{
numbersGuessed.Add(currentGuess);
guessMade = true;
}
}
elseif (gameStarted == true && guessMade == true)
{
if (Input.GetKeyDown(confirmKey))
{
Debug.Log("Yay I got it right!");
computerWins = true;
}
elseif (Input.GetKeyDown(rejectKey))
{
Debug.Log("I'll guess again");
guessMade = false;
}
}
if (computerWins == true)
{
Debug.Log("Lets play again!");
restartGame();
}
//only run this if game has started, and the computer made a guess
}
//function that handles the game starting
void startGame()
{
gameStarted = true;
}
//function that handles the game ending
void restartGame()
{
gameStarted = false;
}
}