0

Edit #2: The random numbers work now however if you put the same one in as before eg. if n1 = 7 and you put in 7 twice it will print 7 is correct twice but only add one to the display. At the same time you can also find the other number thats correct.

Edit: I know you guys are marking this a duplicate but it does not help at all because I don't understand that thread.

I need this to work by tomorrow so any help would work.

I have this game for a class and inside of the game I have this safe cracking puzzle thing and I cant seem to get it to work.

  • The first problem is that when you pass 20 guesses it plays the generateCombo(); WriteLine(); infinitely.

  • The second is the first 2 numbers are always different but numbers 2, 3 and 4 are ALWAYS the same.

I just started coding this year and don't know to much about how to fix an infinite loop.

Thanks in advance!

class Safe
{
    public bool safeLocked { get; set; }
    public int guesses { get; set; }
    public int cn1 { get; set; }
    public int cn2 { get; set; }
    public int cn3 { get; set; } 
    public int cn4 { get; set; }
    public int num1 { get; set; }
    public int num2 { get; set; }
    public int num3 { get; set; }
    public int num4 { get; set; }

}

class Guesses
{
    public string puzzleGuess { get; set; }
}

public static void Main(string[] args)
    {

    static Guesses numberOf = new Guesses();
    static Safe safe = new Safe();

        safe.guesses = 0;
        safe.safeLocked = true;
        safe.cn1 = 0;
        safe.cn2 = 0;
        safe.cn3 = 0;
        safe.cn4 = 0;
}
public static void generateCombo()
    {
        safe.cn1 = 0;
        safe.cn2 = 0;
        safe.cn3 = 0;
        safe.cn4 = 0;

        Random n = new Random();

        //First number
        safe.num1 = n.Next(1, 10);

        //Second number
        safe.num2 = n.Next(1, 10);

        //Third number
        safe.num3 = n.Next(1, 10);

        //Fourth number
        safe.num4 = n.Next(1, 10);

        Console.WriteLine("A new combonation has been generated.");
        Console.ReadLine();
        safeCracking();
    }

    public static void printScreen()
    {
        Console.Clear();
        Console.ForegroundColor = ConsoleColor.Green;
        Console.Write("Guesses: ");
        Console.ResetColor();
        Console.WriteLine("{0}", safe.guesses);
        Console.WriteLine("");

        Console.WriteLine("     =================================    ");
        Console.WriteLine("     |       |       |       |       |    ");
        Console.WriteLine("     |       |       |       |       |    ");
        Console.WriteLine("     |   {0}   |   {1}   |   {2}   |   {3}   |    ", safe.cn1, safe.cn2, safe.cn3, safe.cn4);
        Console.WriteLine("     |       |       |       |       |    ");
        Console.WriteLine("     |       |       |       |       |    ");
        Console.WriteLine("     =================================    ");
        Console.WriteLine("");
        Console.WriteLine("Guess the numbers of the combination one by one.");
        Console.WriteLine("");
        Console.Write(safe.num1);
        Console.Write(safe.num2);
        Console.Write(safe.num3);
        Console.Write(safe.num4);
    }


    public static void safeCracking()
    {
        string numg;
        int numberG;

        for (safe.guesses = 0; safe.guesses < 20;)
        {

            printScreen();

            try
            {
                numg = Console.ReadLine();
                numberG = int.Parse(numg);
            }
            catch
            {
                numg = "";
                numberG = 0;
            }
            numberOf.puzzleGuess = numg;

            //Check number 4
            if (safe.cn3 > 0 && (numg == safe.num4.ToString()) && safe.cn4 == 0)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("");
                Console.WriteLine("{0} Is Correct!", numberOf.puzzleGuess);
                Console.ResetColor();
                safe.safeLocked = false;
                safe.cn4 = numberG;
                safeUnlocked();
                break;
            }

            //Check number 3
            else if (safe.cn2 > 0 && (numg == safe.num3.ToString()) && safe.cn3 == 0)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("");
                Console.WriteLine("{0} Is Correct!", numberOf.puzzleGuess);
                Console.ResetColor();
                safe.cn3 = numberG;
                Console.ReadLine();
            }

            //Check number 2
            else if (safe.cn1 > 0 && (numg == safe.num2.ToString()) && safe.cn2 == 0)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("");
                Console.WriteLine("{0} Is Correct!", numberOf.puzzleGuess);
                Console.ResetColor();
                safe.cn2 = numberG;
                Console.ReadLine();
            }

            //Check number 1
            else if (safe.cn1 <= 0 && (numg == safe.num1.ToString()) && safe.cn1 == 0)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("");
                Console.WriteLine("{0} Is Correct!", numberOf.puzzleGuess);
                Console.ResetColor();
                safe.cn1 = numberG;
                Console.ReadLine();
            }

            else {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("");
                Console.WriteLine("{0} Is Incorrect!", numberOf.puzzleGuess);
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine("Press Enter to continue...");
                Console.ResetColor();
                Console.ReadLine();
                safe.guesses++;
            }

        }

        generateCombo();
        safe.guesses = 0;
    }

    public static void safeUnlocked()
    {
        Console.Clear();
        Console.WriteLine("The latch on the safe pops open and you give the door a pull.");
        Console.ReadLine();
        Console.WriteLine("Inside of the safe you find a medallion and 3 red potions.");
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.ReadLine();
        Console.WriteLine("MEDALLION ADDED TO INVENTORY AND PLAYER DAMAGE 2X...");
        Console.ReadLine();
        Console.WriteLine("3 HEALTH POTIONS ADDED TO INVENTORY...");
        Console.ResetColor();
        Console.ReadLine();
        Console.WriteLine("As soon as you grab the last potion the roof opens and you hear a deep voice mumbling.");
        Console.ReadLine();
        Console.ForegroundColor = ConsoleColor.Magenta;
        switch (playerClass.richtofen)
        {
            case 0:
                Console.WriteLine("THE PURPLE KEEPER HAS AWOKEN...");
                purpleKeeperBattle();
                Console.ResetColor();
                return;
            case 1:
                Console.WriteLine("THE SHADOW MAN HAS AWOKEN...");
                shadowManBattle();
                Console.ResetColor();
                return;
        }

    }
Josh
  • 23
  • 4
  • 2
    Don't create a new random instance each time. – Evan Trimboli Mar 16 '17 at 02:20
  • @EvanTrimboli Im only creating a new one if the guesses pass 20 – Josh Mar 16 '17 at 02:21
  • First create one Random instance (not 4) and use next function on it for generating random number – Logman Mar 16 '17 at 02:27
  • I did that just now and nothing seemed to change, still the same @Logman – Josh Mar 16 '17 at 02:30
  • Not quite a reproducible example: what is `safe`? I assume you have `static Safe safe = new Safe()` somewhere. And what is `numberOf.puzzleGuess` ? I can't debug a non-working sample. – C. Helling Mar 16 '17 at 02:31
  • You may want to review the [Tag Wiki for C#](http://stackoverflow.com/tags/c%23/info) -- the FAQ section has a lot of helpful canonical q/a's, and the third one directly addresses your question. – Kirk Woll Mar 16 '17 at 02:33
  • @C.Helling ill add everything else 1 sec – Josh Mar 16 '17 at 02:34
  • @josh, that's irrelevant. The solution to your problem lies in the question (and answers) to which this has been marked a duplicate. – Kirk Woll Mar 16 '17 at 02:34
  • @KirkWoll agreed but when I don't understand what that page says its no help... – Josh Mar 16 '17 at 02:37
  • Also @KirkWoll Thats not the only problem that im having with this – Josh Mar 16 '17 at 02:39
  • @C.Helling Its all there now – Josh Mar 16 '17 at 02:41
  • 1
    Well, at that point, @C.Helling's concern that there is no minimum self-contained compilable example is an issue. Pasting your code into an IDE, and it won't compile. You declare static fields inside the `Main` method. You make a reference to `safeUnlocked()` that isn't defined. Finally, fixing all that, the `Main` method doesn't do anything, so the problem doesn't manifest. – Kirk Woll Mar 16 '17 at 02:42
  • @Josh I just don't believe that, please assign return value from `Next` to a temporary variable then print this variable and copy it to `safe.numN`(do that for all occurrence of Next) and tell as the result. – Logman Mar 16 '17 at 02:42
  • 1
    @Josh The "seed" for random is taken from Environment.Ticks (kind of like the current time of day). If you create Random several times at once, you're likely to get the same seed. Since Random is a calculated pseudo-random value, multiple copies of Random with the same seed will generate the same numbers. Only define one copy of Random. – ProgrammingLlama Mar 16 '17 at 02:42
  • @Logman My bad I another instance of it further up. It works now – Josh Mar 16 '17 at 02:46
  • 1
    @john yep with the one caveat being to not use the same (perhaps static) instance if using multiple threads. – Kirk Woll Mar 16 '17 at 02:47
  • @john Thats what Logman was just talking to me about and it solved that problem. Thank you – Josh Mar 16 '17 at 02:47
  • Okay but now the other problem the WriteLine in generateCombo(); will write infinitely if you pass 20 guesses. Go look at "Edit #2" – Josh Mar 16 '17 at 02:48
  • If conditions are wrong just add `&& safe.cnN == 0` in every condition (where N is 4,3,2,1). Btw. your winning condition is not breaking the loop. – Logman Mar 16 '17 at 03:25
  • @Log Idk where your talking about sorry... – Josh Mar 16 '17 at 03:39
  • @Josh swap `if (safe.cn3 > 0 && (numg == safe.num4.ToString())) ` to `if (safe.cn3 > 0 && (numg == safe.num4.ToString()) && safe.cn4 == 0 )` and do that for all if conditions – Logman Mar 16 '17 at 04:07
  • @Logman Should i do this to all of them? – Josh Mar 16 '17 at 04:09
  • @Logman I think it worked but one more thing, once i get to the safeUnlocked(); it plays that then it goes back to the safeCracking();. Any idea on how to fix that? – Josh Mar 16 '17 at 04:12
  • do `break;` after `safeUnlocked();` – Logman Mar 16 '17 at 04:16
  • @Logman Yeah that didn't do it. – Josh Mar 16 '17 at 04:27
  • Im about to make an edit just wait im gonna update everything to what I have now – Josh Mar 16 '17 at 04:27
  • @Josh please read about [control flow](https://en.wikipedia.org/wiki/Control_flow). Invoking a function is not just passing control flow to, it is also providing means to get back to exact place where function was invoked. Thing of it as a boxes (invoked functions) inside a box (parent function). If you want to leave outer function you need to leave inner function first. You can return from a function doing `return;` (to make it work, instead of `break;` do `return;`). Right now you actually do infinite loop using recursion technique ([read about it](https://en.wikipedia.org/wiki/Recursion)). – Logman Mar 16 '17 at 05:18

0 Answers0