4. Focus Chimes part 2

Complete the Focus Chimes program begun in last week's lab. You will implement BtnReset to reset the game and implement BtnCheatOrHide to display (or hide) LblHistory. The functionality of BtnTest is extra credit.

For reference, the completed app is described in the following video (2:25):

The components of the interface are shown below:

Focus Chimes app components

Activity

Start with the App Lab starter code given here.

Make your group’s own version of the above App Lab game by first selecting the View Code button, and then the Remix button:

Requirements & Rubric:

Already provided for you is the code to append the letter corresponding to the button pressed to LblHistory. This is done by using a variable called moveHistory and initialize it to a single blank character. Upon each button press the appropriate letter is added to the end of this variable (e.g. moveHistory = moveHistory + "A") and stored in LblHistory using the UI controls / setText(...) block.

  1. (10 points) Implement the Reset button.
  2. Set variable mode to "Train" when your program starts. When BtnTest is selected it will get changed to "Test".
  3. When BtnReset is selected:
    1. Set variable mode back to "Train"
    2. Set moveNumber to 0 and redisplay it
    3. Reset moveHistory to a single blank and store it again into LblHistory.
  4. (10 points) Implement the Cheat/Hide button.
  5. Selecting BtnCheatOrHide should have the effect of:
    1. (5 points) Toggling the display of LblHistory on and off.
    2. To see if LblHistory is currently displayed you can check if getProperty("LblHistory", "hidden") == true
    3. You can show or hide LblHistory using showElement("LblHistory") and hideElement("LblHistory")
    4. (5 points) Toggling the text in BtnCheatOrHide between "Hide" and "Cheat".
    5. You can set the text in BtnCheatOrHide button using setText("BtnCheatOrHide","new text");

Extra Credit

Up to 5 points extra credit may be given for solutions that implement BtnTest so that the user guesses are matched against those stored in the history variable. When BtnTest is selected you should set variable mode to "Test". The code for each chime should check the mode ("Train" vs "Test") to see what section of code should be executed.

  • Incorrect Chime is Selected:
  • If an incorrect chime is chosen enable BtnNo and play sound ScaleUnresolve.mp3. To see if an incorrect chime is chosen you will need to match against history by comparing the expected history letter to the current button pressed. I suggest you have a variable that is set to "A" if BtnA is pressed, "B" if BtnB is pressed, and so on. This letter will need to be compared to a particular character within the history variable string that stores the previously selected sequence of chime presses. Pulling out a single character from a string is explained in the next paragraph.
  • Extracting a Single Character Substring:
  • When attempting to match a previously stored pattern you will need to use substring to pull out an individual letter from the history variable. Individual characters stored in a variable are numbered using 0-based indexing, where the first character is in position 0, the second character is in position 1, and so on. You should have already compensated for this by initializing the history variable to a single blank character, so the position of subsequent characters stored there match with the moveNumber variable. Substring retrieves the characters from some starting index until some ending index position. In our case the index is the move number and we want to retrieve a single character, so retrieving a single letter from moveHistory and storing it into a variable called expectedLetter might look like the following:
  • expectedLetter = moveHistory.substring(moveNumber, moveNumber+1);
  • All Correct Chimes are Selected:
  • If the entire test sequence matches then enable BtnYes and play sound ScaleResolve.mp3. You can identify when you've finished matching against the entire history string by comparing the current moveNumber to the length of moveHistory. To find the length of moveHistory you can use
  • moveHistory.length
  • and compare against that, or store it into some variable. Note that your count will be off by one, since you initiated the history variable to a single blank, so you will actually need to subtract 1 from length of moveHistory to get an accurate comparison against moveNumber to know when you've finished guessing the entire sequence.

After success or failure on a turn you should hide both the "Yes!" and "No" messages (BtnYes and BtnNo) that might currently be displayed.

Turning it in:

Get the link to your solution using the Share button near the top-left of your App Lab page:

Share Link

Turn in your App Lab program by posting the link to it in the essays google form, calling the essay topic Chimes Two.

Also add it to your online portfolio.