D
D
DemiurgeDev2021-06-11 14:54:04
C++ / C#
DemiurgeDev, 2021-06-11 14:54:04

How to make difficulty levels for game Guess the Number C# Windows Forms?

I made a game in C# Windows Forms, but I don't know how to implement difficulty levels. Let's say easy (0-10), medium (0-50) and hard (0-100). Please help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Kachan, 2021-06-12
@MANAB

Wrap the guessing logic in a separate method. Pass the min and max parameters to the method, which indicate the range (0-100 for example). Next, create a class with two numbers, also min and max, and create 3 such objects with the values ​​\u200b\u200bdescribed by you at the beginning of the program start (Main). Add a code to choose the difficulty (for example, enter the difficulty from 1 to 3) and, in accordance with the choice, pass the data from the corresponding object with the difficulty parameters to the function for guessing the number

A
Alexey Bereznikov, 2021-06-12
@gdt

Look, in order to guess the number, you must first guess it.
You yourself understand how the difficulty levels differ - namely, the range of guessed values.
That is, if you think about it (I never would have thought that you need to think about it) - you need to generate numbers from different ranges, according to the level of complexity.
First you need to decide what difficulty levels you will have - the easiest way is to create a new enum:

enum Level
{
    Easy,
    Normal,
    Hard,
}

Next, you need to represent your range somehow. You can make an array with two elements, you can use a tuple, but we will create a new entity for this matter:
class Range
{
    public Range(int min, int max)
    {
        Min = min;
        Max = max;
    }

    public int Min { get; }
    public int Max { get; }
}

Next, we need to somehow link our difficulty levels and ranges. We will not complicate here, let it be a simple method:
private static Range GetRange(Level level)
{
    switch (level)
    {
        case Level.Easy: return new Range(0, 10);
        case Level.Normal: return new Range(0, 50);
        case Level.Hard: return new Range(0, 100);
    }
}

Now it remains only to generate a random number, for this you need a random number generator, and a method that actually generates numbers based on difficulty levels.
Add a field to your class, remember that Random is non-thread-safe (this is hardly a problem in your case, but you need to know): Add a method that generates numbers:
private static readonly Random = new Random();
private static int GenerateNumber(Level level)
{
    var range = GetRange(level);
    return Random.Next(range.Min, range.Max);
}

And where you will guess the number, call this method with the desired level of complexity.
As you can see, if you think about it - there is nothing complicated here, it's all a matter of technology.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question