1

I have a 2D matrix with 0s and 1s - respectively representing Water and Land. It is used to generate an animated gif with Perlin noise (moving water and waves clashing on shores...).

However I wish to refactor my code and use polymorphism in the following manner:

For each element in my matrix, based on the value, I wish to create a new WaterTile or LandTile (which will represent a 30x30 set of pixels that are either a mixture of blue for water, and some combinations of green/yellow/blue for land).

WaterTile and LandTile will be inherited from BaseTile (this is an abstract class), having just 2 vars (x, y for coordinates) and a draw() method (this method for the BaseTile class does nothing, its just there if it has to be defined).

Child classes will be overriding the draw() method.

Map will be a 2D array that whose elements will be WaterTiles and LandTiles.

After that, in my main method, I will have this (pseudocode for simplicity, i is index of row, j is index of element in that row):

foreach (row in matrix) {
     foreach (element in row) {
          if (matrix[i][j] == 0) map[i][j] == new WaterTile();
          else map[i][j] == new LandTile();
      }
 }

After this I will need to invoke more methods for the LandTiles, and then have a new foreach loop, simply having

map[i][j].draw();//invoking draw method based on type

I know this can be done in other ways, but i wish to avoid having if statements that check for the type and call the draw method based on the type, and also practice clean code and learn something new.

If someone can provide me a simple example, ive looked at some already but havent found what I want.

6
  • What is the question? You need a tutorial on javascript inheritance? Commented Apr 29, 2017 at 15:53
  • Edited the Q. A simple example will do, ive found some but they were not very helpful. Commented Apr 29, 2017 at 16:03
  • im looking for a conventional way to do this in node (3 files for the 3 Tile classes, with exports), examples I found were either a bit too complex (and confusing), or for plain javascript Commented Apr 29, 2017 at 16:10
  • Problem is, you have to be more specific. There are thousands of tutorials of javascript inheritance. Plus, since this is javascript, you don't even need to inherit anything, javascript duck types objects, which means that as long as there is a draw method in an object instance, you can call it without any inheritance at all. The question at the moment is too broad then, there are too many good answers. I am voting to close it. Commented Apr 29, 2017 at 19:52
  • any chance of undoing this? ive been away from SO for quite a while, just tried to ask a new question and I see im blocked from asking questions... (getting the reached your question limit thing). Commented Apr 29, 2017 at 21:05

1 Answer 1

1

Put the different actions inside the if statement you already have.

Or, just call the draw method, and let each instance do what it is does with draw.

Or encapsulate the if inside a function and use that.

Sign up to request clarification or add additional context in comments.

1 Comment

Im aware I can do 1), but I have more functionality between the 2 for loops, so thats not an option. 2) is what I wanted to do, was just wondering if its "proper" and doable like that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.