TypeError: Cannot read property '#' of undefined In simple JavaScript Game -
i'm making small battleship game friend of mine. thought practice learned. have 2 dimensional array 10x10 filled zeros. change zeros ones in places our ships go. made function place 1x5 ship down. function prompts user 2 coordinates , assigns variables outside of function. gets coordinates of start point , asks direction player wants rest wrest of ship go. if there no obstacles ship should placed. if there function should restart. thing error. # replaced number in prompt asking vertical coordinate position.
here error. typeerror: cannot read property '#' of undefined in battleship game
here function
function firstship(){ window.alert("we placing 1 5 length ship. take note playing on 10 10 board."); userx = parseint(prompt("horizontal coordinate position first unit of ship")-1); usery = parseint(prompt("vertical coordinate position first unit of ship")-1); direction = prompt("now choose direction want rest of ship face. may use words up, down left, or right.").tolowercase(); //making sure ship fit , nothing there! if ((userx+4)>9 && direction=== "right"){ window.alert("you close right edge of board that. restarting..."); firstship(); } else if ((userx-4)<0 && direction=== "left"){ window.alert("you close left edge of board that. restarting..."); firstship(); } else if ((usery+4)>9 && direction=== "down"){ window.alert("you close bottom edge of board that. restarting..."); firstship(); } else if ((usery-4)<0 && direction=== "up"){ window.alert("you close top edge of board that. restarting..."); firstship(); } else if (user[userx][usery] === 1) { window.alert("coordinate used. please try again"); firstship(); } else if (user[userx][usery] === null || user[userx][usery] === ""){ window.alert("that coordinate isn't on board. restarting..."); firstship(); } else if(direction !=="up" || direction !=="down" || direction !=="left" || direction !=="right") { for(i=1; i<5; i++){ if(user[userx+i][usery] === 1 && direction=== "right"){ window.alert("can't place ship in direction, ship in way."); isthere=true; } if(user[userx-i][usery] === 1 && direction=== "left"){ window.alert("can't place ship in direction, ship in way."); isthere=true; } if(user[userx][usery+i] === 1 && direction=== "down"){ window.alert("can't place ship in direction, ship in way."); isthere=true; } if(user[userx][usery-i] === 1 && direction=== "up"){ window.alert("can't place ship in direction, ship in way."); isthere=true; } if(isthere===true){ isthere = false; firstship(); return false; } else{ user[userx][usery] = 1; } } } else{ window.alert("sorry didn't type in direction wanted ship go correctly. restarting..."); firstship(); } // building ship 1x5 for(i=1; i<4; i++){ if (direction==="up"){ user[userx][usery-i] =1; } else if (direction==="down"){ user[userx][usery+i] =1; } else if (direction==="left"){ user[userx-i][usery] =1; } else if (direction==="right"){ user[userx][usery+i] =1; } } } firstship(); //here new cpu creation. there may way shorten if statements, other that, set
this happening because using negative array index.
if try code here: http://plnkr.co/edit/g4i1qay6cou18xtk6qzi?p=preview
and enter 1, 1, right answers questions, see error on line 28 of script.js:
if(user[userx-i][usery] === 1 && direction=== "left"){
the error is:
uncaught typeerror: cannot read property '0' of undefined
what happens userx = 0 , = 1, evaluating user[-1][0]
. since user[-1] undefined, that's why you're seeing error.
Comments
Post a Comment