Need adivce on how to get the a number guess game to restart

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 loops 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. Any help or adivce on a different approach is greatly aprreicated.
Code that works

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GuessingGame : MonoBehaviour
{
    //Global Public Variables'
    public string confirmKey = "y", rejectKey = "n";
    public bool 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;
            }
        }
        else if (gameStarted == true && guessMade == true)
        {
            if (Input.GetKeyDown(confirmKey))
            {
                Debug.Log("Yay I got it right!");
                computerWins = true;
            }
            else if (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;
    }

code that doesn't work when i add it to while loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 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;
            }
        }
        else if (gameStarted == true && guessMade == true)
        {
            if (Input.GetKeyDown(confirmKey))
            {
                Debug.Log("Yay I got it right!");
                computerWins = true;
            }
            else if (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;
    }
}
Last edited on
Please edit for readability.
https://www.cplusplus.com/articles/jEywvCM9/
Is this c#?
Topic archived. No new replies allowed.