1.5

1.5 | 2 ratings Rate this file 14 downloads (last 30 days) File Size: 4.39 KB File ID: #21720

domino tiles

by ben payne

 

11 Oct 2008 (Updated 01 Nov 2008)

Code covered by the BSD License  

domino tiles random matrix, filling the matrix like in the game "snake"

Download Now | Watch this File

File Information
Description

Create a random matrix of a non-intercepting snake, like the game ("worm" or "snake"). Also described by filling a chessboard with dominos, except now the board can be rectangular, or any arbitrary shape (by changing the boarders).

Uses an infinite loop to enumerate possibilities. There are a finite number of solutions for a given matrix size. Computation time grows based on size of matrix.

Code is documented.

MATLAB release MATLAB 7.0.1 (R14SP1)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (9)
13 Oct 2008 John D'Errico

No help at all. Nothing that says what it does, or why you might use this.

This is a script, that starts out by clearing your matlab workspace, so if you were actually doing any work before this, it is gone. Wiped out. Burnt toast.

There is no H1 line, so next year when you actually want to find this mess, you will have no idea what it was called, and no way to find it. At least unless you can recall the name "draft0_2headed.m" by memory. Read the help for lookfor.

When you actually do try to run this, a 6x6 matrix of integers pops up on the screen. What this means, I have no idea. But then it just sits there and does nothing. Apparently I am supposed to do something at this point, but there is no hint as to what. I look into the code, and see a pause statement. Ah. Ok, so its been paused. But why? What information is being given to me? No idea. No hint.

The code itself is poor. Infinite loops, many places where variables grow without restraint.

One thing about this code that I like is the many internal comments that describe what is being done, as well as the use of white space. The code itself is moderately easy to read. But, after reading through the comments, I still have no idea what the code does, or why it does it, or why I should even remotely care.

I was originally thinking this was was 2 rating, "needs a huge amount of improvement". But it appears to have no redeeming value. Don't waste your time on this file.

30 Oct 2008 ben payne

Hello. Thank you for the comments and for looking at the code.

I don't have a good description for what the code does other than it is related to visualizing what happens in this game
http://www.snakegame.net/
when the snake becomes very long and fills up the entire board (in this case, a matrix). Another way to think of the program is filling a chess board with dominos such that they could be read end-to-end.

I'll respond to the comments in order.
-"No help at all. Nothing that says what it does, or why you might use this. "
--> I'm not sure who would be interested in using this. It may have applications in biology (growing a culture in a petri dish?).

-"This is a script, that starts out by clearing your matlab workspace"
--> Yes, I use "clear" to minimize any error that may be caused by whatever you have in place. I don't recommend testing unknown code in an environment where you are working.

-"There is no H1 line"
--> I was not aware of this function. I will include it in the next version.

-"6x6 matrix of integers pops up on the screen. What this means, I have no idea"
--> See above. The 6x6 matrix is what we are looking for. I use integers to fill in the dominos. The script pauses so you can see the output. I'll add a text comment to the screen so that you know what the output is.

-"Infinite loops, many places where variables grow without restraint."
--> this is intentional. I'm looking for all possible enumerations, so I use an infinite loop and since the parameters measuring this have no bound, I can not limit them. Eventually you stop running the program once you have what you desired.

Thank you for spending enough time to leave a useful comment. I look forward to more interaction.

Ben

30 Oct 2008 ben payne

I forgot to mention that the boarder of "-1000" elements is so that you are not restricted to rectangular matrices. By placing "-1000" elements in arbitrary locations one can create any shape desired (circles, islands, etc).

01 Nov 2008 John D'Errico

This has improved, but quite slowly. It has still a ways to go.

There is still absolutely no help at all. Nothing that explains what it does once you have downloaded it. Nothing that helps you to know or care why it even exists.

Next, the author insists that this is a script, and so must have a clear and clc up front. But why? Learn to use functions instead. They have no need to clear your workspace and then leave a pile of crap behind. There is no reason to write this as a script, nor any reason at all to clear the user workspace. Matlab has functions for a good reason. Learn to use them.

Looking at the code itself, I'd suggest the author learn to read the mlint comments. (Look on the right hand side of your editor window. You will see many orange or red bars. When you pass the mouse over them, or click on a bar, it shows you some ideas about how to improve your code. Or call mlint on your m-file.) In this case, the most common comment that should be looked at is about the several places where arrays are dynamically grown without benefit of preallocation. Growing such an array is a good way to make your code abysmally slow. Perhaps that is why this code is so terribly slow.

I tried to run the code, but it just sat there for a few minutes, having produced nothing. I eventually gave up on it. I tried it again, leaving it for more time, still nothing produced.

My rating has gone up just a bit, perhaps enough to be worth 2 stars. Code that seems to do nothing, has no help, is a script that clears your workspace. Hmm. I've been generous this time to round up my rating to 2 stars.

01 Nov 2008 ben payne

Thank you very much for your help again. I learned about mlint() and used it to eliminate some
of the bugs that were reported.

Unfortuantely two arrays, "times" and "tries" are not-pre-allocated since their size is not knowable beforehand.

I replaced the universal "clear" with a clear of just the variable names I use.

I have not converted the script to a function, but if I learn how to do that and it helps I'll update.

Thanks again and I look forward to more feedback.

01 Nov 2008 John D'Errico

Why not write it as a function? Return only the result that a user cares about. This is what functions are useful for. They are friendly to the user. A function, or any code for that matter, should have help. Even while you know how to use your own codes, other people who are strange to it will have no idea. So help them. Look at the help styles that you see in the tools the MathWorks already provides. Such help will greatly increase the usability of your code.

Preallocation is not always necessary. But this code seems slow, so there is something that is making it slow. Preallocation is a common cause. Use the profiler, find the bottlenecks in your code. If these arrays that are grown never get too large, then it may not be important. Find that out.

If you absolutely must grow an array without benefit of preallocation, then there are some tricks that can work. One of them is to create an array using zeros that is larger than it will ever need to be. Very often, even though you do not know the final size of an array, you may know a maximum that size will never exceed. Then just stuff your new points into that array using indexing. Keep track of the last element that you've inserted. At the end, chop off the extraneous elements.

Another trick that sometimes works is to use a cell array. Preallocate the cells to be empty, but have many such cells. Stuff your new elements into the cell array. Then at the end, you can concatenate all of the cells into one single flat array, using cell2mat. Remember a cell array is just a list of pointers, don't grow the number of cells if you can avoid it. There are a variety of these tricks one can use to avoid the problems of dynamic growth. One that I like is to use a few cells, each of which is preallocated to be fairly large. Then index into those cells, and when you run out of room in one cell, move onto the next.

Next, use tools like a waitbar to show the user what progress you are making. Surely there is some metric you can use to define the extent of progress. This gives them some comfort that your code is doing something, rather than just in an infinite loop. Or, perhaps MATLAB has crashed. A user will not know what has happened. So be friendly.

Good code is FRIENDLY.

My rating will continue to go up as I see improvements, and some authors may think me to be too hard on them. In the end, if you can tear a "well done" out of me, it is something to be proud of. You can then take the things you have learned to improve all of your future work.

01 Nov 2008 ben payne

I uploaded a new version but it hasn't propagated yet.

I have an idea on implementing a function but it will probably have to wait until next semester.

I'm not sure why making the entire program a single function would help. The program is meant to stand on its own.

I'm not sure what "help styles" refers to.

I'm not sure why it seems "slow." What feature is slow?

The "waitbar" is interesting (I didn't know about it) but it does not apply to this program. A single realization is attempted, and when it fails the next realization is attempted. There are many failures for a "complete" realization (success) but a progress bar window is not applicable.

I'll try and keep updating until I earn "well done."

01 Nov 2008 ben payne

I read about the difference between a script versus a function and this file is a proper script. It is not meant to be called by other actions. It is a stand-alone process.

05 Nov 2008 John D'Errico

Sigh. This is indeed a script file. But that does not mean that it should be so. Why do you submit a file to the FEX? Two main reasons I'll argue for. Marco is unexplainable. ;-)

1. Exposition. Teach someone how to use a certain method or technique. This should include a relatively massive amount of text, explaining why you have done something as you did. These submissions might often be as published m-files. Cell mode is also a handy tool here, allowing the user to change a parameter and see what happens to the result. Scripts are often used for this type of submission, and are entirely acceptable in my opinion. Well written, such an expository script will receive a top rating from me if it deserves that rating.

2. Use of the result in some further analysis. Most submissions on the FEX are of this type. As such, it only makes sense for the result to be returned by a function. The user has no wish to look deeply into your script to figure out, from the many variables that are generated, which are of even remote interest to the user. A function takes an input, returns an output. Some functions may arguably need no input. And some functions may produce no output variable, only a graphic, or a result written to a file. Even here, a script is a silly thing to use, trashing the workspace of the user just to generate a plot.

A script that generates its result as a fixed variable is kludgy, inelegant, and amateurish. Sorry, but it is. Suppose the user chooses to run your script a second time, but retaining the first result? Sorry, this script overwrites the first one. You cannot just direct the result into a different variable name.

You call this a "stand-alone" process. Sorry, but that concept is silly. Will any user ever run this code, look at the result, and then log out of matlab, happy with the result? At least, will they do this more than once? Why? What purpose does this tool serve? If it returns something of any value at all, then there must be another step after this that uses the result. If there is such another step, then you should have written it as a function.

Yes, I knew a lot of people from my past place of employment who wrote many script m-files. Those scripts were useful ONLY to themselves, but not to anyone else in the world. Not even useful to someone who sat in the next cubicle over from them. In that same environment, I wrote hundreds of functions that were used/shared by all of my co-workers. These functions formed the backbone of a thriving community of users. If you wish another example, look at the codes supplied with MATLAB itself. Do you see hardly any scripts (that are not demos?) Do you see thousands of functions?

The point is, if your code is to be useful to others in the world, a function is almost always the proper way to present it to that world. A WELL DOCUMENTED FUNCTION.

Please login to add a comment or rating.
Updates
30 Oct 2008

based on user feed back I added an "H1" line and added some documentation. Also updated description and added tags.

01 Nov 2008

Added more documentation. pre-allocated two arrays for increased speed.

Tag Activity for this File
Tag Applied By Date/Time
domino tiles random matrix ben payne 22 Oct 2008 10:23:49
domino ben payne 22 Oct 2008 10:23:49
matrix ben payne 22 Oct 2008 10:23:49
general ben payne 22 Oct 2008 10:23:49
communications ben payne 01 Nov 2008 02:58:01
measurement ben payne 01 Nov 2008 02:58:01
mathematics ben payne 01 Nov 2008 02:58:01
simulation ben payne 01 Nov 2008 02:58:01
mathematics Cristina McIntire 07 Nov 2008 12:57:20
communications Cristina McIntire 10 Nov 2008 13:06:18
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com