Advertisement
Vita_Harvey

BetterBattleship001

Jan 15th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.  
  5.     </head>
  6. <body>
  7.  
  8. <h1>Battleship Game</h1>
  9. <p id="part1">Part 1</p>
  10.     <p id="part2"></p>
  11.     <p id="part3"></p>
  12.     <p id="part4"><button type=button onclick=getUpdateDisplay()>GUESS</button></p>
  13.     <p id="part5">Status</p>
  14.     <p id="part6"></p>
  15.     <p id="part7"></p>
  16.  
  17. <script>
  18.  
  19.     var gameArray=[];
  20.     var empty = 0;
  21.     var hit = 1;
  22.     var miss = 2;
  23.     var ship = 3;
  24.     var guess1 = 0;
  25.     var countTries = 0;
  26.     var countMisses = 0;
  27.     var countHits = 0;
  28.  
  29.     initGame(10);
  30.     dispgameArray();
  31.     shipLocator();
  32.     dispgameArray();
  33.     dispBoard();
  34.     //guess1 = getGuess();
  35.     //updateGameArray(guess1);
  36.     //dispgameArray();
  37.  
  38.  
  39.  
  40.  
  41. //document.getElementById("part1").innerHTML = "XX XX XX XX XX XX XX XX XX XX<br>";
  42. document.getElementById("part2").innerHTML = "<img src='water.png' alt='1' height='100' width='100'>";
  43.  
  44.     function initGame(n){
  45.         alert("initGame");
  46.         var i=0;
  47.         for (i=0; i<n; i++){
  48.             gameArray[i] = empty;
  49.         }
  50.     }
  51.  
  52.     // Can use to determine whether a Ship is vertical or horizontally placed;
  53.     function isOdd(num) {
  54.         if (num%2 === 0){
  55.             return true
  56.         } else {
  57.             return false
  58.         }
  59.     }
  60.  
  61.     function dispgameArray(){
  62.         // var i = 0; commented out as unnecessary;
  63.         var html = "";
  64.         alert(gameArray.length);
  65.         for (i=0; i < gameArray.length; i++){
  66.             if (gameArray[i] == empty) {
  67.                 if (isOdd(i)){
  68.                     html = html + "X"
  69.                 }
  70.                 else
  71.                 {html = html + "X "}
  72.             }
  73.             if (gameArray[i] == ship){
  74.                 if (isOdd(i)){
  75.                     html = html + "&"
  76.                 }
  77.                 else
  78.                 {html = html + "&  "}
  79.             }
  80.             if (gameArray[i] == miss){
  81.                 if (isOdd(i)){
  82.                     html = html + "M"
  83.                 }
  84.                 else
  85.                 {html = html + "M  "}
  86.             }
  87.             if (gameArray[i] == hit){
  88.                 if (isOdd(i)){
  89.                     html = html + "*"
  90.                 }
  91.                 else
  92.                 {html = html + "*  "}
  93.             }
  94.         }
  95.         document.getElementById("part3").innerHTML = html;
  96.     }
  97.  
  98.     // hack this to make useful for different ships;
  99.     // this appears to place the ships;
  100.     // move to inside Ship constructor;
  101.     function shipLocator(){
  102.         var location = 0;
  103.         alert(location);
  104.         location = Math.floor(Math.random()*18);
  105.         alert("shipLocator() -" + location);
  106.         gameArray[location] = ship;
  107.         gameArray[location+1] = ship;
  108.         gameArray[location+2] = ship;
  109.     }
  110.  
  111.     // get user's X-coordinate guess;
  112.     function getGuessX(){
  113.         alert("getGuessX");
  114.         var guessIsBad = true;
  115.         while (guessIsBad) {
  116.             var guessX =  window.prompt("Enter your guess", "Number from 0 to 9");
  117.             alert(guessX0)
  118.             alert(parseInt(guessX));
  119.             if ((parseInt(guessX) >= 0 ) && (parseInt(guessX) <= 9)){alert("in  If -" + guessIsBad);
  120.                         return parseInt(guessX)};
  121.             alert(guessIsBad);
  122.         }
  123.         return parseInt(guessX);
  124.     }
  125.  
  126.     // get user's Y-coordinate guess;
  127.     function getGuessY(){
  128.          alert("getGuessY");
  129.          var guessIsBad = true;
  130.          while (guessIsBad) {
  131.              var guessY =  window.prompt("Enter your guess", "Number from 0 to 9");
  132.              alert(guessY)
  133.              alert(parseInt(guessY));
  134.              if ((parseInt(guessY) >= 0 ) && (parseInt(guessY) <= 9)){alert("in     If -" + guessIsBad);
  135.                          return parseInt(guessY)};
  136.              alert(guessIsBad);
  137.          }
  138.          return parseInt(guessY);
  139.      }
  140.  
  141.  
  142.     function updateGameArray(guess2){
  143.         alert("updateGameArray - " + guess2);
  144.         //alert("updateGameArray - " + gameArray[guess2]);
  145.  
  146.         if (gameArray[guess2] == empty){
  147.             gameArray[guess2] = miss;
  148.             countMisses++;
  149.             countTries++;
  150.         }else if (gameArray[guess2] == ship){
  151.             gameArray[guess2] = hit;
  152.             countHits++;
  153.         }else if (gameArray[guess2] == miss) {
  154.             gameArray[guess2] = miss;
  155.             countTries++;
  156.         }else if (gameArray[guess2] == hit) {
  157.             gameArray[guess2] = hit;
  158.             countTries++;
  159.         }
  160.     }
  161.  
  162.  
  163.     function getUpdateDisplay(){
  164.         var guess1 = 0;
  165.         guess1 = getGuess();
  166.  
  167.     //alert("after getGuess -" + guess1);
  168.     updateGameArray(guess1);
  169.     dispgameArray();
  170.     dispBoard();
  171.     }
  172.  
  173.     function dispBoard(){
  174.         var i = 0;
  175.         var html = "";
  176.        // alert("disp Board");
  177.         for (i=0; i < gameArray.length; i++){
  178.             //alert("gameArray -" + gameArray[i] + "i - " + i);
  179.             if (gameArray[i] == empty) {
  180.                 if (isOdd(i)){
  181.                     html = html + "<a href=dispBoard()><img src='water.png' alt='1' height='25' width='25'></a>"
  182.                 }
  183.                 else
  184.                 {html = html + "<img src='water.png' alt='1' height='25' width='25'> "}
  185.             }
  186.             if (gameArray[i] == ship){
  187.                 if (isOdd(i)){
  188.                     html = html + "<img src='water.png' alt='1' height='25' width='25'>"
  189.                 }
  190.                 else
  191.                 {html = html + "<img src='water.png' alt='1' height='25' width='25'>  "}
  192.             }
  193.             if (gameArray[i] == miss){
  194.                 if (isOdd(i)){
  195.                     html = html + "<img src='miss.png' alt='1' height='25' width='25'>  "
  196.                 }
  197.                 else
  198.                 {html = html + "<img src='miss.png' alt='1' height='25' width='25'>  "}
  199.             }
  200.             if (gameArray[i] == hit){
  201.                 if (isOdd(i)){
  202.                     html = html + "<img src='hit.png' alt='1' height='25' width='25'>"
  203.                 }
  204.                 else
  205.                 {html = html + "<img src='hit.png' alt='1' height='25' width='25'>  "}
  206.             }
  207.         }
  208.         alert(html);
  209.         document.getElementById("part1").innerHTML = html;
  210.         document.getElementById("part5").innerHTML = "Tries - " + countTries + " Misses - " + countMisses + " Hits = " + countHits;
  211.         if (countTries > 8) { document.getElementById("part6").innerHTML ="The ship ESCAPED!"}
  212.         if (countHits >= 3) { document.getElementById("part7").innerHTML = "You SUNK by Battleship!"}
  213.     }
  214.  
  215.  
  216.  
  217. </script>
  218.  
  219. <!-- The core Firebase JS SDK is always required and must be listed first -->
  220. <script src="/__/firebase/7.6.1/firebase-app.js"></script>
  221.  
  222. <!-- TODO: Add SDKs for Firebase products that you want to use
  223.      https://firebase.google.com/docs/web/setup#available-libraries -->
  224. <script src="/__/firebase/7.6.1/firebase-analytics.js"></script>
  225.  
  226. <!-- Initialize Firebase -->
  227. <script src="/__/firebase/init.js"></script>
  228.  
  229.  
  230. </body>
  231. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement