![]() ![]() I suspect that in a previous version of the code I had the cell doing more work, keeping track of its neighbors or some such. What's that class Cell supposed to be doing? All it does is store a reference to an object which is either null or a character. This brings me to a more fundamental problem with the code above. In practice this is not really a problem since we seldom have many instances of a class to begin with. There is some memory overhead in going class-free, since objects no longer share references to their methods, instead each storing their own methods. Essentially, we can avoid ever having to deal with this and all its oddities by avoiding the class syntax all together. For new projects I have adopted the "class free" style I learned from Douglas Crockford's book. While I like the object-oriented style, I do not like the use of Javascript classes here. Still, we could get it down to none by making this its own separate module. This one is not too bad due to its size, only creating 6 global objects. For another, we are filling up the global namespace. For one thing, it could lead to problems down the line if the program were to grow. That's not too unwieldy for a script of this size, but it is still not the way I would do it now. That makes it simple to follow the flow of the program and predict its state. The game board is only generated in one spot, and a new object is made every time a board is generated. It makes sense to have a class for the game board and the ships, since there is a natural flow of these units going back and forth, asking each other questions. I like the functional and object-oriented qualities of this program. getElementById ( "input_form" ) function generateBoard () Let's have a look at my old code and see what nice qualities it has, lest we become overcritical. For now, Javascript has a strong advantage. Perhaps this will change soon, as there are several attempts in the works to make Python available in the browser. For most people, the benefit of having your code run anywhere outweighs the cost of having to deal with Javascript's peculiarities. There are exceptions, of course - if you want to do data science you should learn Python, if you want to program Arduino you should learn C, et cetera. This is why I think Javascript is a good first language for many people to learn. The code is so portable that I was able to copy it directly here with no changes, and it still works perfectly. ![]() Everyone with a modern web browser has a Javascript interpreter already installed and ready to go. One of the things which makes this particular tool so useful is that it is written in pure Javascript. Generated placements are guaranteed to be (pseudo) random, but not guaranteed to be good. Written by Alden Bradford on November 1-2, 2018. Here, "randomly selected" means that every possible arrangement is equally likely. It will give you a randomly selected arrangement of your ships on the board. This is a placement generator for Battleship. I feel compelled to at least comment on the bad parts of the code, if only to reassure myself that I have indeed grown. The tool I made works, and it works well. I know better than to jump back in to a completed project. For that reason, I think it's worth preserving. Last year I heard from an old friend that they had actually found the tool on a search, and that it had been useful. I made a quick tool to generate ship placements for me. I was also frequently playing Battleship over the phone with my then-girlfriend, now-wife. Four years ago, I was teaching myself Javascript. I am in the midst of refreshing my math department webpage, removing what is out-of-date and preserving what deserves to be preserved. If you cringe at the way you solved a problem before, that is a sure sign that you know better now than you did then. To prove this, all you need to do is look at something you made before and feel the cringe. Because you are always learning new things, pushing yourself outside of your comfort zone, it can seem like you have not mastered anything. Particularly in grad school it can feel like your life is standing still.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |