"use strict";
/*
Author: Liam Butler
Assignment #: 4
Description: Q3 (BONUS): Battleship
*/
var fs = require("fs");
var gamemap = [];
var targetmap = [];
var missles = 30;
var hitCount = 0;
var shipssunk = [false, false, false, false, false];
function main() // <-- Don't change this line!
{ 
    //load the map.txt file
    function loadShipMap(targetmap) {
        var targetmap = fs.readFileSync(targetmap, "utf8");
        //split them into rows
        var rows = targetmap.split("\n");
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i].split(",");
            gamemap.push(row);
        }
    }
}
//initiate the targeting map
function initTargetingMap() {
    for (var i = 0; i < 10; i++) {
      var row = [];
      for (var j = 0; j < 10; j++) {
        row.push(" ");
      }
      targetmap.push(row);
    }
  }

  //updates the map with the amount of missles
  function updateTargetingMap(row, col, hit) {
    if (hit) {
      targetmap[row][col] = "X";
    } else {
      targetmap[row][col] = "-";
    }
  }

//makes sure the coords you hit are valid
function isValidCoordinates(row, col) {
    if (row < 0 || row >= 10 || col < 0 || col >= 10) {
      return false;
    }
    return true;
  }

//checks to see if theres a ship in the square you hit
function checkShipSunk(row, col) {
    if (gamemap[row][col] === 1) {
        gamemap[row][col] = "X"; //mark the ship as hit on the map
      for (var r = 0; r < gamemap.length; r++) {
        for (var c = 0; c < gamemap[r].length; c++) {
          if (gamemap[r][c] === 1) {
            return false; //at least one ship cell is still not hit, so the ship is not sunk yet
          }
        }
      }
      return true; //all ship cells are hit, so the ship sank
    }
    return false; //there is no ship at the given location
  }
  
//checks to see if a square has been hit already
function checkSquareHit(row, col) {
    if (targetmap[row][col] === "X") {
        return true;
    }
    return false;
}


loadShipMap(gamemap);
initTargetingMap();

//game loop
while (missles > 0) {
    console.log("your missiles remaining are " + missles);
    console.log("current game board:\n");
    console.log(gamemap);
    console.log(targetmap);
    //input for playing
    var row = parseInt(prompt("row to attack (0-9):"));
    var col = parseInt(prompt("column to attack (0-9):"));
    //tells you when you sunk a ship
    if (isValidCoordinates(row, col)) {
        if (checkShipSunk(row, col)) {
            console.log("You sunk a ship!");
            hitCount++;
            //if you sink 17 ships, you win
            if (hitCount == 17) {
                console.log("Congratulations, you've sunk all the ships and won the game!");
                return;
            }
        }
    }
}
// Do not change any of the code below!
if (require.main === module)
{
    main();
}
"use strict";
/*
Author: Liam Butler
Assignment #: 4
Description: Q3 (BONUS): Battleship
*/
var fs = require("fs");
var gamemap = [];
var targetmap = [];
var missles = 30;
var hitCount = 0;
var shipssunk = [false, false, false, false, false];
function main() // <-- Don't change this line!
{ 
    //load the map.txt file
    function loadShipMap(targetmap) {
        var targetmap = fs.readFileSync(targetmap, "utf8");
        //split them into rows
        var rows = targetmap.split("\n");
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i].split(",");
            gamemap.push(row);
        }
    }
}
//initiate the targeting map
function initTargetingMap() {
    for (var i = 0; i < 10; i++) {
      var row = [];
      for (var j = 0; j < 10; j++) {
        row.push(" ");
      }
      targetmap.push(row);
    }
  }

  //updates the map with the amount of missles
  function updateTargetingMap(row, col, hit) {
    if (hit) {
      targetmap[row][col] = "X";
    } else {
      targetmap[row][col] = "-";
    }
  }

//makes sure the coords you hit are valid
function isValidCoordinates(row, col) {
    if (row < 0 || row >= 10 || col < 0 || col >= 10) {
      return false;
    }
    return true;
  }

//checks to see if theres a ship in the square you hit
function checkShipSunk(row, col) {
    if (gamemap[row][col] === 1) {
        gamemap[row][col] = "X"; //mark the ship as hit on the map
      for (var r = 0; r < gamemap.length; r++) {
        for (var c = 0; c < gamemap[r].length; c++) {
          if (gamemap[r][c] === 1) {
            return false; //at least one ship cell is still not hit, so the ship is not sunk yet
          }
        }
      }
      return true; //all ship cells are hit, so the ship sank
    }
    return false; //there is no ship at the given location
  }
  
//checks to see if a square has been hit already
function checkSquareHit(row, col) {
    if (targetmap[row][col] === "X") {
        return true;
    }
    return false;
}


loadShipMap(gamemap);
initTargetingMap();

//game loop
while (missles > 0) {
    console.log("your missiles remaining are " + missles);
    console.log("current game board:\n");
    console.log(gamemap);
    console.log(targetmap);
    //input for playing
    var row = parseInt(prompt("row to attack (0-9):"));
    var col = parseInt(prompt("column to attack (0-9):"));
    //tells you when you sunk a ship
    if (isValidCoordinates(row, col)) {
        if (checkShipSunk(row, col)) {
            console.log("You sunk a ship!");
            hitCount++;
            //if you sink 17 ships, you win
            if (hitCount == 17) {
                console.log("Congratulations, you've sunk all the ships and won the game!");
                return;
            }
        }
    }
}
// Do not change any of the code below!
if (require.main === module)
{
    main();
}
"use strict";
/*
Author: Liam Butler
Assignment #: 4
Description: Q3 (BONUS): Battleship
*/
var fs = require("fs");
var gamemap = [];
var targetmap = [];
var missles = 30;
var hitCount = 0;
var shipssunk = [false, false, false, false, false];
function main() // <-- Don't change this line!
{ 
    //load the map.txt file
    function loadShipMap(targetmap) {
        var targetmap = fs.readFileSync(targetmap, "utf8");
        //split them into rows
        var rows = targetmap.split("\n");
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i].split(",");
            gamemap.push(row);
        }
    }
}
//initiate the targeting map
function initTargetingMap() {
    for (var i = 0; i < 10; i++) {
      var row = [];
      for (var j = 0; j < 10; j++) {
        row.push(" ");
      }
      targetmap.push(row);
    }
  }

  //updates the map with the amount of missles
  function updateTargetingMap(row, col, hit) {
    if (hit) {
      targetmap[row][col] = "X";
    } else {
      targetmap[row][col] = "-";
    }
  }

//makes sure the coords you hit are valid
function isValidCoordinates(row, col) {
    if (row < 0 || row >= 10 || col < 0 || col >= 10) {
      return false;
    }
    return true;
  }

//checks to see if theres a ship in the square you hit
function checkShipSunk(row, col) {
    if (gamemap[row][col] === 1) {
        gamemap[row][col] = "X"; //mark the ship as hit on the map
      for (var r = 0; r < gamemap.length; r++) {
        for (var c = 0; c < gamemap[r].length; c++) {
          if (gamemap[r][c] === 1) {
            return false; //at least one ship cell is still not hit, so the ship is not sunk yet
          }
        }
      }
      return true; //all ship cells are hit, so the ship sank
    }
    return false; //there is no ship at the given location
  }
  
//checks to see if a square has been hit already
function checkSquareHit(row, col) {
    if (targetmap[row][col] === "X") {
        return true;
    }
    return false;
}


loadShipMap(gamemap);
initTargetingMap();

//game loop
while (missles > 0) {
    console.log("your missiles remaining are " + missles);
    console.log("current game board:\n");
    console.log(gamemap);
    console.log(targetmap);
    //input for playing
    var row = parseInt(prompt("row to attack (0-9):"));
    var col = parseInt(prompt("column to attack (0-9):"));
    //tells you when you sunk a ship
    if (isValidCoordinates(row, col)) {
        if (checkShipSunk(row, col)) {
            console.log("You sunk a ship!");
            hitCount++;
            //if you sink 17 ships, you win
            if (hitCount == 17) {
                console.log("Congratulations, you've sunk all the ships and won the game!");
                return;
            }
        }
    }
}
// Do not change any of the code below!
if (require.main === module)
{
    main();
}
"use strict";
/*
Author: Liam Butler
Assignment #: 4
Description: Q3 (BONUS): Battleship
*/
var fs = require("fs");
var gamemap = [];
var targetmap = [];
var missles = 30;
var hitCount = 0;
var shipssunk = [false, false, false, false, false];
function main() // <-- Don't change this line!
{ 
    //load the map.txt file
    function loadShipMap(targetmap) {
        var targetmap = fs.readFileSync(targetmap, "utf8");
        //split them into rows
        var rows = targetmap.split("\n");
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i].split(",");
            gamemap.push(row);
        }
    }
}
//initiate the targeting map
function initTargetingMap() {
    for (var i = 0; i < 10; i++) {
      var row = [];
      for (var j = 0; j < 10; j++) {
        row.push(" ");
      }
      targetmap.push(row);
    }
  }

  //updates the map with the amount of missles
  function updateTargetingMap(row, col, hit) {
    if (hit) {
      targetmap[row][col] = "X";
    } else {
      targetmap[row][col] = "-";
    }
  }

//makes sure the coords you hit are valid
function isValidCoordinates(row, col) {
    if (row < 0 || row >= 10 || col < 0 || col >= 10) {
      return false;
    }
    return true;
  }

//checks to see if theres a ship in the square you hit
function checkShipSunk(row, col) {
    if (gamemap[row][col] === 1) {
        gamemap[row][col] = "X"; //mark the ship as hit on the map
      for (var r = 0; r < gamemap.length; r++) {
        for (var c = 0; c < gamemap[r].length; c++) {
          if (gamemap[r][c] === 1) {
            return false; //at least one ship cell is still not hit, so the ship is not sunk yet
          }
        }
      }
      return true; //all ship cells are hit, so the ship sank
    }
    return false; //there is no ship at the given location
  }
  
//checks to see if a square has been hit already
function checkSquareHit(row, col) {
    if (targetmap[row][col] === "X") {
        return true;
    }
    return false;
}


loadShipMap(gamemap);
initTargetingMap();

//game loop
while (missles > 0) {
    console.log("your missiles remaining are " + missles);
    console.log("current game board:\n");
    console.log(gamemap);
    console.log(targetmap);
    //input for playing
    var row = parseInt(prompt("row to attack (0-9):"));
    var col = parseInt(prompt("column to attack (0-9):"));
    //tells you when you sunk a ship
    if (isValidCoordinates(row, col)) {
        if (checkShipSunk(row, col)) {
            console.log("You sunk a ship!");
            hitCount++;
            //if you sink 17 ships, you win
            if (hitCount == 17) {
                console.log("Congratulations, you've sunk all the ships and won the game!");
                return;
            }
        }
    }
}
// Do not change any of the code below!
if (require.main === module)
{
    main();
} remember this code for later