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.
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.
When this happens, display an alert box that explains this and that will also show the code.
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:
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.
Code | Guess | Key Values | Explaination |
1234 | 2536 | pv | 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). |
1234 | 6544 | p | 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. |
1525 | 3546 | p | 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. |
1213 | 4156 | v | 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. |
1213 | 4116 | pv | 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. |
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.
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.