JesseClarkND / codeabbey_in_C-NET

Code Abbey problems soved in C# and .NET
1 stars 2 forks source link

Explanation for Dungeons and Dragons Dice game #1

Open ghost opened 7 years ago

ghost commented 7 years ago

Could you please provide an explanation of the algorithm in your code in Dungeons and Dragons problem? [Currently #51 in codeabbey.com]

Mister-G-Lu commented 4 years ago

The explanation is simple:

// keep trying to guess how many sides there are do_again = true; if (min > 0 && min < 6) { // (the maximum divided by minimum is initial guess, for example if I have a dice with 6 sides then 6 is max and 1 is min) double sides = (max / min); if (sides <= 12) { if (sides % 2 == 0) { // our guess is correct since it is 2,4,6,8,10,12... (due to probability) answer = answer + min.ToString() + 'd' + (max / min).ToString() + ' '; do_again = false; } else { // our maximum is likely too low (we might not have rolled the maximum) max++; if(max>(min12)) { // The minimum might not be low enough (might not have rolled minimum) min--; } } } else { // guess that we only have maximum equal to (min+1)2 (lower dice by 2, because that is the next potentially plausible amount) max = min * 2; min--; }

                }
                else
                { //we guessed a bad amount of sides and minimum must be lower (min has to be 5 at most)
                    min--;
                }