Hello folks.
I wanna try to make a simple grid based game, and currently with my 3×3 grid finding myself copying alooot of code. I’m simply wondering if there are huge solutions to compress repeating code I’m missing out on? see jsfiddle at bottom
For example:
if(cell1.innerHTML == <img src="/images/character.png">
|| cell2.innerHTML == <img src="/images/character.png">
|| cell3.innerHTML == <img src="/images/character.png">
){
Could I maybe write this as something like:
if (cell[1,2,3].innerHTML == <img src="/images/character.png">
){
The biggest issue is I wanna expand to 9×9 or larger–, which is 81+ cells. So I can have more room to work with and actually make a small game. But unless there is a smart way to compress the repeating code its simply going to be overwhelming to work with i think..
What I’ve tried to create so far is a 3×3 grid with a character in the middle and (shadow) on the adjacent tiles. To remove the shadow the character.png in the middle has to move to said cell to reveal whats underneath (havent added anything underneath any cell yet).
I’ve not found a smart way to move character from one cell to another either.
So currently I’ve landed on this repetetive cellNumber.innerHTML = “”; thing to clear out all cells.
https://jsfiddle.net/ykj9e2cs/
Any answers are welcome. Thx 🙂