Guessing+Game

**I. Introduction**


Captain Hammer! Dr Horrible's arch nemesis.

Dr. Horrible needs to modify his evil henchman training simulator. The henchmen need to estimate how many robots should be deployed to deal with Captain Hammer. media type="custom" key="7726989" align="center"

**II. Learning Objectives**
//**Knowledge**// //**Skills.** By the end of the lesson you will be able to://
 * **Control statements** - control statements are code that specify which other code statements are run and when. There are five common control statements: if-else, switch, for, while, and try-catch. //If-else// and //switch// are branching statements. //For// and //while// are looping statements.
 * **If-else** - if-else is a control statement that checks some condition and decides which code to run based on that condition.
 * **Comparison operators** - Comparison operators allow you to compare the values of two variables to determine whether something is true or false. Comparison operators include:
 * > (greater)
 * >= (greater than or equals)
 * < (less than)
 * <= (less than or equal)
 * == (equals)
 * != (not equals)
 * **Logical operators** -- logical operators allow you to combine a series of comparisons. Logical operators include:
 * && (and)
 * || (or)
 * ! (not)
 * **Variables in MXML** - In MXML, you nest non-visual variables within a  tag.
 * **String concatenation** - You can create strings by using a plus "+" to concatenate them. For example, var name:String = "John" + " " + "Doe".
 * Write if-else statements
 * Use comparison and logical operators to create conditional statements.

**III. Concepts**
Control statements are code that specify which other code statements are run and when. Control statements define the program logic. There are five common control statements: //If-else// and //switch// both check some condition then decide which code to run based on that condition. //For// and //while// both run statements repeatedly until some condition is met. //Try-catch// will try to run some code (that might not run successfully) and if the code fails, run some backup code.
 * Control statements**
 * if-else
 * switch
 * for
 * while
 * try-catch

**Comparison operators** Most control statements check whether some condition is true or false before they decide what to do. Comparison operators allow you to compare the values of two variables to determine whether something is true or false. For example, if you have a statement //var result:Boolean = 5 > 4;// then the value of //result// will be //true//. Common operators include:


 * > (greater)
 * >= (greater than or equals)
 * < (less than)
 * <= (less than or equal)
 * == (equals)
 * != (not equals)

If-else is a control statement that checks some condition and decides which code to run based on that condition. For example, lets say you are writing some code to decide what a Prius object should do at a stop light. code format="actionscript3" var light:StopLight = new StopLight; light.color = "green";
 * If-else**

var car:Prius = new Prius;

if(light.color == "green"){ car.drive; }else if(light.color == "red"){ car.stop; }else{ car.speedUp; } code

You can use comparison operators to create true/false statements for control statements, however sometimes you want to check multiple conditions. Logical operators allow you to combine a series of comparisons. For example, if you have a statement:
 * Logical operators**

code format="actionscript3" var result:Boolean = (5 > 4) && (4 > 3) && (4 > 5); code

then the value of //result// will be //false// because that last comparison is not true. Logical operators include:
 * && (and)
 * || (or)
 * ! (not)

We've talked about variables in actionscript using the //var// statement, and you've created variables for GUI objects in MXML using, however In MXML, you nest non-visual variables within a  tag. This is a convention not a requirement. However, if you follow this convention, then other programs like Catalyst will be better able to read your Flex programs.
 * Variables in MXML**

**IV. Scenario**
**Henchman Force Assessment Trainer (a.k.a. Guessing Game)**

**Use Case** 1. Randomly decide how many robots are needed (we not really sure after all) 2. Ask henchman how many robots are needed to defeat captain hammer 3. Henchman guesses 4. Give the henchman feedback on whether the guess is too high, too low, or correct

You should know that you can create random numbers using the static functions in the Math class:
 * Math.random will create a random number between 0 and less than 1
 * Math.floor will round a number down to the closest integer

**V. Program**
Here's the program you'll write: code format="mxml"  

  { Math.floor(Math.random * 10) } 

 <![CDATA[

protected function enterClickHandler(event:MouseEvent):void {               var guess:int = int(_userGuess.text); if(guess > _secretNumber){ _feedback = "No, " + guess + " is too high."; }else if(guess < _secretNumber){ _feedback = "No, " + guess + " is too low."; }else{ _feedback = "Correct! Muwahahahah"; }           }

]]>   

 <s:VGroup x="10" y="10" width="178" height="94" gap="12"> <s:Label text="How many robots are needed to defeat Captain Hammer?" height="27" width="178"/> <s:HGroup width="123" height="25"> <s:TextInput id="_userGuess" width="42" restrict="0-9" maxChars="1" /> <s:Button label="Enter" click="enterClickHandler(event)"/> </s:HGroup> <s:Label text="{_feedback}" width="125" height="17"/> </s:VGroup> </s:BorderContainer> </s:Application> code

<span style="font-size: 1.3em; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 5px;">**VI. Self-explain (Try it!)**
Go over each tag in the MXML program and explain out loud to yourself what it does.

<span style="font-size: 1.3em; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 5px;">**VII. Program Walkthrough (Try it!)**
To create this program, do the following:
 * Create a new Flex project
 * Create the interface
 * Add an event handler to the Enter button
 * Create two pieces of data (variables), one for the "secret number" and one for the feedback to the henchmen
 * Set the initial value of the secret number using the Math library
 * Connect the interface to the feedback field so the interface will display the feedback.
 * Give the TextInput an id so that you can get the henchman's guess
 * In the event handler, get the henchman's guess, then use an if-else statement to set the appropriate feedback
 * Add some restrictions on the type of input TextInput will accept so that users can only enter a single digit at a time

<span style="font-size: 1.3em; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 5px;">**IX. Check yourself**
Can you answer the following questions? If not, time to review!
 * What do control statements do? What are 5 control statements commonly used?
 * Write an if-else statement.
 * What are the common comparison operators?
 * What are the common logical operators?

<span style="font-size: 1.3em; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 5px;">**X. Feedback**
Need to explain using multiple conditions for an else if test Need to explain Math.floor(Math.random) line Need to explain that declarations tags are run when program starts up Need to explain that need to reload browser to restart program