Create account / Log in

Rounding down calculations

Talk about module design ideas and techniques.

Moderators: uckelman, Tim M

Rounding down calculations

Postby Benkyo » January 22nd, 2021, 6:52 am

I have 10 points to distribute between 0-6 players depending on how many of their pieces have been placed on certain locations in an area. Say 3 players have an equal number of pieces there, they get 3 points each. This calculation is done for about 20 different areas, with 4-8 locations in each area, so there are lot of possible outcomes.

Do I need to write ? : strings for each location, for each possible combination and outcome? Or is there a more concise way to write the calculation and round down?

Like, here's a draft example I wrote to calculate just "r's" score for area 1 with 4 locations, where I first calculated totals 1r, 1b etc. for how many pieces each player has in the area:

1r==0 ? 0 : 1r>0&&1b+1g+1y+1k+1e=0 ? 10 : 1r==(1b+1g+1y+1k+1e) ? 5 : 1r==1&&1b+1g+1y+1k+1e=2 ? 3 : 1r==1&&1b+1g+1y+1k+1e=3 ? 2 : 1r==2&&1b+1g+1y+1k+1e=1 ? 6 : 1r==3&&1b+1g+1y+1k+1e=1 ? 7

This gives me outcomes in rough order of likelihood: 0, 10, 5, 3, 2, 6, 7, and will work, but repeat 6 times for each of 20 locations, and it's a chore to write out.

Any tips?
Benkyo
 
Posts: 326
Joined: March 13th, 2013, 11:02 pm

Re: Rounding down calculations

Postby DrNostromo » January 22nd, 2021, 6:56 pm

Unless I'm not fully understanding what you're trying to do, Vassal automatically rounds down and throws out remainders unless you get sophisticated with it.

For example, if you have a formula that is x = y/3 than, x will return 3 if y is 9, 10 or 11. It won't return 4 until y hits 12. In other words, it returns the whole number only and throws out the remainder.

Does that help?
User avatar
DrNostromo
 
Posts: 1080
Joined: December 21st, 2007, 3:54 am
Location: Wine Country, Cal. USA

Re: Rounding down calculations

Postby Benkyo » January 22nd, 2021, 9:02 pm

It certainly does! So, as long as I calculate each set of 10 as a GP before adding them together, it should work.
Benkyo
 
Posts: 326
Joined: March 13th, 2013, 11:02 pm

Re: Rounding down calculations

Postby Benkyo » January 23rd, 2021, 9:27 pm

OK, now I need to know how to "get sophisticated with it".

I have the following for the point calculation:

Code: Select all
((GetProperty("1r")==0) ? 0 : (10*(GetProperty("1r")/(GetProperty("1r")+GetProperty("1b")+GetProperty("1g")+GetProperty("1y")+GetProperty("1k")+GetProperty("1e")))))


So if 1r is 0 it returns 0.

If 1r is not zero, it is supposed to calculate 10 x the ratio of 1r to all 6 of the colours. In practice, it returns 10 when only 1r is positive, but 0 if any other colour is positive. I guess because the ratio is rounded down to 0 before being multiplied by 10? Seems odd. How can I do this the right way?
Benkyo
 
Posts: 326
Joined: March 13th, 2013, 11:02 pm

Re: Rounding down calculations

Postby jrwatts » January 23rd, 2021, 9:45 pm

Are all of those properties guaranteed to have a numeric value? If any of them don't, the whole thing will end up being treated as a string instead of a number....

Also, I'm not clear on why you're even using GetProperty, rather than referencing the properties directly....
Last edited by jrwatts on January 23rd, 2021, 9:47 pm, edited 1 time in total.
jrwatts
 
Posts: 222
Joined: April 29th, 2020, 10:30 pm

Re: Rounding down calculations

Postby Benkyo » January 23rd, 2021, 9:45 pm

Yes, they can only be numeric values.
Benkyo
 
Posts: 326
Joined: March 13th, 2013, 11:02 pm

Re: Rounding down calculations

Postby Benkyo » January 23rd, 2021, 9:50 pm

jrwatts wrote:Also, I'm not clear on why you're even using GetProperty, rather than referencing the properties directly....

Neither am I. For some reason that's what the expression builder does. Perhaps because the global properties start with a numeral?
Benkyo
 
Posts: 326
Joined: March 13th, 2013, 11:02 pm

Re: Rounding down calculations

Postby jrwatts » January 23rd, 2021, 9:58 pm

It shouldn't make any difference (according to the java order of operations), but have you tried an extra set of parentheses around the "10*1r" bit of the calculation?
jrwatts
 
Posts: 222
Joined: April 29th, 2020, 10:30 pm

Re: Rounding down calculations

Postby jrwatts » January 23rd, 2021, 10:02 pm

Oops. Just looked closer at your formula, and it isn't working because you already have an extra set of parentheses in there that don't belong...you're forcing the division to occur before the multiplication!
jrwatts
 
Posts: 222
Joined: April 29th, 2020, 10:30 pm

Re: Rounding down calculations

Postby Benkyo » January 23rd, 2021, 10:11 pm

Thanks, obvious solution but I needed someone to point it out!
Benkyo
 
Posts: 326
Joined: March 13th, 2013, 11:02 pm


Return to Module Design

Who is online

Users browsing this forum: Google [Bot] and 4 guests