4

So our teacher gave us the "Game Of Life" project. I was really proud of my project until I ran it and got the error "Uncaught TypeError: Cannot read property '1' of undefined". For those who don't know what Game Of Life is, it's a "game" where you have a matrix (here an 8x8 matrix) and some rules to define if each "person" (the numbers in the matrix) gets to live or not. 1 means alive and 0 means dead. I'll attach the whole project for reference, but here's the part with the error:

function process(matrix) { //creates the next generation var neighbors = 0, position = '', nextgen = new Array(8); //the matrix has 8 rows for (i = 1; i <= 8; i++) { nextgen[i] = new Array(8); for (j = 1; j <= 8; j++) { neighbors = neighbors(matrix, i, j); //Game Rules if (neighbors < 2) {nextgen[i][j] = 0; } else if (neighbors == 2) {nextgen[i][j] = 1; } else if (neighbors <= 3) {nextgen[i][j] = nextgen[i][j]; } else {nextgen[i][j] = 0;} } } return nextgen; } 

The error appears at if (neighbors < 2) {nextgen[i][j] = 0;}. Anyone have any ideas as to why? I defined nextgen[1][1] when I defined 8 arrays for nextgen[1], right?

Here's the whole project. the names and everything are in german, so please keep that in mind.

function berechne_nachbarn(matrixb, x, y) { //berechnet die Anzahl der Nachbarn fuer eine Zelle var zelle = 0, nachbarn = 0, oben = 0, unten = 0, links = 0, rechts = 0; //loesung fuer die Randzellen	if (x == 1) {oben = 1} else {oben = x - 1}	if (y == 1) {links = 1} else {links = y - 1}	if (x == 8) {unten = 8} else {unten = x + 1}	if (y == 8) {rechts = 8} else {rechts = y - 1} for (i = oben; i <= unten; i++) {	for (j = links; j <= rechts; j++) {	zelle = parseInt (matrixb[i][j]);	//loesung fuer leere Zellen	if (isNaN(zelle)) {zelle = 0;}	nachbarn = nachbarn + zelle;	}//ende for j }//ende for i //die Zelle selbst ist nicht sein eigener Nachbar!	nachbarn = nachbarn - matrixb[x][y];	return nachbarn; }//ende berechne_nachbarn; function eingabe() {//erstellt Matrix var spalte = 0, zeile = 0, ort = '' zelle = 0;	brett = new Array(8); //8 Zeilen	for (i = 1; i <= 8; i++) {	brett[i] = new Array(8); //8 Spalten	for (j = 1; j <= 8; j++) {	ort = i + 'xx' + j;	zelle = parseInt(document.formular.elements[ort].value);	if (isNaN(zelle)) {zelle = 0;}	brett[i][j] = zelle;	}//ende for j	}//ende for i	return brett; }//ende eingabe function verarbeitung(matrix) {//Spielregeln und Verarbeitung var nachbarn = 0, ort = '',	nextgen = new Array(8); //neue Matrix fuer neue Generation	for (i = 1; i <= 8; i++) {	nextgen[i] = new Array(8);	for (j = 1; j <= 8; j++) {	nachbarn = berechne_nachbarn(matrix, i, j);	//Spielregeln	if (nachbarn < 2) {nextgen[i][j] = 0;	} else if (nachbarn == 2) {nextgen[i][j] = 1;	} else if (nachbarn <= 3) {nextgen[i][j] = nextgen[i][j];	} else {nextgen[i][j] = 0;}	}//ende for j	}//ende for i	return nextgen; }//ende verarbeitung function ausgabe(matrix) {//aendert die Werte im Formular var ort = '';	for (i = 1; i <= 8; i++) {	for (j = 1; j <= 8; j++) {	ort = i + 'xx' + j;	document.formular.elements[ort].value = matrix[i][j];	}//ende for j	}//ende for i }//ende ausgabe
<body> <h1> Game Of Life</h1> <form name = "formular">	<input name = "1xx1" size = "1">	<input name = "1xx2" size = "1">	<input name = "1xx3" size = "1">	<input name = "1xx4" size = "1">	<input name = "1xx5" size = "1">	<input name = "1xx6" size = "1">	<input name = "1xx7" size = "1">	<input name = "1xx8" size = "1"> <br>	<input name = "2xx1" size = "1">	<input name = "2xx2" size = "1">	<input name = "2xx3" size = "1">	<input name = "2xx4" size = "1">	<input name = "2xx5" size = "1">	<input name = "2xx6" size = "1">	<input name = "2xx7" size = "1">	<input name = "2xx8" size = "1"> <br>	<input name = "3xx1" size = "1">	<input name = "3xx2" size = "1">	<input name = "3xx3" size = "1">	<input name = "3xx4" size = "1">	<input name = "3xx5" size = "1">	<input name = "3xx6" size = "1">	<input name = "3xx7" size = "1">	<input name = "3xx8" size = "1"> <br>	<input name = "4xx1" size = "1">	<input name = "4xx2" size = "1">	<input name = "4xx3" size = "1">	<input name = "4xx4" size = "1">	<input name = "4xx5" size = "1">	<input name = "4xx6" size = "1">	<input name = "4xx7" size = "1">	<input name = "4xx8" size = "1"> <br>	<input name = "5xx1" size = "1">	<input name = "5xx2" size = "1">	<input name = "5xx3" size = "1">	<input name = "5xx4" size = "1">	<input name = "5xx5" size = "1">	<input name = "5xx6" size = "1">	<input name = "5xx7" size = "1">	<input name = "5xx8" size = "1"> <br>	<input name = "6xx1" size = "1">	<input name = "6xx2" size = "1">	<input name = "6xx3" size = "1">	<input name = "6xx4" size = "1">	<input name = "6xx5" size = "1">	<input name = "6xx6" size = "1">	<input name = "6xx7" size = "1">	<input name = "6xx8" size = "1"> <br>	<input name = "7xx1" size = "1">	<input name = "7xx2" size = "1">	<input name = "7xx3" size = "1">	<input name = "7xx4" size = "1">	<input name = "7xx5" size = "1">	<input name = "7xx6" size = "1">	<input name = "7xx7" size = "1">	<input name = "7xx8" size = "1"> <br>	<input name = "8xx1" size = "1">	<input name = "8xx2" size = "1">	<input name = "8xx3" size = "1">	<input name = "8xx4" size = "1">	<input name = "8xx5" size = "1">	<input name = "8xx6" size = "1">	<input name = "8xx7" size = "1">	<input name = "8xx8" size = "1"> <br> <br>	<br>	<button type = "button" onClick = "{jetzt = eingabe(); nextgen = verarbeitung(jetzt); ausgabe(nextgen);}"> Next Generation </button> </form> </body>

2
  • Did you manage to solve the issue? Did my answer helped you? Commented Dec 17, 2018 at 14:40
  • yes, I did solve the issue. There were two problems. One, in countNeighbors, I had maxY = y -1 instead of y +1, which caused the weird property '-1' error. Another problem was in input. It gave me an error and said I hadn't defined the matrix. So, before the Game Rules part, I just gave the value 0 to every cell of the board, and then looked at the form and replaced the values with the ones the user had typed in Commented Dec 19, 2018 at 22:04

1 Answer 1

1

I found the problem guys. It's in the berechne_nachbarn function, where the program counts the neighbors of each cell on the game board. More specifically in this part:

if (y == 8) {rechts = 8} else {rechts = y - 1} 

y - 1 should have been y + 1.

Ps. the program works now and I'm so proud of it ^_^

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.