Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

rand() problem

$
0
0
I'm making a small but surprisingly complex game for me and my friend, and i'm stuck.

its basically military style Rock-paper-scissors.

my problem is this...
the COM will reach 0 of a unit type, and when it reach's zero for that type, i don't want it to be able to use that type of unit anymore. But it will continue to use and have negative amounts of units. Here is what i try'd for my solution but it didn't work and i know why, but i just cant find away around it.
switch(comUnit)//comUnit is the unit type the com has selected, which is determined by rand() % 3 + 1;
            {
                case 1://1 is aircraft
                    if(COMairCraft <= 0)
                    {
                        while(comUnit == 1)
                        {
                            comUnit = rand() % 3 + 1;
                        }
                    }
                    break;
                case 2://2 is tank
                    if(COMTanks <= 0)
                    {
                        while(comUnit == 2)
                        {
                            comUnit = rand() % 3 + 1;
                        }
                    }
                    break;
                case 3://3 is infantry
                    if(COMinfantry <= 0)
                    {
                        while(comUnit == 3)
                        {
                            comUnit = rand() % 3 + 1;
                        }
                    }
                    break;
            }


When the com run's out of aircraft, I don't want it to be able to select 1 as its choice anymore. same for all the other types of units.

my solution does not work because when the com has another unit type at 0, it will be able to select it ,I don't know how to stop it from selecting both of the unit types at 0 when that happens.

I wan't the computer to randomly select a unit type, I don't want to make scripted choices for it.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>