Jump to content

Is there a cleaner way of doing this without all the if/elseif statements


Go to solution Solved by mac_gyver,

Recommended Posts

Okay I have been working on script for about a week now. In a game(runescape) there is content known as the citadel. What you do is on weekly basis you gather resources towards upkeep and upgrades(if any). Upkeep is the weekly cost to keep the plots of the citadel up and running or they will degrade. Each tier or level that upkeep cost will change. Upgrade is any thing you are upgrading or building(new plots). You must meet upkeep first before any resources are applied to any upgrades. Every week each player can only cap a certain amount of resources. Okay now that I got that outta of the way now to explain what I am trying to accomplish. There will be a form that passes the level of each plot to the script. So what I am doing is:
 
if($citadel = '1'){
	$wood_upkeep = "1500";
	$stone_upkeep = "0";
	$bar_upkeep = "0";
	$pbar_upkeep = "0";
	$cloth_upkeep = "0";
	$rations_upkeep = "0";
}elseif($citadel = '2'){
	$wood_upkeep = "3600";
	$stone_upkeep = "0";
	$bar_upkeep = "0";
	$pbar_upkeep = "0";
	$cloth_upkeep = "0";
	$rations_upkeep = "0";
}elseif($citadel = '3'){
	$wood_upkeep = "2975";
	$stone_upkeep = "2550";
	$bar_upkeep = "0";
	$pbar_upkeep = "0";
	$cloth_upkeep = "0";
	$rations_upkeep = "0";
}elseif($citadel = '4'){
	$wood_upkeep = "1690";
	$stone_upkeep = "1500";
	$bar_upkeep = "1500";
	$pbar_upkeep = "0";
	$cloth_upkeep = "0";
	$rations_upkeep = "0";
}elseif($citadel = '5'){
	$wood_upkeep = "2250";
	$stone_upkeep = "2300";
	$bar_upkeep = "2200";
	$pbar_upkeep = "100";
	$cloth_upkeep = "0";
	$rations_upkeep = "0";
	$cap_limit = "2000";
}elseif($citadel = '6'){
	$wood_upkeep = "2470";
	$stone_upkeep = "1495";
	$bar_upkeep = "1690";
	$pbar_upkeep = "1365";
	$cloth_upkeep = "2210";
	$rations_upkeep = "0";
	$cap_limit = "2350";
}elseif($citadel = '7'){
	$wood_upkeep = "2800";
	$stone_upkeep = "400";
	$bar_upkeep = "1600";
	$pbar_upkeep = "2000";
	$cloth_upkeep = "3000";
	$rations_upkeep = "3000";
	$cap_limit = "2700";
}

Times that by 10 plots that is alot of code(over 500 lines). I calculated and it would come out to over 2000 lines of code just to spit out a number of "cappers" needed to achieve x upgrade/upkeep. That is to many lines to achieve one thing. What these if/elseif statements are doing is simply just assigning the cost values based on what tier(lvl) x plot is. Then I use the cost values and add em all up and divide by the weekly cap limit and that will give me the number of cappers need to achieve x upgrade + upkeep. The cost values are static and do not change. What I am asking is if there is a simpler way to achieve the same thing with less lines of code. I thought of associative arrays, but I think I will still need all those if statements. Switch statements would generate just as much code if not more. 

Edited by natturefrk
  • Solution

what you doing is mapping input values to a set of output values. rather than use conditional logic to do this, just store the data somewhere, addressed/indexed by the input value. a database would be the best choice, or for a smaller set of data, you can use an array.

 

once you have the data stored, you can perform any calculation when you 'query' for the matching data, either in an sql query, if the data is stored in a database, or using php array functions, if the data is stored in an array.

 

some additional advantages of storing the data in a database, you would be able to create an administrative interface that allows the data to be created/edited by someone who doesn't have any programming knowledge and you can display the various levels to the users simply by querying for the data and displaying it.

  • Like 1

Really?? Huh I always thought using the database for this type of thing was a bad thing which is why I shyed away from using the database in the first place haha. Now I can use the where clause :) .  Thank you for you input!!!!!

Edited by natturefrk
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.