javascriptgoogle-apps-scriptgoogle-sheetsgoogle-apps-for-education

D&D style random chance outcomes with if/then statements on button click - teacher trying to make activity for classes


I'm a high school teacher trying to make an activity for my students around a personal finance unit. I have to give you a bit of exposition for my question to make sense, as the only training I've had in script writing is what I've learned on my own via google, stack, and screwing around.

I'm making a "game of life" style activity to go along with a budget creation lesson for my economics students. I've made a basic game board on page one of the sheets document by coloring in empty cells. Then I placed a transparent box over the top to attach the scripts to. When students click on the box, it runs the script. An alert box with yes/no buttons pops up and gives them a scenario with a choice. Depending on the choice made, they will either earn or spend income, which is then recorded automatically on the "bank account" sheet.

So far, all my simple scripts look hideous, but serve their purpose - like my cooking. But I'm stumped at the moment.

The main issue is the current script I am writing. Here is the scenario: When the student clicks on the box, an alert with a yes/no button box pops up. The text is - "You are invited to join Fight Club. You can accept or refuse the offer. If you accept and win your fight, you will earn $300. If you lose, you will have to pay the hospital $200 to fix your face. Will you fight?"

Here's what I want to happen: If "yes", then get a random integer between 1,2. If 2, alert pops up "Fists of Fury" and the winnings are added to the bank sheet (I know how to do that part) if 1, alert pops up "Fists of failure" and the loss is debited from the bank sheet ('') if no, another alert that says "Brave sir Robin ran away"

I cannot figure out how to tie the button click to the random integer selection and then get the if/thens to work. When I run what I have, it just behaves as though every choice was selected instead of one.

So here's my problem code. Feel free to laugh at it, it knows what it looks like. I really appreciate any help you can give me!

  function arena(arena) {
  var bankSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Bank account");
  var lr = bankSheet.getLastRow();
  var ui = SpreadsheetApp.getUi(); 
  var result = ui.alert(
    'You are invited to join Fight Club. You can accept or refuse the offer. If you accept and win your fight, you will earn $300. If you lose, you will have to pay the hospital $200 to fix your face. Declining the fight has no effect.',
'will you fight?',
  ui.ButtonSet.YES_NO);

   if (result == ui.Button.YES) {
      getRndInteger(1,2)
        if (result === 1)
      ui.alert('You enter the ring, raise your fists, and wake up in the hospital.');  
       bankSheet.getRange(lr+1,4).setValue( 200 );
       bankSheet.getRange(lr+1,3).setValue( "Fists of Failure" );
       bankSheet.getRange(lr+1,8).setValue( "1" );
       if (result === 2)
         ui.alert('You enter the ring, raise your fists, and conquer.');  
       bankSheet.getRange(lr+1,2).setValue( 300 );
       bankSheet.getRange(lr+1,1).setValue( "Fists of fury" );
       bankSheet.getRange(lr+1,7).setValue( "1" );
   }
     if (result == ui.Button.NO) {
       ui.alert('Brave Sir Robin ran away.');
         bankSheet.getRange(lr+1,2).setValue( 0 );
         bankSheet.getRange(lr+1,1).setValue( "escape" );
         bankSheet.getRange(lr+1,7).setValue( "1" );
     }

 
 }

Solution

  • I think your problem is in your logic check (using a triple equal === instead of a double ==). But also, you can make your checks more clearly by using if else if.

    function arena(arena) {
        var bankSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Bank account");
        var lr = bankSheet.getLastRow();
        var ui = SpreadsheetApp.getUi();
        var userAnswer = ui.alert(
            'You are invited to join Fight Club. You can accept or refuse \
        the offer. If you accept and win your fight, you will earn $300. \
        If you lose, you will have to pay the hospital $200 to fix your \
        face. Declining the fight has no effect.',
            'will you fight?',
            ui.ButtonSet.YES_NO);
    
        if (userAnswer == ui.Button.YES) {
            var fightResult = getRndInteger(1, 2);
            if (fightResult == 1) {
                ui.alert('You enter the ring, raise your fists, and wake \
                          up in the hospital.');
                bankSheet.getRange(lr + 1, 4).setValue(200);
                bankSheet.getRange(lr + 1, 3).setValue("Fists of Failure");
                bankSheet.getRange(lr + 1, 8).setValue("1");
            } else if (fightResult == 2) {
                ui.alert('You enter the ring, raise your fists, and conquer.');
                bankSheet.getRange(lr + 1, 2).setValue(300);
                bankSheet.getRange(lr + 1, 1).setValue("Fists of fury");
                bankSheet.getRange(lr + 1, 7).setValue("1");
            } else {
                // shouldn't get here!
            }
        } else if (userAnswer == ui.Button.NO) {
            ui.alert('Brave Sir Robin ran away.');
            bankSheet.getRange(lr + 1, 2).setValue(0);
            bankSheet.getRange(lr + 1, 1).setValue("escape");
            bankSheet.getRange(lr + 1, 7).setValue("1");
        }
    }