0

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

  1. Telling them to only enter numeric characters if they enter non numeric

  2. 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;
}

3 Answers3

4
  1. store the guesses in array and check if the answer is include in the array.
const guesses = [];

// after getting the input...
if (guesses.includes(guess)) {
    console.log('you already made that guess')
} else {
    guesses.push(guess);
}

  1. use isNaN(guess) isNan return true if the value is not a number, see this answer
// after getting the input...
if (isNaN(guess)) {
   console.log('the value inserted is not a number')
}
  1. save the amount of rounds in a variable and check if it reached zero in the while loop
let roundCount = 0;
const MAX_ROUNDS = 10; // 10 for example, set any value you want
do {
   // all the logic...
   roundCount++;
} while ((guess || guess === 0) && roundCount < MAX_ROUNDS);
Noy Gafni
  • 1,091
  • 9
  • 19
  • 1
    better yet, use a [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set). although it won't make much of a difference unless you have a large amount of guesses. – kennyvh Apr 28 '21 at 19:46
  • you can use a Set, but in this case you need to check if the value exists, not only save non duplicates values. But this is a good option as well. – Noy Gafni Apr 28 '21 at 19:48
0

Telling them to only enter numeric characters if they enter non numeric

  var code = ('charCode' in e) ? e.charCode : e.keyCode;
            !(code > 47 && code < 58) 

Storing Guesses and then telling the player they have already guessed that number

const count = [1,2,3,4];

const count = count.push(5);
// expected output: 4
count.log(count);

After a certain amount of rounds they loose.

This can be achieved by few if condition

Madhu Nair
  • 428
  • 1
  • 7
  • 20
-1

@NoyGafni answer is good.

Nothing to do with your question.


You can dynamically init grid like this :

const grid = (() => {
    let arr = [];
    arr.length = ROW_GRID_SIZE * COL_GRID_SIZE;
    arr.fill(0, 0, ROW_GRID_SIZE * COL_GRID_SIZE);
    return arr;
})();
Obzi
  • 2,384
  • 1
  • 9
  • 22
  • This doesn't answer the question. This could be in a comment, but **not in an answer!** – FZs Apr 28 '21 at 19:56