Dice 21 game help

So i found this JS 21 dice game on github, it just rolls 2 dice, and checks for a double roll.
If the plater gets 3 doubles in a roll, they will recieve a message to go to jail ( i want to change this to either a reward of some sort or a 5 minute time out).
also it needs to work as a per user roll process… i have no experience with JS.

var dice = {
  doubles: 0,
  roll : function() {
    var x = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
    var y = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
    var dicetotal = x + y;
    $('.dice1').attr('id', "dice" + x);
    $('.dice2').attr('id', "dice" + y);
    if (x == y) { //<----checking if there is a double
        this.doubles++;
        //<---increment double count
        if(this.doubles%3==0) $('.out').attr('id', "jail");
        Now reroll the dice, but if you hit 3 doubles in a row, you get 5 minute TIME OUT!.
        this.roll();
    }
};

A guy with little JS suggested this code should use -
var user1 = new dice();
var user2 = new dice();

user1.roll();
user2.roll();

Any help appreciated, ThankYou.
remember i have 0 experience with JS.

Hey @1grouchy_gamer!

Nightbot can’t store data for you, so checking if a specific user gets 3 doubles in a row is complicated enough already. You could use the quote system API for that, but it’s not meant to store data in the first place, and it wouldn’t solve the complexity issue, checking for a total with it is easy, but checking if the last three throws were doubles is another story.

That said it can definitely be done, but first I would like to suggest a work around and if it’s good enough for you, great, if you really want the complex solution, then we’ll try to write it. Also, I don’t like overloading servers of a free API meant for quotes, that’s why I usually include an “autoclean” function on commands where we use it to store data, but I don’t really see how I could implement it this time, which is an issue for me.

So here’s my suggestion for now, just copy/paste this in you chat and test it:

!addcom !dice21 $(eval r=()=>Math.floor(Math.random()*6)+1; d=[]; c=0; for(i=0;i<6;i++){d.push(r())} for(i=0;i<6;i++){if(d[i]==d[i+1]){c++}i++} `${d[0]} ${d[1]} | ${d[2]} ${d[3]} | ${d[4]} ${d[5]} → That's ${c} double${c>1?`s`:``}${c==3?`, you earn a reward!`:`.`}`)

Thankyou for this.
there seems to be an issue with the math, heres the results i got from 3 test runs, which don’t make sense to me…

2 6 | 5 1 | 3 4 → That's 0 double.
6 2 | 4 4 | 1 1 → That's 2 doubles.
6 5 | 2 6 | 4 5 → That's 0 double.
oh my bad, i see the doubles now :P 

I think this could work, instead of storing the results from 1 run at a time, maybe let the player select the number of rolls per try( needs a roll limit too), lets say 3 as happens now, and if 3 doubles, they get the reward,
So how would i also trigger a twitch command like a 5 minute timeout ? …
cant really do much else…if scores cant be saved simply.

If you want a regular die where people can pick the number of rolls per try, this is what you need:

$(eval n=`$(1)`; isNaN(n)?n=1:n; r=()=>Math.floor(Math.random()*6)+1; d=[]; for(i=0;i<n;i++){d.push(r())} `${n}d6: ${d.join(` | `)}`)

I don’t really see the point of making them select the number of rolls on the first command because we don’t store the results so they wouldn’t know if they get 3 doubles in a row, and this would also mean rewriting the output, which would become less clear as I can’t predict how many rolls the user do: basically I’d have to remove the | (vertical bars) that I use as separators for now.

And to answer your second question, Nightbot can’t time out automatically, the only time it can time out is when someone break a spam protection rule, sorry, I too wished it was different.

i think the original code you gave is much closer to what i want.

But you say nightbot cant timeout ?, in my IRC bot, i can type /clear for instance to clear the screen on twitch, my bot can do the same with .clear . why cant nightbot output to chat like my IRC client can ??
note that my IRC bot needs a period before it, not a forward slash… i believe its a twitch IRC thing.
I’ll have to try adding .clear to nightbots output for your dice code…
ie: “.clear” replacing “you earned a reward”

This is because the only slash command you can use with Nightbot is /me, all others are disabled.

Thankyou for this @Emily & @xgerhard I think Emily’s first code piece will do what i need.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.