Jump to navigation

Assignment #3

Due Tuesday, March 25

75 Points

This assignment gives you a chance to expand your experience and sophistication with the use of the JavaScript core language. By the time you have completed this assignment, you should have the solid understanding of JavaScript syntax and basic programming techniques you will need to move into the more advanced assignments required during the second half of the course.

Since you will be writing several separate programs, each requiring its own XHTML page (and CSS if you choose to use it), start by writing a simple XHTML page which will eventually provide links to each program’s XHTML page. Make it clear from each link’s label and/or context to which program it leads. Eventually, when the assignment is complete, publish this page (along with all the files comprising your solutions) to the wcit server and add a link to it to your home page.

Defining and testing a function

Good programmers use functions extensively. A program that has been modularized into a collection of self-sufficient functions is easier to write, understand and maintain. And if carefully written, there’s a good chance the functions one writes today will be useful in future projects too, saving lots of time and effort down the road.

In Assignment #2, you wrote some code to generate a random integer within a specified range. This is exactly the sort of code that experienced programmers look to compartmentalize within functions. Since the code could be used to generate a random integer in any range at all, it could be useful for simulating the roll of a die, the flip of coin, drawing a card from a deck, picking a pebble from a jar, and a virtually limitless range of other scenarios.

So take that code you wrote to implement the random integer algorithm for Assignment #2 and turn it into a function. Your function should accept low and high as parameters and return the generated integer as its return value.

Write your function to expect the unexpected. Check that the values of the low and high parameters are both integers and that the value of low is less than the value of high. If your function discovers a problem with the parameter values, it should return a Boolean value of false (instead of the generated integer).

Store your function in a JavaScript library so that you can incorporate it easily into other programs, then write a separate JavaScript program to thoroughly test your function (along with the XHTML page necessary to bring the library and your test program together). Your test program should be carefully designed to test your function as completely as possible. It should test both several combinations of both valid and invalid values of the low and high parameters.

When testing a particular combination of valid low and high values, your program should invoke the function with those values at least 1,000 times, tracking the highest and lowest results. At the end of each such test, the program should display an alert reporting the low and high values being tested, the highest and lowest result, and whether or not the test passed.

When testing a particular combination of invalid low and high values, there would be no point in running the test numerous times. Therefore, for such a test, simply run the test once, check the function’s return value, and produce an alert dialog indicating the low and high values being tested, why they are invalid and whether or not the test passed.

This sort of program that runs a function (or any other piece of code) through many different scenarios and reports its findings is commonly called a test frame, and learning how to write one that thoroughly tests your code is an important skill to acquire as a programmer.

Defining and testing an object

In modern JavaScript programming, it would be more common to find the functionality you created in the preceding section of this assignment encapsulated within an object. Therefore, in this part of the assignment, you’re going to take that code and use it to define a simple object.

In the object you define, the function that generates a random integer will need to be defined as a method. Likewise, the test frame code that you wrote will also need to be incorporated into one or more additional methods. How you divide things up is up to you. However, your object should contain at least two methods, one that generates a random integer given low and high as parameters and an init() method that executes the tests.

Use the textbook’s Core object library to schedule your object’s init() method so that it runs after the XHTML page is done loading.

Building a full-fledged application

For the last part of this assignment, you are going to design and implement your first full-fledged application — a guessing game. You should implement your game as an object and use the textbook’s Core object library to begin running the game as soon as the XHTML page is done loading. You may display whatever you wish within your XHTML page as a backdrop, but use only dialog boxes for your game’s user interface.

Once the game begins running, it should allow the user to play as many matches as they desire. Each match consists of the program picking a random integer and the user getting three chances to guess the number. For the first five matches, have the program pick a number in the range 1 to 10, inclusive. Then, for each subsequent set of five matches, increase the range by another 10, making those matches more difficult. For example, for matches 6 through 10, the program will pick a number in the range 1 to 20, inclusive. For matches 11 through 15, it will use the range 1 to 30, and so on.

After each guess, the user should receive feedback indicating whether or not they have successfully guessed the program’s number. If they have, notify the user that the match is over and a win has been recorded. If they have not correctly guessed the program’s number, the user should be notified of that fact and informed whether their guess was too high or too low and how many guesses they have left. If the user has any guesses left, they should be allowed to guess again. Otherwise, they should be notified that the match is over and a loss has been recorded.

After each match, the user should receive a performance report consisting of a list of matches played in the current game. Each match in the game should be listed on a separate line of the report indicating the range from which the program picked its number, whether it was a win or a loss and how many guesses were used. After being presented with the report, the player should be given a choice whether or not to play another match. If they choose not to play another match, the program should thank the user for playing and then end.

In writing your code, be sure to expect the unexpected. Make sure you take into account all the choices the user has when presented with a dialog box. And when asking the user for a guess, be sure to check that the guess is valid. If the user does not provide a valid value, keep asking until they do. Any time the user has the option to click a Cancel button within a dialog, treat that as a signal they wish to immediately end the match and the game. However, before acting on that signal confirm that they really want to terminate the match and the game. If they do not, redisplay the dialog that they canceled. If they do, display the final report and then thank them for playing before ending the game.

This is an excellent problem to which to apply top-down design. Viewed as a whole, it’s reasonably complex. However, it’s easily broken down into simpler subproblems that can be tackled one at a time. Get each subproblem solution working reasonably well before moving on to the next one. For example, the game is essentially just a series of matches, which are simpler. Each match, in turn, is really just a series of guesses, which are simpler yet. So if you start by figuring out how to handle a single guess, you should be able to use that solution in creating a solution that will play a single match. And then you can use that solution several times in creating a solution that will play an entire game. And, of course, make use of the code you wrote in the earlier parts of this assignment to simplify the implementation of this program.