Avoid selecting an integer

hi,
i am writing a console c# program where there is three types of monsters like : troll , ork and goblin.

user should select two monster race to begin the fight in this game.

how when user has selected one type of monster , then user cannot select the same race for second monster?

i wrote this code already :

static void Main(string[] args)
        {


MonsterSelection(1,3);
       Console.ReadKey();

        }

static int MonsterSelection(int _min, int _max)
        {
            int number;

            bool wasValid = false;
            do
            {
                Console.WriteLine("Please specify the first Monster:\n");
                Console.WriteLine("1 = Ork\n2 = Troll\n3 = Goblin\n");

                wasValid = int.TryParse(Console.ReadLine(), out number);
                if (wasValid)
                {
                    if (number < _min || number > _max)
                    {
                        wasValid = false;
                    }
                }
            } while (!wasValid);

            return number;
        }

thanks.

You can have book value for each monster. When selecting monster, book is set as selected. Then next time selecting, check if monster was selected.

Or, use int as bit mask, for marking selected monsters Typically useful, if you have less than 32 monsters.

1 Like

i am new to prgramming , could you explain more what means book value for each monster. ?

i have only 3 types of monsters.

i need to select one and then select another and then they attack to eachother inside attack method.

should i set a bool ?

can you give me some snippet code?

Sorry my typo/auto correction. Not book but bool. :slight_smile: that I hope should make more sense now.

When you use bool as bit mask, monster 1 is 1, monster 2 is 2, monster 3 is 4, Monster 4 is 8.
That gives you an unique combination.
So if you select monster 3, it adds 4 to bit mask.
In if condition you can check by using && AND logic, if monster 3 is set by AND (&&) with value 4. Will return true or false.

But I you have only 3 monsters, just use for simplicity variables. bool usedMonster1, bool usedMonster2, bool usedMonster3. Then simply check with if loop.

1 Like

Or you could use another int to store, and check for a previously selected monster race.
I edited your code and added another int called ‘previousNumber’ which should do the trick:

static int previousNumber = -1;

    static void Main(string[] args)
    {


        MonsterSelection(1, 3);
        Console.ReadKey();

    }

    static int MonsterSelection(int _min, int _max)
    {
        int number;

        bool wasValid = false;
        do
        {
            Console.WriteLine("Please specify the first Monster:\n");
            Console.WriteLine("1 = Ork\n2 = Troll\n3 = Goblin\n");

            wasValid = int.TryParse(Console.ReadLine(), out number);
            if (wasValid && number != previousNumber)
            {
                if (number < _min || number > _max)
                {
                    wasValid = false;
                }
                else
                    previousNumber = number;
            }
        } while (!wasValid);

        return number;
    }

Edit: I have to say, your code looks a bit weird to me. Is ‘Console’ some method you wrote?
And I also think that, if number is smaller than _min or bigger than _max, your method will get stuck in an endless do-while loop?

@Team2Studio
Console is an app/game developed in VS, but not a Unity game. This isn’t really a question that should be in the Unity forum as it’s not related to developing within Unity.

If OP meant developing a Windows or Mac game within Unity, then their code doesn’t look right as it looks like code I wrote back when developing a Windows program outside of Unity.

where exactly should i check with if loop ?

inside main method or MonsterSelection() ?

    static void Main(string[] args)
        {
 

            // so we ask which two different races fight together...

            bool usedMonster1;
            bool usedMonster2;
            bool usedMonster3;

            MonsterSelection(1, 3);
            Console.ReadKey();

        }

@ArshidaGames any reason you writing console type, rather Unity?

Whenever you think is suitable.
Makes sense in the method, where you run check, of available monsters.