CS1132 Fall 2014 Assignment 2 due 11/20 11:59pm Adhere to the Code of Academic Integrity. You may discuss background issues and general strategies with others and seek help from course staff, but the implementations that you submit must be your own. In particular, you may discuss general ideas with others but you may not work out the detailed solutions with others. It is never OK for you to see or hear another student s code and it is never OK to copy code from published/internet sources. If you feel that you cannot complete the assignment on you own, seek help from the course staff. There are two parts to Assignment 2. art 1 is given below; art 2 and the submission instructions will appear in a separate document. Do not use the break or continue statement in any homework or test in CS1132. 1 Morse Code erhaps the most famous of all coding schemes is the Morse code, developed by Samuel Morse in 1832 for use with the telegraph system. The Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few special characters (such as period, comma, colon, and semicolon). In sound-oriented systems, the dot represents a short sound and the dash represents a long sound. Other representations of dots and dashes are used with light-oriented systems and signal-flag systems. The international version of the Morse code appears in the file mosecode.txt. We will refer to this set of characters (the letters of the alphabet, numbers, and punctuation marks) and their Morse-equivalents as the Morse Table. (i) Function and subfunctions Implement a function and two subfunctions, as specified below, that together will allow us to translate between English and Morse Code. In the description below, English refers to the upper and lower case letters of the English alphabet, the digits (0 to 9), and the punctuation marks in an English paragraph. Function (main function): function morsecell = MorseCodeConverter(morseCodeFilename, inputfilename,... convertedfilename, flag) % Translate a passage from the plain text file named by inputfilename. % If flag is 1, inputfilename contains English text, which should be % translated to Morse code. If flag is 2, inputfilename contains Morse text, % which should be translated to English. Use one blank (space) between % Morse-coded letters in a word; use three blanks between Morse-coded % words. The translated text is stored in the plain text file named by % convertedfilename. The translation of each line of the input file should % appear as one line of the output file. Return a cell array that stores % the data of the Morse Table. % % INUT: % morsecodefilename: a string that names the textfile that stores the Morse % Table. The first line contains title text. Each of the remaining line % has this format: an English character, followed by a comma, followed % by the Morse coding of that character. % inputfilename: a string that names the input file % convertedfilename: a string that names the file that contains the % translated text. % flag: Either 1 or 2. 1 means convert from English to Morse Code; % 2 means convert from Morse Code to English. 1
% % OUTUT: % morsecell: an n-by-2 cell array where n is the number of English-Morse % pairings in the Morse Table. morsecell{k,1} stores the kth English % character in the Morse Table; morsecell{k,2} stores the Morse Code (a % string) of the kth character in the Morse Table. The main function must read the data file storing the Morse Table in creating the cell array do not hard code the Morse Table into the function. See section (ii) for the list of built-in functions allowed for file and string handling. The main function must make effective use of the following two subfunctions, which you will implement as specified: function morseword = EnglishtoMorse(engWord,morseCell) % Translate one English word to Morse code. % INUT: % engword is a string. It is a "word" in English, which is simply a string % of non-space characters. E.g., the string CS1132 is considered a % word. Treat any symbol not represented in the Morse Table as a space. % morsecell is the cell array that stores the data of the Morse Table. % OUTUT: % morseword is a string. It is the Morse translation of engword. function engword = MorsetoEnglish(morseWord, morsecell) % Translate one Morse code string representing a word to the corresponding % English word. % INUT: % morseword is a string. It is a word coded in Morse. Assume morseword to % be a correct Morse string (no unspecified symbols). % morsecell is the cell array that stores the data of the Morse Table. % OUTUT: % engword is a string. It is the English translation of morseword. (ii) Built-in functions Below is a list of useful built-in functions for handling characters, strings, and files. Use only these builtin functions for handling characters, strings, and files. You may not need all of them. You can of course still use general built-in functions not related specifically to strings and files, such as length, zeros, rem, etc. You can use: fopen, feof, fgetl, fclose strcmp, upper, isspace, isletter You must NOT use Matlab built-in functions find, strfind, findstr. (iii) Hints The provided file encodedfile.txt contains correct Morse Code that you can use for testing. encodedfile.txt is the Morse translation of the English text file whychoosematlab.txt. Suggestions for program development: 1. Start with reading the Morse Table to create the cell array in the main function. 2
2. Next, develop each of the subfunctions one at a time, as an independent function instead of a subfunction! This way you can test each function independently without involving the rest of the code and functions. 3. Continue working on the main function. It is best to isolate the file handling tasks. So read the input text file and store the data in a cell array store one line of text into one cell. Then iterate through the cell array to do the translation, storing the translated text in another cell array. Finally open the output file and write the cell array of translated text into the file. You can implement additional subfunctions as appropriate. 4. Don t forget to close each file that you open. 5. After testing your entire program (all functions) to make sure that it works, copy to MorseCodeConverter.m the other functions that should be subfunctions. Submit only MorseCodeConverter.m. 2 Graphics Fun! eg Solitaire eg solitaire is a one-player board game with the goal of removing all pegs except one from the game board. If a single peg remains on the board and it is in the middle, then you have won! First, take a look at the Wikipedia entry on eg Solitaire (http://en.wikipedia.org/wiki/eg_solitaire) and pay attention to the second section (Board) and third section (lay) to learn how the player makes a move on the board. You will implement a Matlab function for playing the game on the English board (not European board). Our game board looks like this at the beginning of the game: all the holes in the board are filled with pegs (marked ) except for the hole in the middle of the board. An empty hole is marked in blue. A valid move is always a vertical or horizontal jump of one peg over another into a hole on the board. The jumped over peg is removed from the board. Below we show an example sequence of two moves starting from the initial board configuration. The first move is to select a peg (marked yellow) and jump it east, removing the jumped over peg that was adjacent to the selected peg. The second move in this example selects a peg and jumps it south. Observe that every move results in one more free space (blue) on the board. Select a space to put the yellow peg Select a space to put the yellow peg In this final exercise in the course, we give you the freedom to structure the code as you see fit. You should define helper functions as needed to make your solution modular do not submit one long giant function. The helper functions, i.e., subfunctions, must be saved in the same file containing the main function for the game: egsolitaire.m. The function takes no input argument and does not return any value. Below we give the specifications and some hints: Initialize the game board as the English game baord as shown in the left-most figure above. Valid move: A valid move comprises the selection of a valid peg and the selection of a valid empty space to which the selected peg can jump. Only the four directions north, east, south, and west are used. A peg can be selected for a move if, from that peg looking in any one of the four directions the adjacent position is a peg and is immediately followed by a space. Given a valid selected peg color that peg yellow a space selection is valid if there is exactly one peg between the selected space and the yellow peg. A valid move removes the jumped over peg from the board and turns that position to an empty space (blue). Throughout the game, appropriate messages should be displayed above the game board (the title area of the figure) to tell the user what to do, as shown in the figures above. 3
The player clicks on one of the squares of the game board in order to select a peg or a space. Use the Matlab built-in function ginput to get the coordinates of the user s click: [a,b]=ginput(1) stores the x and y coordinates of a user s mouse click in a and b, respectively. Any invalid click should not change the game board and the game proceeds only after the user makes a valid click. Throughout the game, an empty space is a blue square, a peg selected for a move is a yellow square with a inside, and the remaining pegs on the board are each a white square with a inside. The board should be updated according to each valid move. End of game: The game must stop when no valid move is possible. If a single peg remains on the board and it is in the middle, then the player wins; otherwise the player loses. An appropriate message indicating whether the player has won or lost should be displayed above the game board. The interaction happens only in one figure window. Below are some highly recommended subfunctions to design and implement in order to decompose the problem. We give only a descriptive name and the general idea for each recommended subfunction. It is up to you to design and implement the details or modify the purpose of a subfunction. function gameboard = creategameboard(): gameboard is a 7-by-7 matrix representing the English game board. Use 1, 0, and -1 to represent a peg, an open space, and an invalid location. function [isvalid, freespace] = checkroposedeg(peglocation, gameboard): Is a userclicked square a valid peg selection for a move? This subfunction is helpful for both determining whether a peg selection is valid and for checking for the end-of-game condition. This function returns also the locations of the valid free space for the proposed move to make the check for the next user-clicked square, the proposed space for the selected peg, simple. function [peglocation,freespace] = chooseeg(gameboard): Allow user clicks until a valid peg is selected. aint the valid peg yellow. function spacelocation = choosespace(freespace): Allow user clicks until a valid space associated with the selected (yellow) peg is chosen. function gameboard = updateboard(gameboard,peglocation,spacelocation): The function name and parameters say everything that needs to be said. But remember to update both the matrix and the graphic. function isendofgame = endofgame(gameboard): Check that no valid move is possible. 3 Self-check list The following is a list of the minimum necessary criteria that your assignment must meet in order to be considered satisfactory. Failure to satisfy any of these conditions will result in an immediate request to resubmit your assignment. Save yourself and the graders time and effort by going over it before submitting your assignment for the first time. Although all these criteria are necessary, meeting all of them might still not be sufficient to consider your submission satisfactory. We cannot list everything that could be possibly wrong with any particular assignment! Comment your code! Make sure your functions are properly commented, regarding function purpose and input/output parameters. Suppress all unnecessary output by placing semicolons (;) appropriately. At the same time, make sure that all output that your program intentionally produces is formatted in a user-friendly way. Make sure your functions names are exactly the ones we have specified, including case. Check that the number and order of input and output parameters for each of the functions match exactly the specifications we have given. Test each one of your functions independently, whenever possible, or write short scripts to test them. 4
Check that your scripts do not crash (i.e., end unexpectedly with an error message) or run into infinite loops. Check your script several times in a row. Before each test run, type the commands clear all; close all; to delete all variables in the workspace and close all figure windows. 4 Submission instructions 1. Upload files MorseCodeConverter.m and egsolitaire.m to CMS in the submission area corresponding to Assignment 1a in CMS before the deadline. Late submission is accepted up to 24 hours after the deadline with a 10% penalty. 2. After grading: If you resubmit the assignment, upload your corrected files and be sure to select the Regrade Request option. 5