Michael M
Michael M

Reputation: 141

Keeping track of points in simple console game

I'm having a hard time wrapping my mind around this. Every time the player makes a wrong guess, it should subtract his/her bet from the initial balance. Since it is in a loop, it always takes the initial balance from the beginning spits out the same balance every time. (obviously) I've tried assigning different variables and just can't seem to figure it out.

I've omitted the medium and hard difficulty methods as they are of no use right now, until I figure out this one.

My Main() only calls the setDifficulty(). Nothing else.

class Control
{
    int selectedNumber = 0;
    Random num = new Random();
    bool playAgain = true;
    int difficulty = 0;
    int bet = 0;
    int initialBalance = 20;
    int runningCredits = 0;
    int credits = 0;

    public Control()
    { }

    //sets game difficulty
    public void SetDifficulty() 
    {


        Console.Clear();
        Console.WriteLine("Please select level of difficulty between 1 - 100");
        difficulty = int.Parse(Console.ReadLine());


        if (difficulty >= 1 && difficulty <= 20)
            LetsPlayEasy();


        else if (difficulty >= 21 && difficulty <= 50)
            LetsPlayMedium();


        else if (difficulty >= 51 && difficulty <= 100)
            LetsPlayHard();


        else
        {
            SetDifficulty();
        }


    }

    //easy level method
    public void LetsPlayEasy()
    {
        //variables
        int userGuess;
        int numGuesses = 0;
        selectedNumber = num.Next(1, 101);

        Console.BackgroundColor = ConsoleColor.DarkYellow;
        Console.Clear();
        Console.ForegroundColor = ConsoleColor.White;

        Console.WriteLine("Difficulty level = EASY");

        Console.WriteLine("\nBeggining credit balance = " + initialBalance);
        Console.WriteLine("\nPlease place a bet. You will lose those credits for every incorrect guess!");
        bet = int.Parse(Console.ReadLine());

        do
        {
            Console.WriteLine("\nGuess a number between 1 and 100.");
            userGuess = Convert.ToInt32(Console.ReadLine());
            numGuesses++;

            UI output = new UI();
            output.CompareNumbers(userGuess, ref selectedNumber, ref playAgain, ref numGuesses);


            runningCredits = (initialBalance - bet);
            Console.WriteLine("\nYou have " + runningCredits + " credits remaining.");

        } while (playAgain == true);
    }

class UI
{
    Random num = new Random();
    int keepGoing;

    public UI()
    { }

    //compare user's guess to selected number
    public void CompareNumbers(int userGuess, ref int selectedNumber, ref bool playAgain, ref int numGuesses)
    {
        Control difficulty = new Control();
        Admin say = new Admin();


        if (userGuess > selectedNumber)
        {
            Console.Beep(600, 300);
            Console.WriteLine("\nToo High! Guess Again!");

        }

        else if (userGuess < selectedNumber)
        {
            Console.Beep(300, 300);
            Console.WriteLine("\nToo Low! Guess Again!");

        }
        else
        {
            Console.Beep(350, 300);
            Console.Beep(380, 200);
            Console.Beep(380, 100);
            Console.Beep(500, 1100);
            Console.WriteLine("\n\nCongrats! It took you " + numGuesses + " guesses to win.");
            numGuesses = 0;
            Console.WriteLine("Press 1 to play again or 2 to quit.");
            keepGoing = int.Parse(Console.ReadLine());


            while (keepGoing != 1 && keepGoing != 2)
            {
                Console.WriteLine("\n\nPlease type either 1 or 2 only!");
                keepGoing = int.Parse(Console.ReadLine());
            }

            if (keepGoing == 2)
            {
                playAgain = false;
                say.Goodbye();
            }

            else
            {
                Console.Clear();
                difficulty.SetDifficulty();
            }

        }

    }
}

}

Upvotes: 0

Views: 101

Answers (2)

crashmstr
crashmstr

Reputation: 28573

runningCredits = (initialBalance - bet);

You don't change initialBalance or bet in the loop, so every iteration has the same value of runningCredits.

Outside the loop, do this:

runningCredits = initialBalance;

Inside the loop, do this:

runningCredits -= bet;

Note: you don't have any code to check in the loop to see if the user guessed right or wrong (and as such, the user always loses and you always subtract out the bet).

Upvotes: 0

Fleury26
Fleury26

Reputation: 585

Initialise runningBalance to initialBalance and only use this value for calculations. If you only need initialBalance once, you can also do it by simply switching runningBalance to initialBalance without initializing runningBalance.

 Console.WriteLine("\nBeggining credit balance = " + initialBalance);
 Console.WriteLine("\nPlease place a bet. You will lose those credits for every incorrect guess!");
 bet = int.Parse(Console.ReadLine());
 runningBalance = initialBalance
 do
 {
     Console.WriteLine("\nGuess a number between 1 and 100.");
     userGuess = Convert.ToInt32(Console.ReadLine());
     numGuesses++;

     UI output = new UI();
     output.CompareNumbers(userGuess, ref selectedNumber, ref playAgain, ref numGuesses);


     runningCredits -= bet;
     Console.WriteLine("\nYou have " + runningCredits + " credits remaining.");

 } while (playAgain == true);

Upvotes: 2

Related Questions