I need to add 3 functions to my battleship game how would I go about adding the following;
1 Storing Guesses and then telling the player they have already guessed that number
Telling them to only enter numeric characters if they enter non numeric
After a certain amount of rounds they loose.
I feel like this is basic to implement but Im confused. Code
//Grid size: result will be n*n cells
const ROW_GRID_SIZE=4;
const COL_GRID_SIZE=5;
// Show Grid
const grid = ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"];
// Ship Locations
const shipLocations = ["3", "9", "15"];
//Entered value
let guess;
//Counter Rounds
let count = 0;
let promptText = "Enter a Number Between 0 and 19";
//We'll loop indefintely until the user clicks cancel on prompt
do {
//Construct prompt using string template (multiline)
const prpt = "Round #"+ ++count + '\n'+ printGrid() + promptText;
guess = prompt(prpt);
if (!guess && guess !== 0)
//Stop when cancel was clicked
break;
// Registeres and Hit
const hit = shipLocations.indexOf(guess) >= 0;
// Updated Game with Hit or Miss
if (hit == true) {
promptText = "You Sunk a Ship";
} else if (hit == false) {
promptText = "You Missed a Ship";
}
grid[guess] = hit ? '1' : 'X';
} while (guess || guess === 0); //Must have an exit condition
/** Pretty-print the grid via function **/
function printGrid() {
let res = "";
for (let r = 0; r < ROW_GRID_SIZE; r++) {
let srow = "";
for (let c = 0; c < COL_GRID_SIZE; c++) {
srow += " " + grid[r * COL_GRID_SIZE + c];
}
res += srow.substr(1) + '\n';
}
return res;
}