My code passed validation so I can't attach any code. I'm less concerned about validation than about the code making sense. I have added some simple println statements to get the two cells that are set. Here is one example with a screenshot of the board:
When I test the values I get:
gameField[1][0] = 2
gameField[3][3] = 2
This seems wrong. It should be gameField[0][1] that is = 2. X is for counting horizontal, y is for counting vertical, that's a standard. I've done programming in other languages and never done this grid inversion thing, and I don't understand why we're doing that at all.
Why do we invert gameField[x][y] to gameField[y][x]? It produces wrong cell reference
Under discussion
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
26 July 2021, 16:34
It is because it is not a graph, its a multi-dimensional array (an array that comprises of arrays).
- gameField[0] is the first sub-array of the gameField array
- gameField[1] is the second sub-array of the array
- gameField[2] is the third sub-array of the array
etc.
You then can access each index of the sub array with the next box:
- gameField[1][0] would be the first index of the second sub-array. Or if it were a graph then the coordinate (0,1)
- gameField[3][1] would be the second index of the fourth sub-array. Or if it were a graph then the coordinate (1,3)
- gameField[2][3] would be the fourth index of the third sub-array. Or if it were a graph then the coordinate (3,2)
0
Justin Smith
26 July 2021, 17:17
I understand what you are saying, but it seems like this approach makes the code unnecessarily unintuitive. It works just fine if you use gameField[x][y], other than validation, and you get cell references that match the grid locations rather than having them inverted. 20 years ago I wrote code that would read and edit pixel data on images loaded. The images were converted to 2D arrays that stored the color value of each pixel. I never had to invert the array to [y][x]. The pixel[x][y] reference was always a direct correlation to the pixel at the x,y coordinate on the image. It was easy. I'm just having difficulty accepting that the "right" way to do this is more confusing and less intuitive, especially when the "wrong" way works.
+1
Guadalupe Gagnon
26 July 2021, 17:38
All the languages that I know access multidimensional arrays (MDA) in the way I describe, as that is how the standard implemented of an array. Changing the implementation to make it work like a grid would be backwards in relation to how arrays work. Think of an array of any other type of data (ints, Lists, Strings, Maps, etc):
array[0] would be the first value
array[1] would be the second value
Consider this code:
This prints out 6, which makes perfect sense. It grabbed the array at index 1, then the value in that array at index 2. This is the same as simply outputting field[1][2].
However I don't know all languages and don't what language you were using. In that language it could have been implemented differently; or it didn't convert the image to a standard MDA but instead a class specifically designed to work as a grid. That could have been a language specific class or a user created class to access an MDA as a grid with (x,y) coordinates .
0
Justin Smith
26 July 2021, 19:38
It is mostly with PHP, some C. I don't mean that they don't use multi-dimensional arrays the same way, just that my way of working with them has always worked (it works with Java, too, I was able to get the game to move and combine pieces as the user would expect without inverting the array, but I have to change it back to validate).
I guess I am just having difficulty with this because I've always interpreted multi-dimensional arrays as like a coordinate system, and have been using them that way for 20+ years, and now I have to unlearn that and it's really difficult. It's like riding a bike your whole life and suddenly you are told you have to pedal backwards to make the bike go forwards. It's very hard to adjust. I will try.
+1