## Rolling Dice

```/*** Rolling Dice ********************************
Betty likes gambling games.  She played a dice game with 3 dice.
She could bet on whether the dice would come up with two (or 3)matching numbers, or whether all the dice would be different.e.g.  3 4 3 --> MATCH    3 4 5 --> no match    4 4 4 --> MATCHBetty wants to know which is more likely: matching dice or
non-matching dice.  This program simulates the dice by using a
Random function and using if.. commands to check for matches.
**************************************************/``` ### Complex Decisions

This program uses 3 random numbers to simulate 3 dice rolls.  It repeats the dice rolls 10 times.
Each time the dice are rolled, the program must decide whether 2 of the dice match (as shown in the results above).

if(a==b || b==c || a==c)
{ println( .... "MATCH"); }

This command checks whether dice A matches dice B OR dice B matches dice C OR dice C matches dice A.
The | | symbols stand for "OR".  The  = =  symbol checks whether two numbers are equal.
If a match is detected, then it prints "MATCH".

else
{  println(..... "no match"); }

"Else" means "otherwise".  So if NO match occurs, it prints "no match".

### Boolean Logic

George Boole was a mathematician who outlined the basic principles of logical decisions.
A Boolean expression is a series of logical operations that give a result of TRUE or FALSE.
The basic operators in Boolean expressions are OR and AND.

For example, when  a = 4 , b = 5 , c = 4, then

(a == b)    ==>  FALSE
(b == c)    ==>  FALSE
(a == c)    ==>  TRUE

OR expressions
(a == b || b == c )  ==>  FALSE, because neither is true
(a == b || b == c || a == c)  ==>  TRUE, because a == c is true

Whenever a Boolean expression uses OR ( | | ) , only one part must be true to make the whole thing true.

The Java symbol for AND is && .  For example, when a = 4, b = 5, c = 4 , then

AND expressions
(a == b && b == c )  ==>  FALSE, because both parts are false
(a == b && a == c )  ==>  FALSE, because a == b is false
(a == 4 && c == 4 )  ==>  TRUE, because both parts are true

The following command checks whether two numbers are smaller than 100 and add up to 150.

( a < 100  &&  b < 100  &&  a+b==150 )

### Practice

• Download the program above, run it and check that it gives sensible results.
• Change the program so that, instead of checking for 2 dice matching, it checks whether
ALL 3 DICE match.  You will need to use && to create an AND expression.
• Change the program so that it rolls 4 dice instead of 3 dice - you need another variable D.
Now make it check for a simple match of 2 dice, e.g.  a = = b  ||  b = = c ....
You need to check lots of pairs to get them all.
• Change the program to check for 3 matching dice.  There are several different ways this can happen, for example:
3   3    4   3   ,     6   2   2   2   ,   5  1  5  5 ...  You need to use both AND (&&) and OR(| |) operations
together in one expression.
• Change the program so that you WIN when all the dice are different.
You need to use the NOT EQUAL operator, written    (a != b), and must check MANY combinations.

### Summary of Symbols

AND  &&         OR  | |        EQUAL  = =       not Equal   ! =

Less than or equal     < =        Greater or equal    > =

You should find a summary of Boolean expressions in a Java textbook
and study all the possible operations.