Resolved Recursive Method is behaving weird

blaudroid

New member
Joined
Jan 22, 2022
Messages
2
Location
Germany
Programming Experience
10+
Hi there! I am a PHP Developer and we had in our company as an exercise a small little game to write in order to implement AI into the game. The game itself is a guess the number game where you basically been given a range (1 to 100), the computer generates a random number and then you will have to guess which number has been given to you by entering a number. This whole thing works this way:

Computer asks: Guess the number between 1 to 100: (waits for your input). After you type your input, the computer evaluates your answer and then tells you if the number was lower or higher.
Now, there were two options that have been spoken about during this training and one was to use a while loop and one was to use recursion for the function that asks you what number it is etc.

I have already done the exercise in PHP but i wanted to re-create this in C# (i am a C# beginner) and make a small console application that my friend then can run on his windows machine afterwards without going through the hassle of setting up PHP with Composer etc to run my original "game".

Now, the problem I am encountering with the code that I wrote is that, it works fine by itself but it is printing the end message "Congrats, you have finished the game and took you X number of tries to do so." exactly as many as the number of tries that have been used. I don't know if C# is behaving differently in recursion as PHP does but it seems to me that my application is trying to print the message even if before the end message it has a condition to check if the number was valid or not and if not it will repeat the method.

I hope you can understand what I have wrote.

This is the code:

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GuessNumberGame.Players;

namespace GuessNumberGame.Engine
{
    public class GameEngine
    {
        private Player? Player;
        private int number;
        private bool gameState = false;
        private int triesCount = 0;

        public void Run()
        {
            Console.WriteLine("Guess number between 1 and 100: ");
            string? playerInputRaw = Console.ReadLine();

            if(playerInputRaw == null || playerInputRaw.Length == 0)
            {
                this.Run();
            }

            int playerInput = Convert.ToInt32(playerInputRaw);

            bool numberWasGuessed = this.checkIfNumberIsGuessed(playerInput, this.number);

            if(!numberWasGuessed)
            {
                //here it should restart the method, but when the number is correct, it shows
                //the last 2 Console.WriteLine as many times as the tries count is...
                this.Run();
            }

            //these two lines should be printed once but they are printed as i said before, as many times as the value of this.triesCount is.
            Console.WriteLine("Congrats! You guessed the number! The game will exit now!");
            Console.WriteLine("It took you " + this.triesCount + " tries to guess the number!");
        }

        public void SetPlayer(Player Player)
        {
            this.Player = Player;
        }

        public void SetExpectedNumber(int number)
        {
            this.number = number;
        }

        public bool checkIfNumberIsGuessed(int givenNumber, int expectedNumber)
        {
            if(givenNumber < expectedNumber)
            {
                Console.WriteLine("Your number was lower !");
                this.triesCount++;
                return false;
            }

            if (givenNumber > expectedNumber)
            {
                Console.WriteLine("Your number was greater !");
                this.triesCount++;
                return false;
            }

            return true;
        }
    }
}

I have attached a photo of the console output to better understand the problem.
1642853108761.png
 
Last edited:
Solution
That is happening because as each level of recursion pops back up, execution of the code continues on. You recursively call into line 35. As each level comes back up, it will continue on to execute lines 39-40. If your PHP code logic is structured exactly the same way as your C# code, you would have the same effect in PHP.
That is happening because as each level of recursion pops back up, execution of the code continues on. You recursively call into line 35. As each level comes back up, it will continue on to execute lines 39-40. If your PHP code logic is structured exactly the same way as your C# code, you would have the same effect in PHP.
 
Solution
That is happening because as each level of recursion pops back up, execution of the code continues on. You recursively call into line 35. As each level comes back up, it will continue on to execute lines 39-40. If your PHP code logic is structured exactly the same way as your C# code, you would have the same effect in PHP.
You are right ! Damn why didn't i see that in the first place ! My PHP Code was not structured the same way, it's more complicated, this C# code was just proof of concept before coding it actually clean.
This is the solution for the code I wrote in the question:

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GuessNumberGame.Players;

namespace GuessNumberGame.Engine
{
    public class GameEngine
    {
        private Player? Player;
        private int number;
        private bool gameState = false;
        private int triesCount = 0;

        public void Run()
        {
            this.playRound();           

            Console.WriteLine("Congrats! You guessed the number! The game will exit now!");
            Console.WriteLine("It took you " + this.triesCount + " tries to guess the number!");
        }

        public void SetPlayer(Player Player)
        {
            this.Player = Player;
        }

        public void SetExpectedNumber(int number)
        {
            this.number = number;
        }

        public bool checkIfNumberIsGuessed(int givenNumber, int expectedNumber)
        {

            if(givenNumber < expectedNumber)
            {
                Console.WriteLine("Your number was lower !");
                this.triesCount++;
                return false;
            }

            if (givenNumber > expectedNumber)
            {
                Console.WriteLine("Your number was greater !");
                this.triesCount++;
                return false;
            }

            return true;
        }

        public void playRound()
        {
            Console.WriteLine("Guess number between 1 and 100: ");
            string? playerInputRaw = Console.ReadLine();

            if (playerInputRaw == null || playerInputRaw.Length == 0)
            {
                this.playRound();
            }

            int playerInput = Convert.ToInt32(playerInputRaw);

            bool numberWasGuessed = this.checkIfNumberIsGuessed(playerInput, this.number);

            if (!numberWasGuessed)
            {
                this.playRound();
            }
        }
    }
}
 

Latest posts

Back
Top Bottom