” 写作CISC 106程序、 辅导MATLAB课程程序CISC 106 Spring 2020 Project 2May 10, 20201 BackgroundFor this project, youll be implementing a solution to the packing problem as it relates to tetrominoes.Your goal will be to fill a board represented as a N M matrix of characters by packing arandom sequence of tetrominoes as tightly1 as possible on the board until it is full. Emptysquares on the board are represented as space characters, and filled squares are represented asone of I, J, L, O, S, T, or Z; each letter corresponding to a single square of one of the seventetromino shapes.NB: Please make sure to follow the links to and read all of the footnotes in this writeup, asthey contain useful information!2 ObjectivesAfter completing this project you will have gained the following experience with the MATLABprogramming language and environment: Creating functions which return one value as well functions which return multiple values. Working with 2-dimensional matrices in MATLAB: Using loops to iterate over elements Using filtering and logical indexing rather than loops when applicable Creating and manipulating slices. Using MATLABs robust plotting API: Creating and configuring figures and axes Drawing a scene composed of simple geometric shapes. Using MATLABs extensive online documentation.3 Outline of StepsThe following section describes the steps you must take in order to complete this project.1That is, with the fewest gaps between them1Project Set UpIn MATLAB creating a project is as simple as making a new folder and then saving your codethere as a collection of .m files. Create yourself a folder for your project, and then download thefollowing files from Canvas and put them there: run game.m draw test board.m unittests.m assertEqual.mNow if you open MATLAB and navigate to that folder you can open those files, look at thecode, and run the function defined in them at the REPL.2A MATLAB project is composed of files, each with at least one function in them. You can callthe first function defined in a file by using the name of the file3, and as such the convention is toname that first function the same as the name of the file. For this project, you will be followingthis convention.Open the unittests.m that you downloaded from Canvas. Note that there is one test alreadywritten for seven of the eight functions youll be writing for this project.4 Youll be following theBeginner Design Recipe when writing your functions for this project, and here is where youll putall of your tests. You should add two or three more tests for each of your functions as youreimplementing them to test different possibilities. You can run your tests any time at the REPLby entering the command unittests.From now on, when you see instructions saying something along the lines of write a functiondo stuff, you should understand that to mean first create a script and save it as do stuff5and then implement the function in that file, also calling the function do stuff. 写作CISC 106作业、 辅导MATLAB课程设计作业、MATLAB程序语言作业调试Write a function draw game board which consumes a board which is a 2D matrix of charactersas described in Section 1, and a figure6. This function will then clear whatever happens to be onthe passed in figure, and construct a new axes with that figure as parent. Youll then use therectangle function to create a graphical depiction of the passed in board on the created axesobject, and return the axes object. Keep in mind that youll be drawing one rectangle for everycharacter on the board – this means youll probably want to consider using nested for loops!You can run the draw test board function you downloaded in the Project Setup section totest your draw game board. Open draw test board.m to see the example board matrix, as well ashow to create a figure and how your function will be called. See Figure 1 for how your drawnboard should look.7 You should check the documentation for axes to see how to make yourboard match Figure 1. You can also take a look at this improperly configured axes document tosee some common axes misconfigurations.Part 2Write a function make board which consumes a whole number of rows and a whole number ofcolumns. This function will return a rows columns empty board – that is, a matrix of all space2Of course, if you try to run them now, youll get an error message because they call code that you will write.3Other functions defined in the same file can only be called within that file, and are called as theyre named in the file4As well see draw game board needs to be eyeballed for correctness, and as such you wont write unit tests for it.5you can add the .m extension or leave it off – MATLAB will add it for you automatically6See MATLABs documentation for figure7Assuming youre using the standard tetromino colors. Feel free to get creative and pick your own color palette! Thepositions and rotations and squareness of the shapes should match Figure 1 regardless of your color choices.2Figure 1: A correctly drawn test board, using the standard colors for the seven tetrominoes. Seethe discussion about setting colors in MATLABs documentation for rectangle.characters. You should be able to implement the body of this function in just a single line of code!Look in MATLABs documentation on Creating and Combining Arrays – from DocumentationHome, MATLABLanguage FundamentalsMatrices and Arrays.Part 3Write a function get shape which consumes a single character that should be one of I, J, L,O, S, T, or Z and returns the matrix representing that shape. The returned matrix should bethe smallest sized matrix necessary to represent the requested shape. Elements representingsquares occupied by the shape should be the letter, and elements representing empty squaressurrounding the shape should be a space. For example, the J could be represented by the matrix:[J J J; J]Part 4Write a function get board slice which consumes a board, a shape (such as what would bereturned by get shape), an integer row, and an integer column. The function should return theslice of the board starting from (row, column) and having the same dimensions as the shape, aswell as the ending row and ending column for the slice. For example, given the board[I ;I ;I O O ;I O O ]3the shape[L L; L; L]along with row 2 and column 3, the resulting slice would be[ ;O ;O ]the ending row would be 4, and the ending column would also be 4.If attempting to place the shape at the passed in row and column would cause the shape to bepartially or fully off the board, then the empty matrix [] should instead be returned. Going withour exmple from above, we would get the empty matrix if we instead passed in 4 for the column,since the three rightmost blocks of the L would be pushed off the right edge of the board.Part 5Write a function place shape which consumes a board, a shape, an integer row, and an integercolumn. The function should then return a new board which is the old board modified to havethe shape placed on the board such that location (row, column) corresponds to the upper leftcorner of the shape. Using the example from Part 4, with the board[I ;I ;I O O ;I O O ]the shape[L L; L; L]the row 2, and column 3, the resulting board would be:[I ;I L L;I O O L;I O O L]If you take advantage of your get board slice from the previous section, you should beable to do this in just a few lines of code without resorting to a for loop. Look at the ReplaceValues That Meet a Condition section of the Find Array Elements That Meet a Condition pagein MATLABs documentation. You can find a link to this page in the documentaion for logicaloperators.8 Then keep in mind that:1. If, for example, you have two matrices A and B with the same dimensions, you can replaceall the elements of A that correspond to elements in B that are less than 10 with thoseelements in B by doing A(B 10) = B(B 10).2. You can replace a slice of a matrix A with matrix B by doingA(start row:end row, start column:end column) = B, assuming B and the resultingslice of A have the same dimensions.8from Documentation Home, MATLABLanguage FundamentalsOperators and Elementary OperationsLogicalOperations.4Part 6Write a function try fit which consumes a board slice and a shape, and returns two values:1) A boolean which is true if the shape fits in this slice (that is, if all the squares that would beoccupied by the shape are spaces on the board slice), f alse otherwise; and 2) The fill score forthis fit – that is, the number of squares where a space in the shape lines up with a non-space onthe board slice. You should be able to implement the body of this function with 2 lines of code- one for each of the return values – using relational operators to filter your matrices along withsome reducing functions9Part 7Write a function find best place for rotation which consumes a board and a shape and findsthe best fitting location on the board for the shape in its current rotation. If two locations areequally good fits, then you should default to the lowest, rightmost position on the board. Thisfunction will return the following values:1. A boolean which is true if the shape fits on the board at all, f alse if there isnt anywhere onthe board where it fits.2. The row of the upper left block of the best location for the shape on the board.3. The column of the upper left block of the best location for the shape on the board.4. A numeric score for the shapes best location on the board. Higher scores are better. Thisscore can be any heuristic you believe leads to a better packing of shapes on the board.NB that the fit returned by try fit can be used here, either as the entire score or as acomponent of it.You should make use of your try fit and get board slice functions when implementingthis function.Part 8Write a function find best place which consumes a board and a shape. This function shouldfind the best fitting location on the board for the shape over its current rotation and three possible90rotations10 This function will return the following values:1. A boolean which is true if the shape fits on the board at all, f alse if there isnt anywhere onthe board where it fits.2. The row of the upper left block of the best location for the shape on the board.3. The column of the upper left block of the best location for the shape on the board.4. The number of clockwise11 rotations (which will be between 0 and 3, inclusive) required forthe best fit.You should make use of your find best place for rotation function when implementingthis function.9Namely, the sum and all functions. You should read MATLABs documentation for these.10Look up how to do this on the same documentation page that discusses creating and combing arrays referenced inPart 2! This time youll want to look in the section on reshaping and rearranging.11Yes, the direction here is important!54 Putting it all TogetherOpen the run game.m file you downloaded in the Project Setup section. You dont need to changeany code here, but you can look at it to get an idea of how its calling the code you just wrote inthe above parts. Now you should be able to call run game at the REPL and watch your programfill up the game board as best it can.Submit the following files on Canvas: unittests.m draw game board.m make board.m get shape.m get board slice.m place shape.m try fit.m find best place for rotation.m find best place.m如有需要,请加QQ:99515681 或邮箱:99515681@qq.com
“
添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导。