javascript - Stopping a function, Arrays, and Integer Check -
so made code creating 5 ships in battleship game. succesfully made code layed out players ships. had no bugs. had player write out cords wanted position ship. write in ship position corresponding part in 2 dimensional array game map.. of course cords had integers or crash , burn.
so made check if coordinate integer before doing else. if wasn't restart function making rest of function wouldn't run. if write in numbers correctly there no problems. problem if don't write in number function restart function or part of must still running because ship gets written array no reason. cords haven't been specified have no clue how can possible.
here code made checking if integer , restarting function.
userytest = parseint(prompt("horizontal coordinate position first unit of ship")); userxtest = parseint(prompt("vertical coordinate position first unit of ship")); if(userxtest % 1 === 0 && userytest % 1 === 0) { usery = userytest-1; userx = userxtest-1; direction = prompt("now choose direction want rest of ship face. may use words left, right up, or down.").tolowercase(); } else{ window.alert("you must enter number between 1 , ten 2 coordinates."); ship(); //ship name of function }
here code.
//these different game boards need keep track of. 2 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,0,0,0,0,0,0,0,0,0]]; var userguessed = [[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 userhit = [[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 cpuguessed = [[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 cpuhit = [[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 clearboard = [[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]]; // these used set left game board. // counted 10 10 - should 10 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 1x5 length ship, 1x4 length ship, 2 1x3 length ship, , 1x2 length ship. down checks how many units covered see if have ships. later need add ships down shape //user add ships here 1 one. if can think of better have go @ it! for(i=0;i<4;i++){ if (i===0){ window.alert("we placing 1 5 length ship. take note playing on 10 10 board."); ship(); } if (i===1){ window.alert("we placing 1 4 length ship. take note playing on 10 10 board."); ship(); } if (i===2){ window.alert("we placing 2 1 3 length ships. take note playing on 10 10 board."); ship(); ship(); } if (i===3){ window.alert("we placing 1 2 length ship. take note playing on 10 10 board."); ship(); } function ship(){ userytest = parseint(prompt("horizontal coordinate position first unit of ship")); userxtest = parseint(prompt("vertical coordinate position first unit of ship")); if(userxtest % 1 === 0 && userytest % 1 === 0) { usery = userytest-1; userx = userxtest-1; direction = prompt("now choose direction want rest of ship face. may use words left, right up, or down.").tolowercase(); } else{ window.alert("you must enter number between 1 , ten 2 coordinates."); ship(); } //making sure ship fit , nothing there! if ((usery+4-i)>9 && direction=== "down"){ window.alert("you close down edge of board that. restarting..."); ship(); } else if ((usery-4-i)<0 && direction=== "up"){ window.alert("you close edge of board that. restarting..."); ship(); } else if ((userx+4-i)>9 && direction=== "right"){ window.alert("you close bottom edge of board that. restarting..."); ship(); } else if ((userx-4-i)<0 && direction=== "left"){ window.alert("you close top edge of board that. restarting..."); ship(); } else if (user[usery][userx] === 1) { window.alert("coordinate used. please try again"); ship(); } else if (user[usery][userx] === null || user[usery][userx] === ""){ window.alert("that coordinate isn't on 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 ship in direction, ship in way."); isthere=true; } } if(direction=== "up"){ if(user[usery-a][userx] === 1){ window.alert("can't place ship in direction, ship in way."); isthere=true; } } if(direction=== "right"){ if(user[usery][userx+a] === 1 ){ window.alert("can't place ship in direction, ship in way."); isthere=true; } } if(direction=== "left"){ if(user[usery][userx-a] === 1){ window.alert("can't place ship in direction, ship in way."); isthere=true; } } if(isthere===true){ isthere = false; ship(); return false; } else{ user[usery][userx] = 1; } } } else{ window.alert("sorry didn't type in direction wanted ship 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);
first, understand calling function within not stop running original function. try this (jsfiddle) see how works:
var = 0; function askdogsname() { var dogsname = prompt("what dog's name?"); if (dogsname != "rover") { askdogsname(); } i++; document.body.innerhtml += "i = " + + "; dog's name: " + dogsname + '<br />'; } askdogsname();
after new recursion of function has completed, original 1 carries on left off; not 'restart'. not way of responding user input not valid, because using global variables. each recursion of function can alter variables in way can become difficult predict, before returning control 'parent' (the recursion of function called it).
what can instead use return values check whether correct input has been given or not:
function ship() { var c; while (!c) { c = getvalidcoords(); } x = c[0]; y = c[1]; // make ship @ x, y } function getvalidcoords() { y = parseint(prompt("horizontal coordinate position first unit of ship")); x = parseint(prompt("vertical coordinate position first unit of ship")); // conduct various tests on x , y if (testsfail) { return false; } return [x, y]; }
Comments
Post a Comment