CS 101 - MP 4

JavaScript - Code Breaker

Due - Thursday 3/14/2002 at 11:59 pm

For this assignment, you are to create a game in JavaScript that will random create a code and allow the user to guess the code. For each guess, the user will be given some (vague?) information about the correctness of the guess. The goal is to guess the code with the fewest number of guesses. This game is sold under the name Mastermind by the Pressman Toy Company.

This program will not be exactly following the offical rules of Mastermind but will be following a slight variation that should be easier to create in JavaScript. Instead of using six different colored pegs to create the code, we will use the digits from 1 to 6. The code will consist of 4 digits. When the program is to create the code, each digit must have the same probability of being picked. Therefore, do NOT use the algorithm that is given in the textbook that does not give an even distribution when creating the random number.

Your program is to use 4 selection boxes and one button to allow the user the guess the code. The selection boxes are to have options for all 6 digits and must display all 6 options. The first selection box will correspond to the first digit of the code, the second selection box to the second digit, and so on. Each selection box should only allow a single option to be chosen and must NOT have any option selected when the page is first displayed. Once the user has selected the desired code from the four selection boxes, the user will click the button to make the guess. If any of the boxes does not have an option selected, display an alert box with an appropriate message and put the focus on the first selection box without a selected option. When the user clicks on the guess button and all four selection boxes do have an option selected, process the guess and unselect all four selection boxes. This will help decrease the chance of the user making an guess accidentially.

The user is allowed to make 10 guesses to break the code. Your program is to display a field that informs the user of the number of the current guess. This field is to initially be a value of 1 and is to be incremented every time a guess is made. This field should not be allowed to be changed by the user. One way to do this is whenever the field gets focus, change the focus to some other element on the screen. The use of the onFocus event handler can be used to do this.

There are three possible things that can happen with each guess.

  1. The user guesses the code correctly.

    When this happens, display an alert box with a message stating the code was broken, how many guesses it took to break the code and what the code was.

  2. The user does not guess the code in 10 guesses.

    When this happens, display an alert box that explains this and that will also show the code.

  3. The user does not guess the code and has more guesses.

    When this happens the program must give some information about the correctness of the guess. Your program will display a "guess history and result" field. This field is to be a text area with each guess being listed on a different line. The text area must be large enough to show the guess information for all 10 guesses without scrolling the text area field. Each line will first list the user's guess followed by the information about the correctness of the guess. Just like the guess counter field, the guess history text area should not be allowed to be changed by the user.

    The correctness of the guess is given by "key values". There are two key values:

    When the key values are given, the user is not told which key values correspond to which code values. Therefore the user must try to figure out which selected values are correct. A good way to make sure that the order of the p and v keys do not given away any extra information is to have all of the p's listed first followed by all of the v's.

    The only rules are that one key values correspond to a single pair of code values (where a pair is one value from the code and one value from the guess) and the p key value takes precedence over the v key value. The real problem comes when the same value appears multiple times in either the code or the guess. Let us look at a few examples.
    CodeGuessKey ValuesExplaination
    12342536pv The p value is because there is one right value in the correct position (the value: 3). The v value is because there is one correct value in the wrong position (the value: 2).
    12346544p There is only one p value since the value of 4 in the code will only match to one of the values of 4 in the guess and a key value of p takes precedence over the key value of v.
    15253546p There is only one p value since the value of 5 in the guess will only match to one of the values of 5 in the code and a key value of p takes precedence over the key value of v.
    12134156v There is only one v value since the value of 1 in the guess will only match with ine of the values of 1 in the code. Since neither set match the same value to a correct position the key value will be a single v.
    12134116pv There are two sets of matches with the value of 1 between the code and the guess. Since the p key value takes precedence, one match must match the 1's in the third position of both the code and guess resulting in the p key. Then the othe match will be the first position of the code with the second position of the guess resulting in the v key.
    As a hint as how to right your code to correctly determine this, when determining the key values keep track of which positions in the guess and the code have been matched. First attempt to find matches for the p key for all four positions. Then try to find matches for the v key for positions that have not yet been matched.

In addition to the above items, your page must have the following:

Make sure your code is written using good programming style. This includes the use of comments, indentation, blank lines and meaningful identifier names. Each function that you write must have comments at the start explaining the purpose of the function. Also, at the top of your HTML file must have a comment explaining the purpose of the file including your name, and lab section day and time.

The program is to be submitted to the digital drop box for grading before the time the program is due.

Notes on the assignment and Academic Dishonesty

Since everyone in the class is creating the same program, each student must take reasonable steps to make sure their assignment is not copied. Recall that academic dishonesty includes supplying information as well as receiving information. Towards this end, you are not to have your assignment readable in your web directory or any subdirectory of a web directory (the WWW directory is the web directory on the CS machines, the public_html directory is the web directory on the icarus machines) until the day after the last due date for the assignment (Friday 3/15/2002). Any student who does not follow this will receive a grade of zero for the assignment.

In order to work on this assignment on a UNIX machine, you are to use the "Open File" option in the Netscape browser to view and run your assignment. Remember that this will only open a file on the physical machine that you are working with. If you are running Netscape on one of the EECS machines, you can open any file in your account (not only the ones in your WWW directory). If you are not running Netscape on one of the EECS machines, you will have to transfer the file to the machine your are working on before you can open the file in Netscape. To transfer the file, use either FTP or Fetch.

Another note on academic dishonesty, you should never show your written code for an assignment to another student. You may discuss ideas related to the project, but code must be entirely written on your own. If you and another student must discuss code, you must discuss an example other than the assignment.