All of the above answers are great. Here are some tips I would do:
Instead of a two-dimensional char array, I would create a custom object, such as Space , and define a two-dimensional array of spaces (for example, Cosmos [] []). There are several reasons for this:
- You can define a space in various ways (and not just with one character). For example, Space [i] [j] .hasTreasure () may return a boolean so that you know if you have found this treasure.
- If you want to add functionality later, it's as simple as adding an attribute to your Space class. Again, you are not limited to one character here.
More about your question about traffic, I would also recommend a few things. Like the redneckjedi suggestion of the CheckIfMoveIsValid () method, I would pass the grid and direction of movement as parameters and return a boolean value. To make sure you have no problems with ArrayIndexOutOfBounds, I would also suggest adding a row / column of walls on each side. I would expand the grid to 12x12 and put a strip of blocks like obstacles outside. This ensures that you cannot go beyond the grid, since hitting the wall will always return โfalseโ to the actual move.
source share