A Complete Example A Complete Example 6 Let’s modify last week’s dice program to play a simplified version of craps: –if the dice roll is 2, 3 or 12, the user loses (craps) –if the dice roll is 7 or 11, the user wins –any other number and the user continues rolling We’ll use if statements to do the job. As an outside-class exercise, try changing the program to use a switch statement.
Some Useful Array Methods Some Useful Array Methods 8
A for Loop Example A for Loop Example 11
Another for Loop Example Another for Loop Example 12 for loops are often used to cycle through the different values contained within an array:
A Final Example: Rock, Paper, Scissors For player 1, generate a random # between 1 and 3. 1= rock, 2=paper, 3-scissors. 2. For player 2, generate a random # between 1 and 3. 1= rock, 2=paper, 3-scissors. 3. Figure out which player wins by the rules: - paper covers rock (paper wins) - scissors cuts paper (scissors win) - rock breaks scissors (rock wins) - both players have the same item (tie) 4. Display an alert message like: “Player 1 has rock. Player 2 has paper. Player 2 wins.” 5. Keep playing if they tie until one player wins.