"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);
}
}
ReferenceError: loadShipMap is not defined
at Object. (C:\Users\Liam Butler\Documents\PROG1700\prog-1700--assignment-4-liamb0469148\q3.js:79:1)
at Module._compile (node:internal/modules/cjs/loader:1126:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
at Module.load (node:internal/modules/cjs/loader:1004:32)
at Function.Module._load (node:internal/modules/cjs/loader:839:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47
Assistant
It looks like there is a ReferenceError thrown in the code. The error message says that the loadShipMap function is not defined. It's possible that there is a typo or a mistake in the function definition or call.
User
loadShipMap(gamemap); on line 79
Assistant
You are passing the gamemap variable to the loadShipMap function on line 79, but the function is not defined anywhere in your code. You have defined a similar function called before the main function, but you are passing to it as a parameter, which is not correct.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
//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
loadShipMap
gamemap
To fix this error, you need to remove the parameter from the loadShipMap function and simply call it without any arguments. This will correctly load the map.txt file into the gamemap variable.
Here's the corrected code:
"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() {
var data = fs.readFileSync("map.txt", "utf8");
//split them into rows
var rows = data.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;