4 Replies Latest reply on Apr 27, 2011 10:32 AM by George_Johnson

    Point-Buy Calculator (math help)

    boyse456787654

      I've tried this a few different ways, and can get around what I need, but not the whole ball of wax.

       

      If you are familiar with table-top games, I'm trying to make a point-buy calculator for player abilties, in the standard fassion.

       

      For specifics, I have 6 dropboxes, each with numbers 8 through 18 representing abilties. It is pre-set with five of the boxes show a 10, and the final starts at 8, each player can spend points to get ability higher (max 18), but at a point cost which I want to show the total in a text box.

       

      Each number has a value, and it costs you a number of points equal to its own value plus the value of every number before it, so:

       

      For the 5 stats that begin at 10 use this chart.
      10: Cost: 0 / total: 0 (Base)
      11: Cost: 1 / total: 1
      12: Cost: 1 / total: 2
      13: Cost: 1 / total: 3
      14: Cost: 2 / total: 5
      15: Cost: 2 / total: 7
      16: Cost: 2 / total: 9
      17: Cost: 3 / total: 12
      18: Cost: 4 / total: 16

       

      For the 1 stat that begins at 8, use this chart.
      8: Cost: 0 / total: 0 (Base)
      9: Cost: 1 / total: 1
      10: Cost: 1 / total: 2
      11: Cost: 1 / total: 3
      12: Cost: 1 / total: 4
      13: Cost: 1 / total: 5
      14: Cost: 2 / total: 7
      15: Cost: 2 / total: 9
      16: Cost: 2 / total: 11
      17: Cost: 3 / total: 14
      18: Cost: 4 / total: 18

       

      And if all that garbled mess wasn't enough to make you want to walk away, here is a working sample who's formula I wasn't able to adjust to Adobe: http://www.betahelix.net/pb4e/pb4e.html

       

      If anyone can help, and I know its a longshot, I'd love to have this work in adobe, nothing extra fancy like capping the total, just need to know how many points they've spent using drop bosex that can only show 8 through 18, thanks a ton!

       

      -S.

        • 1. Re: Point-Buy Calculator (math help)
          George_Johnson MVP & Adobe Community Professional

          You can set up objects to associate each combo box value with the corresponding total. Then loop through the combo boxes, get the value of each, look up the total associated with the value, and add it to a running sum. Then set the value of the field to the sum. For example, if you have combo boxes named stat1 through stat6, the custom calculate script for the total text field could look like:

           

          // Custom Calculate script for text field
          (function () {
          
              var v, sum = 0;
          
              // Set up lookup tables to associate combo box values with a total
              var t1 = {10:0, 11:1, 12:2, 13:3, 14:5, 15:5, 16:9, 17:12, 18:16};
              var t2 = {8:0, 9:1, 10:2, 11:3, 12:4, 13:5, 14:7, 15:9, 16:11, 17:14, 18:18};
              
              // Loop through the combo boxes and add up totals
              for (var i = 1; i < 7; i++) {
                  v = getField("stat" + i).value;
                  sum += (i !== 6) ? t1[v] : t2[v];
               }
          
              // Set this field value to the sum
              event.value = sum;
          
          })();
          

           

          You'd have to use a similar naming convention to you can loop through the fields easily, but you should get the idea.

          • 2. Re: Point-Buy Calculator (math help)
            boyse456787654 Level 1

            Absolutly perfect, and a lot more elegant a calculation that what I was hacking away at, thank you a ton!

             

            -S.

            • 3. Re: Point-Buy Calculator (math help)
              George_Johnson MVP & Adobe Community Professional

              I just noticed there's a typo in the t1 object for item 15. It's value should be 7, not 5.

              • 4. Re: Point-Buy Calculator (math help)
                George_Johnson MVP & Adobe Community Professional

                Also, this can be simplified a bit since it appears as though the value associated with each item in the list is unique, at least in these cases. At first I thought they might not be.

                 

                You can set the export value of each combo box item equal to the total for the item, and then you don't have to set up the lookup table objects, just get the field value directly. The field value of each combo box will equal the export value. You'll still need to loop through the fields and keep a running sum, so the code will be very similar.