Friday, 9 August 2013

Stopping a function, Arrays, and Integer Check

Stopping a function, Arrays, and Integer Check

So I made code for creating 5 ships in a battleship game. I succesfully
made some code that layed out all the players ships. It had no bugs. I had
the player write out the cords for where he wanted to position his ship.
It would then write in the ship position to the corresponding part in a
two dimensional array which was the game map.. Of course the cords had to
be integers or it would crash and burn.
So I then made something to check if the coordinate was a integer before
doing anything else. If it wasn't it would restart the function making so
that the rest of the function wouldn't run. If you write in the numbers
correctly there are no problems. The problem is that if you don't write in
a number the function does restart but the function or some part of it
must still be running because a ship gets written to the array for no
reason. The cords haven't even been specified for it so I have no clue how
this can be possible.
Here is the code I made for checking if its an integer and restarting the
function.
userYTest = parseInt(prompt("Horizontal Coordinate position for the first
unit of a ship"));
userXTest = parseInt(prompt("Vertical Coordinate position for the first
unit of a ship"));
if(userXTest % 1 === 0 && userYTest % 1 === 0) {
userY = userYTest-1;
userX = userXTest-1;
direction = prompt("Now choose the direction you want the rest
of your ship to face. You may use the words left, right up,
or down.").toLowerCase();
}
else{
window.alert("You must enter a number between one and ten for
the two coordinates.");
ship();
//ship is the name of the function
}
Here is all the code.
//These are all the different game boards you need to keep track of. Two
possible values in each position 1 or 0
var user =
[[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0]];
var cpu =
[[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,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,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0]];
// These are just used to set left the game board.
// I counted 10 by 10 - it should be 10 by 10
var userY = 0;
var userX = 0;
var cpuX = 0;
var cpuY = 0;
var cpuDir = 0;
var cpuWork = false;
var direction = "";
var isThere = false;
var i=0;
var userXTest;
var userYTest;
// In battleship, there is 1x5 length ship, 1x4 length ship, 2 1x3 length
ship, and 1x2 length ship. down now it checks how many units are covered
to see if you have all the ships. Later we need to add so they ships are
the down shape
//User will add their ships here one by one. If you can think of a better
have a go at it!
for(i=0;i<4;i++){
if (i===0){
window.alert("We will be placing your 1 by 5 length ship. Take
note that you are playing on a 10 by 10 board.");
ship();
}
if (i===1){
window.alert("We will be placing your 1 by 4 length ship. Take
note that you are playing on a 10 by 10 board.");
ship();
}
if (i===2){
window.alert("We will be placing your two 1 by 3 length ships.
Take note that you are playing on a 10 by 10 board.");
ship();
ship();
}
if (i===3){
window.alert("We will be placing your 1 by 2 length ship. Take
note that you are playing on a 10 by 10 board.");
ship();
}
function ship(){
userYTest = parseInt(prompt("Horizontal Coordinate position for
the first unit of a ship"));
userXTest = parseInt(prompt("Vertical Coordinate position for the
first unit of a ship"));
if(userXTest % 1 === 0 && userYTest % 1 === 0) {
userY = userYTest-1;
userX = userXTest-1;
direction = prompt("Now choose the direction you want the rest
of your ship to face. You may use the words left, right up,
or down.").toLowerCase();
}
else{
window.alert("You must enter a number between one and ten for
the two coordinates.");
ship();
}
//Making sure the ship will fit and nothing is already there!
if ((userY+4-i)>9 && direction=== "down"){
window.alert("You are too close to the down edge of the board to
do that. Restarting...");
ship();
}
else if ((userY-4-i)<0 && direction=== "up"){
window.alert("You are too close to the up edge of the board to do
that. Restarting...");
ship();
}
else if ((userX+4-i)>9 && direction=== "right"){
window.alert("You are too close to the bottom edge of the board
to do that. Restarting...");
ship();
}
else if ((userX-4-i)<0 && direction=== "left"){
window.alert("You are too close to the top edge of the board to
do that. Restarting...");
ship();
}
else if (user[userY][userX] === 1) {
window.alert("Coordinate already used. Please try again");
ship();
}
else if (user[userY][userX] === null || user[userY][userX] === ""){
window.alert("That coordinate isn't on the board.
Restarting...");
ship();
}
else if(direction ==="left" || direction ==="right" || direction
==="up" || direction ==="down") {
for(var a=1; a<5-i; a++){
if(direction=== "down"){
if(user[userY+a][userX] === 1){
window.alert("Can't place your ship in that
direction, another ship is in your way.");
isThere=true;
}
}
if(direction=== "up"){
if(user[userY-a][userX] === 1){
window.alert("Can't place your ship in that
direction, another ship is in your way.");
isThere=true;
}
}
if(direction=== "right"){
if(user[userY][userX+a] === 1 ){
window.alert("Can't place your ship in that
direction, another ship is in your way.");
isThere=true;
}
}
if(direction=== "left"){
if(user[userY][userX-a] === 1){
window.alert("Can't place your ship in that
direction, another ship is in your way.");
isThere=true;
}
}
if(isThere===true){
isThere = false;
ship();
return false;
}
else{
user[userY][userX] = 1;
}
}
}
else{
window.alert("Sorry but you didn't type in the direction you
wanted your ship to go correctly. Restarting...");
ship();
}
// Building Ship 1x5
for(var b=1; b<5-i; b++){
if (direction==="left"){
user[userY][userX-b] =1;
}
else if (direction==="right"){
user[userY][userX+b] =1;
}
else if (direction==="up"){
user[userY-b][userX] =1;
}
else if (direction==="down"){
user[userY+b][userX] =1;
}
}
}
}
console.log(user);

No comments:

Post a Comment