Wednesday, 7 August 2013

TypeError: Cannot read property '#' of undefined In Battleship Game

TypeError: Cannot read property '#' of undefined In Battleship Game

I'm making a small battleship game with a friend of mine. We thought it
would be good practice as we learned. We have a two dimensional array that
is 10X10 all filled with zeros. We will change those zeros to ones in the
places our ships will go. I made a function to place a 1X5 ship down. The
function prompts the user for two coordinates and assigns those to
variables that are outside of the function. It gets the coordinates of the
start point and asks for the direction the player wants the rest wrest of
the ship to go. If there are no obstacles the ship should be placed. If
there are some the function should restart. The thing is I get this error.
The # is replaced by the number in the prompt asking for the vertical
coordinate position.
Here is the error. TypeError: Cannot read property '#' of undefined In
Battleship Game
Here is the Function
function firstShip(){
window.alert("We will be placing your 1 by 5 length ship. Take not
that you are playing on a 10 by 10 board.");
userX = parseInt(prompt("Horizontal Coordinate position for the first
unit of a ship")-1);
userY = parseInt(prompt("Vertical Coordinate position for the first
unit of a ship")-1);
direction = prompt("Now choose the direction you want the rest of your
ship to face. You may use the words up, down left, or
right.").toLowerCase();
//Making sure the ship will fit and nothing is already there!
for(i=1; i<5; i++){
if(user[userX+i][userY] === 1 && direction=== "right"){
window.alert("Can't place your ship in that direction, another
ship is in your way.");
isThere=true;
}
if(user[userX-i][userY] === 1 && direction=== "left"){
window.alert("Can't place your ship in that direction, another
ship is in your way.");
isThere=true;
}
if(user[userX][userY+i] === 1 && direction=== "down"){
window.alert("Can't place your ship in that direction, another
ship is in your way.");
isThere=true;
}
if(user[userX][userY-i] === 1 && direction=== "up"){
window.alert("Can't place your ship in that direction, another
ship is in your way.");
isThere=true;
}
if(isThere===true){
isThere = false;
firstShip();
return false;
}
}
if ((user[userX+4][userY] === null || user[userX+4][userY] === "") &&
direction=== "right"){
window.alert("You are too close to the right edge of the board to do
that. Restarting...");
firstShip();
}
else if ((user[userX-4][userY] === null || user[userX-4][userY] ===
"") && direction=== "left"){
window.alert("You are too close to the left edge of the board to do
that. Restarting...");
firstShip();
}
else if ((user[userX][userY+4] === null || user[userX][userY+4] ===
"") && direction=== "down"){
window.alert("You are too close to the bottom edge of the board to do
that. Restarting...");
firstShip();
}
else if ((user[userX][userY-4] === null || user[userX][userY-4] ===
"") && direction=== "up"){
window.alert("You are too close to the top edge of the board to do
that. Restarting...");
firstShip();
}
else if (user[userX][userY] === 1) {
window.alert("Coordinate already used. Please try again");
firstShip();
}
else if (user[userX][userY] === null || user[userX][userY] === ""){
window.alert("That coordinate isn't on the board. Restarting...");
firstShip();
}
else if(direction !=="up" || direction !=="down" || direction
!=="left" || direction !=="right"){
window.alert("Sorry but you didn't type in the direction you
wanted your ship to go correctly. Restarting...");
firstShip();
}
else {
user[userX][userY] = 1;
}
// Building Ship 1x5
for(i=1; i<5; 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;
}
}
cpuWork = true;
}
firstShip();

No comments:

Post a Comment