因此,我们的老师给了我们“生命游戏”项目。在运行它并收到错误“未捕获的TypeError:无法读取未定义的属性'1'”之前,我为我的项目感到非常自豪。对于那些不知道什么是“人生游戏”的人,这是一个“游戏”,您拥有一个矩阵(此处为8x8矩阵),并定义了一些规则来定义每个“人”(矩阵中的数字)是否活着或不。 1表示存活,0表示死亡。我将附上整个项目以供参考,但这是出现错误的部分:
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;
}
错误出现在if (neighbors < 2) {nextgen[i][j] = 0;}
。有人对为什么有任何想法吗?我为nextgen[1][1]
定义了8个数组时定义了nextgen[1]
,对吗?
这是整个项目。名称和所有内容都是德语,因此请记住这一点。
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>
答案 0 :(得分:1)
我找到了问题的人。它位于berechne_nachbarn
函数中,该程序会计算游戏板上每个单元的邻居。在这部分中更具体:
if (y == 8) {rechts = 8} else {rechts = y - 1}
y - 1
应该是y + 1
。
Ps。该程序现在可以运行了,我为此感到骄傲^ _ ^