Thread: Does my code look correct

Results 1 to 7 of 7
  1. #1 Does my code look correct 
    Perplexity owner

    Join Date
    Feb 2011
    Posts
    171
    Thanks given
    32
    Thanks received
    15
    Rep Power
    0
    So I want to make people with item 773 have a lower drop chance than a player without, I have no errors in eclipse but just want someone to tell me if this code would work properly?

    Code:
    	public static void casketDrop(Player player, int hitpoints, Position pos) {
    		int chance = (int) (1 + hitpoints);
    		
    		if (player.getEquipment().contains(773)) {
    			chance = (int) (1 + hitpoints / 3);
    		}
    		if (Misc.getRandom(hitpoints <= 25000 ? 50000000 : 250000000) < chance) {
    		GroundItemManager.spawnGroundItem(player, new GroundItem(new Item(15522), pos, player.getUsername(), false, 150, true, 200));
    		World.sendMessage("@bla@" +
    				player.getUsername() + "@red@ has received a @blu@ Tier 1 upgrade token @red@ as a Global drop!");
    		}
    	}
    Or Could I just use
    Code:
    chance /= 3
    please try to tell my why I would use one rather than the other or a better way to do it o I can actually learn, Thanks
    Reply With Quote  
     

  2. #2  
    Extreme Donator


    Join Date
    Jul 2008
    Age
    31
    Posts
    956
    Thanks given
    186
    Thanks received
    344
    Rep Power
    1060
    To keep consistency and make it true /3 chance you've already done the math above, you should just divide it by 3 instead of redoing the math.

    You've already declared and initialized it above. No need to redo it.
    Chance = chance / 3 would be fine.

    Sent from my SM-G955U using Tapatalk
    Reply With Quote  
     

  3. Thankful user:


  4. #3  
    Registered Member
    Grant_'s Avatar
    Join Date
    Aug 2014
    Posts
    495
    Thanks given
    96
    Thanks received
    109
    Rep Power
    136
    Quote Originally Posted by Perplexity View Post
    So I want to make people with item 773 have a lower drop chance than a player without, I have no errors in eclipse but just want someone to tell me if this code would work properly?

    Code:
    	public static void casketDrop(Player player, int hitpoints, Position pos) {
    		int chance = (int) (1 + hitpoints);
    		
    		if (player.getEquipment().contains(773)) {
    			chance = (int) (1 + hitpoints / 3);
    		}
    		if (Misc.getRandom(hitpoints <= 25000 ? 50000000 : 250000000) < chance) {
    		GroundItemManager.spawnGroundItem(player, new GroundItem(new Item(15522), pos, player.getUsername(), false, 150, true, 200));
    		World.sendMessage("@bla@" +
    				player.getUsername() + "@red@ has received a @blu@ Tier 1 upgrade token @red@ as a Global drop!");
    		}
    	}
    Or Could I just use
    Code:
    chance /= 3
    please try to tell my why I would use one rather than the other or a better way to do it o I can actually learn, Thanks
    To address your question.
    Code:
    chance /= 3;
    May not give you the result you're looking for.
    Lets do an example of 99 hitpoints.

    This is your current implementation. (body of function)
    Code:
    hitpoints = 99;
    chance = (1 + 99); //100;
    if (hasItem(773)) {
    	chance = (1 + 99 / 3); // 34
    }
    This is /= operator implementation
    Code:
    hitpoints = 99;
    chance = (1 + 99); //100;
    if (hasItem(773)) {
    	chance /= 3; //33.33 (rounded to 33 as an int)
    }
    Notice that this is a small change. But none the less is not the same output. Keep in mind as you write mathematical lines that PEMDAS applies. It's always best to run a hypothetical call through the function or have some debug statements to see what values equate to with dumby data to make sure they're giving you the output you want.

    PEMDAS can be found here.
    https://www.mathsisfun.com/operation-order-pemdas.html

    Quote Originally Posted by Patrity View Post
    To keep consistency and make it true /3 chance you've already done the math above, you should just divide it by 3 instead of redoing the math.

    You've already declared and initialized it above. No need to redo it.
    Chance = chance / 3 would be fine.

    Sent from my SM-G955U using Tapatalk
    @Patrity, just as a note.
    This is more condensed.
    Code:
    chance /= 3;
    Attached image
    Reply With Quote  
     

  5. Thankful user:


  6. #4  
    Perplexity owner

    Join Date
    Feb 2011
    Posts
    171
    Thanks given
    32
    Thanks received
    15
    Rep Power
    0
    Quote Originally Posted by Patrity View Post
    To keep consistency and make it true /3 chance you've already done the math above, you should just divide it by 3 instead of redoing the math.

    You've already declared and initialized it above. No need to redo it.
    Chance = chance / 3 would be fine.

    Sent from my SM-G955U using Tapatalk
    Thank you for your help

    Quote Originally Posted by Grant_ View Post
    To address your question.
    Code:
    chance /= 3;
    May not give you the result you're looking for.
    Lets do an example of 99 hitpoints.

    This is your current implementation. (body of function)
    Code:
    hitpoints = 99;
    chance = (1 + 99); //100;
    if (hasItem(773)) {
    	chance = (1 + 99 / 3); // 34
    }
    This is /= operator implementation
    Code:
    hitpoints = 99;
    chance = (1 + 99); //100;
    if (hasItem(773)) {
    	chance /= 3; //33.33 (rounded to 33 as an int)
    }
    Notice that this is a small change. But none the less is not the same output. Keep in mind as you write mathematical lines that PEMDAS applies. It's always best to run a hypothetical call through the function or have some debug statements to see what values equate to with dumby data to make sure they're giving you the output you want.

    PEMDAS can be found here.
    https://www.mathsisfun.com/operation-order-pemdas.html



    @Patrity, just as a note.
    This is more condensed.
    Code:
    chance /= 3;
    Brilliant, just what I needed thanks for your assistance with this
    Reply With Quote  
     

  7. #5  
    Extreme Donator


    Join Date
    Jul 2008
    Age
    31
    Posts
    956
    Thanks given
    186
    Thanks received
    344
    Rep Power
    1060
    Quote Originally Posted by Grant_ View Post
    @Patrity, just as a note.
    This is more condensed.
    Code:
    chance /= 3;
    It's just a (bad) habbit of mine. More readable imo.
    Reply With Quote  
     

  8. #6  
    Extreme Donator


    Join Date
    Sep 2014
    Posts
    139
    Thanks given
    98
    Thanks received
    92
    Rep Power
    1287
    Could do this as well:
    Code:
    int chance = (player.getEquipment().contains(773) ? ((int) 1 + hitpoints / 3) : ((int) 1 + hitpoints);
    Attached image
    Reply With Quote  
     

  9. #7  
    Respected Member


    Join Date
    Jul 2015
    Posts
    781
    Thanks given
    206
    Thanks received
    394
    Rep Power
    524
    There's no reason to cast RHS as an int.

    Code:
    int chance = player.getEquipment().contains(773) ? (1 + hitpoints / 3) : (1 + hitpoints);
    Reply With Quote  
     

  10. Thankful user:

    Extreme DonatorD


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 4
    Last Post: 09-29-2015, 01:39 AM
  2. Replies: 5
    Last Post: 03-11-2013, 05:04 AM
  3. Looked for experienced IRC Programmer to help finalize my code
    By Yous in forum Application Development
    Replies: 4
    Last Post: 01-29-2012, 08:15 AM
  4. Replies: 18
    Last Post: 04-03-2010, 03:34 AM
  5. Does my code is right?
    By Hexagon in forum Help
    Replies: 7
    Last Post: 07-27-2009, 11:24 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •