I’m back… So, I’m trying to recreate Conway’s Game of Life, using js, html, and css. I’m having issues with the js part, specifically with the method .hasAttribute, from my understanding, it returns a Boolean value, depending on if the element it’s targeting has a certain attribute; in my case, I want to target all the cells surrounding the main cell (the center one) who have the class “badMario”.
First, I want to clarify that my code for this game interpretation is sub-optimal, however I really just want to finish it now, and I’ll appreciate any kind of feedback and I would also like it if this feedback was chill; in any case, thanks.
Secondly, I’ll explain how the code (in theory) should function, specifically a little html and a lot js.
HTML: The HTML part is basically some instructions and an empty table without rows nor columns.
Javascript: The Javascript code is somewhat complicated, and again, it’s probably sub-optimal, but that’s how I would like to remain per se.
So first it makes the rows of the table, and 20 tds per row with unique ids. Then it assigns them all the “goodMario” class and when you hit the button “iniciar conflicto bélico” it should do the following:
- Assign the class “badMario” on a coin toss for each cell.
- Check how many cells surrounding the priority cell (center one) exist, starting on cell 1 (or cell 0 in this case).
- Check how many cells surrounding the priority cell that exist, have the “badMario” class.
- Add 1 to the incremental variable that counts how many cells have the “badMario” class.
- Depending on the value of the incremental variable it will:
a. With 2 or 3 alive cells: Keep the center cell alive.
b. With 4 cells: Make a dead cell alive.
c. With more than 4 cells: Kill the alive cell (due to overpopulation)
- Repeat.
Now, the part that is confusing to me is basically any part that uses the js method .hasAttribute that for some reason is understanding as a function of sorts. For example, there’s the following code block:
if(campo1 == null){
console.log("feo1");
}
else
console.log(campo1.hasAttribute("class"));
So, from line 157 to 203 or so, it’s purely for understanding how it’s interpreting the cells and what not, it’s not fundamental to the functioning of the game. What I intend for it to do here is to put in the console whether the space 1 (“campo1”) was detected in the first place (if not, then write “feo1”), and if so to check if it has the class “badMario” (I only put class there to try and learn the syntax, but to be honest I have no idea how to use it). And that’s pretty much my issue, I do believe that the logic with the surrounding cells is good enough for it to work, but if it isn’t I’ll manage to fix it eventually; but yeah, this issue with .hasAttribute has been rough and has me certainly dumbfounded.
here’s a codepen link to the whole code: https://codepen.io/Lucky737V/pen/rNRbVGv
Again, any help would be appreciated. In case of needing any translations from the code to English (referring to the Spanish segments and names)