Make Complex Decisions

Sometimes your program will need to determine what to do based on whether one condition AND another condition are both true.  At other times your code might need to check if one condition OR another condition is true.  In this lesson, you will try building a program that makes decisions like these, based on two different inputs.  These decisions can be made with the boolean comparison block from the OPERATORS > NUMBERS menu.

To use the block for a complex decision, insert two compare values blocks into the boolean comparison block, and choose an option from the drop-down menu: and, or, and not, or not.  The stuffed boolean comparison block can then go into an if...do block. For example, the expression below in plain words would read as "if the statement 'a is equal to b and c is not equal to d' is true, then clear the terminal screen."

It's a powerful technique with lots of practical uses.  Let's use it to build prototype code for a simple game.

 

Rock, Paper, Scissors

Let's build a program will display who the winner is in a two-player game of “Rock, Paper, Scissors”.  The rules of Rock, Paper, Scissors are simple:

  • rock beats scissors (smash!)
  • scissors beats paper (cut!)
  • paper beats rock (wrap!)

Players make their choices by entering one of following letters into the Terminal: r = Rock, p = Paper, s = Scissors

The program will collect input characters using the Terminal receive number block with its dropdown option set to ASCII character, and store them into a different variable for each player. Here is an example for Player 1:

Then, each variable and a character value block can be inserted into a compare values block to identify which letter was entered. Here's an example for Player 1:

The compare values blocks for each player’s input go into a boolean comparison block:

By building a block structure like this for different possible outcomes of the game, you will be able to determine which player won the match.

Creating several of the above blocks might seem like a lot of work.  But, since each block is similar, you can make copies of the completed boolean comparison block and then change the values for other possible combinations. To do this, right click on the far edge of the entire block to view the drop down menu. Be careful to duplicate the complete block and not just a part of it.

 

Try This

Ready to build the game?

  • Create two variables, named Player1 and Player2.
  • Create two Terminal receive number blocks set to accept ASCII code values for storing in Player1 and Player2.
  • Create two compare values blocks, one for Player1 and one for Player2. Set up each one to compare-equals a player variable against an ASCII code character r, p, or s.
  • Insert both compare values blocks into a boolean comparison block.
  • Make two more copies of the stuffed boolean comparison block.

The table of possible outcomes for the game of Rock, Paper, Scissors is shown below.  Your code doesn't have to test for every possible outcome though.

  • First, test for a tie by by seeing if the statement "Player 1 is equal to Player 2" is true with a simple compare values block. The program doesn't even have to know what letters the players chose, only if they chose the same one!
  • Next, modify the three boolean comparison blocks to check for each possible way for player 2 to win. 

If it is not a tie, and Player 2 did not win, then Player 1 must have won. No need to check further than that!

  • Insert your compare values and boolean comparison blocks into an if...do block, using else if conditions to test each.
  • Build Terminal print text blocks for the top of the program, to give instructions to the players: "Player 1 (r, p, or s):"
  • Build Terminal print text blocks to display the winner, or a tie if one occurs.
  • Save your program!!!

Here is an example of the complete program. You may have built yours slightly different. That's okay as long as it works as you expect it to. So test it out!

  • Click Load RAM, and wait for your program to load and for the terminal to open.
  • When the program asks for player 1’s choice, click the terminal and type “p”, “r”, or “s”.  Do not press enter (it is unnecessary when you set the Terminal receive number block to receive a character).
  • Enter a choice for player two.

The Propeller will display the winning results in the terminal.

 

Your Turn

  • Try using the random block to make a single player version of Rock, Paper, Scissors where you play against the Propeller microcontroller. Make sure that your single player game prints out whether the Propeller chooses rock, paper, or scissors. Hint: you will have to figure out a creative way to respond if the Propeller selects ASCII 113, which is q.
  • Once you've done the next lesson (Blocks that Repeat) come back and nest the entire game inside an infinite loop that asks you if you want to play again, and you reply with a y to restart the game. Hint: If you want the game to quit if a different letter is chosen, consider using the break block.