Jump to content
xisto Community
Feelay

Any Way To Make Else-ifs Shorter

Recommended Posts

Hey! I know I am asking lots of questions theese days, sorry ;)

I was wondering: Is there anyway to make the following code shorter =?

#If user is level 1 if($user['level']=1){ if($enemy['level']=1{ $winner=mt_rand(50,100);} elseif($enemy['level']=2){ $winner=mt_rand(45,100);} elseif($enemy['level']=3){ $winner=mt_rand(40,100);} elseif($enemy['level']=4){ $winner=mt_rand(35,100);} elseif($enemy['level']=5){ $winner=mt_rand(30,100);} elseif($enemy['level']=6){ $winner=mt_rand(25,100);} elseif($enemy['level']=7){ $winner=mt_rand(20,100);} elseif($enemy['level']=8){ $winner=mt_rand(15,100);} elseif($enemy['level']=9){ $winner=mt_rand(10,100);} elseif($enemy['level']=10){ $winner=mt_rand(5,100);}}

Because I am going to make this for every single level =/ atm i only have 10 levels.. but what wil happen when I have 100 levels =?

Thanks for any replie

//Feelay

Share this post


Link to post
Share on other sites

you can use this function...

for($level=1;$level<11;$level++)for($string=50;$string>4;$string=$string-5)if($enemy['level']=$level)$winner=mt_rand($string,100);
i think that should be correct code...u can use that for all 100 levels,just change the first value in the for loops which are starting values of the strings and second values which are ending values....
Edited by Eggie (see edit history)

Share this post


Link to post
Share on other sites

hmm.. I ain't the best with for loops. But ty for the code ;) Can you please explain it a bit, so i know what values i should change, if I ever need to change something =?

Share this post


Link to post
Share on other sites

i'll be back in few minutes to explain itEDIT:i'm backthat code i made for you can be put instead of your BIG code...just remember to put "<?php" and "?>" because it ain't gonna work without it....if you want it for 100 levels just put this :$level<101 instead of $level=11 and it will work but i don't see what $winner=mt_rand($string,100) is for from your code so i don't know what you should edit in that...i can see that it goes from 50 to 5, and it goes down by 5, but as there is no lower numbers (except 0 and minuses) than i don't know whats with that....

Edited by Eggie (see edit history)

Share this post


Link to post
Share on other sites

Little confused what your are attempting? First off you only used one = in your if which will set a variable in some cases. Also you covered the entire code with if($user['level']=1){ twice not exactly sure why but it makes the elseif() pointless because 1 cant also be equal to 7 ect.

Anyway... It looks as if you might be making a linear pattern. If so you could just run a while($count<10){ then just increase $count by one every time. Then you run an if $count==$enemy['level'] if it comes true then you could simply subtract ($count-1)*5 from 50 do you get what I am saying? This however will only work if your change is a linear equation otherwise you could
A: make a function.
B: just simply type it out and require the file.
C: make a small database table with all the levels and run a while (recommended, easiest).

Here is another one of my untested examples:

while($count<11){if($count==$enemy['level']){$count=$count+1;$equation=50-(($count-1)*5);$winner=mt_rand($equation, 100);}}

@Eggie: Why don't you use brackets? As far as I am aware is that they are required?

Maybe this will help out a bit,
Sparkx

Share this post


Link to post
Share on other sites

as far as i know: when you put function "if" and after it only one function stands than you dont need brackets...
like this:

if(1==1)$a=1;
and if you have more than one function after "if" than you need brackets
like this:
if(1==100){$a=12;$s=12;}
so in my code up there i wrote one "for"and in it theres another "for" -so no need for brackets...
in that "for" stands if ,and in it there's a number which goes to variable... so again no need for brackets...
while writing this i saw a mistake...
i wrote
if($enemy['level']=$level)

instead of
if($enemy['level']==$level)
so that should do it... ;)
i'm gonna download "apache" to try it out...i'm gonna edit this post when i do and try it out

$string=50;for($level=1;$level<11;$level++)$string=$string-5;if($enemy['level']==$level)$winner=mt_rand($string,100);

this is my final code...if it doesn't work i dont know what you should do...
Edited by Eggie (see edit history)

Share this post


Link to post
Share on other sites

here you can find something about loops:

 

https://en.wikipedia.org/wiki/For_loop

 

Also, here is a quote:

 

You've heard the term loop before, but you may not really be sure what it is. A loop is a series of commands that will continue to repeat over and over again until a condition is met. For example, let's say you have the names of people in an array, and you wanted print a list of all of the names. You could setup a loop that would print the first persons name, and then move on to the next and print her name, etc (the continuing to repeat itself part.) The condition would be that it stops once all of the names have been used. Our example would best be used in a foreach loop. The other types of loops that are used are while and for loops.

Share this post


Link to post
Share on other sites

ok.. maybe I should explain a little more.. because now, I need the code, and I can't figure out yours :)The thing I wan't to do is, a loop that checks values from the database. Lets say, I have a strenght stat.I want the for loop to do the following.If the strenght stat is set to 1, the player will hit 5 more damage. Else if the strenght stat is set to 2, the player will hit 10 more damage, and so on..I want to use this function in more than one situation. I hope that you got a view of what I want.. When I created this post, I didn't know how to explain stuff :) I was a noob. the Only thing I did was asking the question and then let people firgure out what I wanted to do.I know how loops works, but in this case, I am confused.. My brain isn't good enought to think out something like this alone ;)Thanks //Feelay

Edited by Feelay (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.