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 are 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.
Let's build a program that will display who the winner is in a two-player game of “Rock, Paper, Scissors.” The rules of Rock, Paper, Scissors are simple:
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 drop-down 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 players enter. 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.
Ready to build the game?
The table of possible outcomes for the game of Rock, Paper, Scissors is below. Your code does not have to test for every possible outcome.
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!
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. Now it's time to test it out!
The Propeller will display the winning results in the Terminal.