Elaborate Dice Roller

366 views
Skip to first unread message

denialdeliciousbeer

unread,
Apr 1, 2015, 4:42:46 PM4/1/15
to mitappinv...@googlegroups.com

Hi

I've created a simple dice roller, picture included, which generates a random integer from the value (number of dice) in the textbox.

Now I want to add a new dice roller which is more complex. This dice roller has to generate a number of integers from the value in textbox1. Then it has to select those integers with the highest values, the number of which are determined by the value in textbox2, and add those together to get the result.

I don't expect someone to do my work for me but I don't even know where to start with such a complex process. Any ideas on how I should tackle this problem?
diceroller.png

Abraham Getzler

unread,
Apr 1, 2015, 8:33:54 PM4/1/15
to mitappinv...@googlegroups.com
You need to read up on lists.
A good online book:

Your collection of dice should be kept in a list,
one per item, with each item ranging from 1 to 6.

ABG

denialdeliciousbeer

unread,
Apr 2, 2015, 8:19:25 AM4/2/15
to mitappinv...@googlegroups.com
Okay here's my attempt. Could it be pared down to something simpler?
diegenerator.png

Taifun

unread,
Apr 2, 2015, 9:45:35 AM4/2/15
to mitappinv...@googlegroups.com
yes, you could use the for each loop to add n items in the list, in your case with the following parameters
from = 1
to = textbox6.text
by = 1

Trying to push the limits of App Inventor! Snippets and Tutorials from Pura Vida Apps by Taifun.         


for each from to

Runs the blocks in the do section for each numeric value in the range starting at from and ending at to, incrementing number by the value of by each time. Use the given variable name, number to refer to the current value. You can change the name number to something else if you wish.

Abraham Getzler

unread,
Apr 2, 2015, 10:16:47 AM4/2/15
to mitappinv...@googlegroups.com
Also be merciful to readers of your blocks - 
Rename Textbox6 to something more informative,
like txtDiceCount.

Don't worry, the name change will propagate automatically
from the Designer to all its blocks.

ABG

denialdeliciousbeer

unread,
Apr 2, 2015, 3:49:46 PM4/2/15
to mitappinv...@googlegroups.com
Thanks for the loop, it's a lot simpler and works perfectly. Now onto the difficult part, selecting, from the list, integers by their max value, the number of which selected is determined by one of the numbers of dice rolled. After looking through the board and some of the tutorials I came up with this.

As you can probably tell, it does not work. Probably for an obvious reason that escapes me.
dieselector.png

Taifun

unread,
Apr 2, 2015, 4:25:10 PM4/2/15
to mitappinv...@googlegroups.com
to simplify even a little bit more, set the list to empty list in the beginning like this

 selecting, from the list, integers by their max value, the number of which selected is determined by one of the numbers of dice rolled
unfortunately I did not understand what you are trying to do
but probably an example could help: 
lets assume dieroll1=2 and dieroll2=3, which means, we have a list with 5 die let's say like this:  6,3,5,6,1
now how should the result look like?
Taifun

Abraham Getzler

unread,
Apr 2, 2015, 4:35:55 PM4/2/15
to mitappinv...@googlegroups.com
Names are important.

Without a good scheme for naming your variables,
you (and we) have no hope of understanding
your blocks.

Build up your names from multiple parts, reflecting the roles
and attributes of the thing they name.

These name parts have special uses:
index = position in a list
list tells you it is a list
value tells you it is a value, not an index
die = which die this is
roll = step in the game
component = thing on the screen

ABG


denialdeliciousbeer

unread,
Apr 2, 2015, 7:02:39 PM4/2/15
to mitappinv...@googlegroups.com
Sorry it makes sense to me.  In effect what I am attempting to do is the following:-

dieroll1 is the first set of dice being rolled
dieroll2 is the second set of dice being rolled
The results of both go into list global melresult
A number of integers from global melresult are selected to go into global melselection.
The number of them selected is equal to the value of dieroll1. 
They are selected in order of highest to lowest.
The integers in global melselection are then added together to give a total value for local melresult which goes into the melresult label.

I will rename them for the next set of images to be clearer.

Abraham Getzler

unread,
Apr 3, 2015, 12:17:44 AM4/3/15
to mitappinv...@googlegroups.com
I suggest a Divide and Conquer approach.

Write a few value procedures you will need ...

SumOfList(list) = sum of the values in the given list
(use a local variable with running subtotal, initially 0)

IndexOfMaxOfList(list) = the index of the biggest value in the given list
(use a local variable with running max, initially 0
and another local variable with running index of max, initially 0)

Also you will probably need extra lists with names like
Candidates and Chosen, during the process of choosing
the high dice and eliminating them from future consideration.

There's a Copy List block that might come in useful.

ABG

denialdeliciousbeer

unread,
Apr 3, 2015, 9:05:16 AM4/3/15
to mitappinv...@googlegroups.com
Right here's my second attempt. I came at the problem from a different angle.

The procedure should, for each item in the dicelist (the random values generated by the diceroller) compare it to global maxnumber which starts at 6.
If the item is equal to that value then it adds it to the second list, diceselection, and then removes the item from the first list, dicelist.
Then it sets the global maxnumber value to 5.
If the dicelist has no items left then the next procedure starts,dicesum.
If the dicelist does have items in it then it starts the procedure again with the new global maxnumber of 5.

It loops until all of the items in dicelist have been assigned to diceselection.

The idea is that that diceselection should be ordered from highest to lowest so that the dicesum procedure can then total the first x items in the list.

Again it doesn't work. At best I get an error of --empty string-- at worst it crashes the emulator.
dieselector2.png

Taifun

unread,
Apr 3, 2015, 10:14:29 AM4/3/15
to mitappinv...@googlegroups.com
why don't you just order the list descending?
as already said, an example would help to understand what you are trying to do...

denialdeliciousbeer

unread,
Apr 3, 2015, 10:39:33 AM4/3/15
to mitappinv...@googlegroups.com

The result should look like

6 6 5 3 1

Does the webviewer solution require internet access? It looks like it does and that wouldn't work for me.

Taifun

unread,
Apr 3, 2015, 12:52:23 PM4/3/15
to mitappinv...@googlegroups.com
Does the webviewer solution require internet access?
no, you also can set the URL to a local webpage first, see also 
Taifun

denialdeliciousbeer

unread,
Apr 4, 2015, 6:55:31 AM4/4/15
to mitappinv...@googlegroups.com
Okay so the dicesorter works. It will take the list and sort it into another list in descending order.

So see the attachment for the next step, the sum procedure. 

It should total the first few list values, the number of which is determined by the value of the first set of dice rolled.

e.g. if the list looks like this 6 6 5 3 1. If the first sets value was 1 it will return 6. If the first sets value was 2 it will return 12, 3 would be 17 etc.

It works properly most of the time but if the value of the first set is 1 it occasionally returns a sum that is larger than 6. I cannot see why it would do so as all the global and local variables are cleared or reset after every operation. Any ideas on where I am going wrong?
diesum.png

Taifun

unread,
Apr 4, 2015, 3:20:56 PM4/4/15
to mitappinv...@googlegroups.com
probably there is an issue in your current version if there are two die with the same value?
try the for each number from to loop instead
Reply all
Reply to author
Forward
0 new messages