点击注册 1d 战舰游戏。使用数组记录以前的用户输入,然后与当前输入进行交叉检查

Hit register for a 1d Battleship game. Using array to record previous user inputs, then cross-checking them with current input

我是一名新手程序员。我已经开始自学 JavaScript。我做了一个基本的战舰游戏。问题是,如果用户进入同一位置(如果命中)3 次,战舰就会沉没。为了避免这种情况,我添加了一个数组 "userchoices" 来记录用户输入,然后通过遍历 for 循环进行交叉检查。反过来,for 循环包含一个 If 语句,如果用户之前已经在该位置开过火,该语句应该提醒用户。问题是每次都会执行 if 语句。

请查看下面的代码并提出更正建议。谢谢。

var randomloc = Math.floor(Math.random() * 5);
var location1 = randomloc;
var location2 = location1 + 1;
var location3 = location2 + 1;
var guess;
var userchoices = [];
var hits = 0;
var guesses = 0;
var issunk = false;

function battleship() {
while(issunk == false)
{
    guess = prompt("Ready,Aim,Fire! (Enter a number 0-6):");

    console.log("users input = " + guess);

    if (guess == null)
        break;

    if (guess < 0 || guess > 6){
        alert("Please enter a valid cell number. No of guesses has been 
    incremented.")
    }
    else{
        guesses++;
        userchoices[guesses] = guess;
        console.log("users choices = " + userchoices);
        }

   /* for(var i = 0; i <= guesses; i++)
        {
            if(userchoices[guesses] = guess)
            console.log("you have already fired at this location");
        } */

    if (guess == location1 || guess == location2 || guess == location3){
        alert("Enemy Battleship HIT");
    hits = hits + 1;

    if (hits == 3){
        issunk = true;
        alert("Enemy battleship sunk")
    }
        }
    else{
        alert("You Missed");
    }
    }
    if (issunk){var stats = "you took " + guesses + " guesses to sink the battleship. You accuracy was " + (3/guesses);alert(stats);}
    else{alert("You Failed!"); issunk = false;}
    }

这是导致错误的部分

for(var i = 0; i<=guesses; i++)
{
if (userchoices[guesses] = guess){
console.log("you have fired at this location already");
}}

if 语句应该只在用户输入他已经开火的格子号时执行,无论命中还是未命中。

您正在通过错误的索引访问数组。尝试 userchoices[i] 而不是 userchoices[guesses]。还使用 2 个等号 ==:

执行相等比较
for(var i = 0; i<=guesses; i++)
{
  if (userchoices[i] == guess){
    console.log("you have fired at this location already");
  }
}

也可以表示为:

  if (userchoices.includes(guess)){
    console.log("you have fired at this location already");
  }

此外,在添加第一个值后应增加猜测:

    else{
      userchoices[guesses] = guess;
      guesses++;
      console.log("users choices = " + userchoices);
    }

编辑

此处存在逻辑错误,因为您在将元素插入数组后检查数组中的元素,请在插入元素之前在else 语句中执行检查。结合以上所有内容:

else if (userchoices.includes(guess)){
  console.log("you have fired at this location already");
} else {
  userchoices[guesses] = guess;
  guesses++;
  console.log("users choices = " + userchoices);
}

在 Avin Kavish 的急需帮助和我自己的一些修补之后,我现在可以为未来的观众提供我自己的问题的答案。

编辑:更像是我的最终计划

function battleship() 
{
var guess; //Stores user's guess
var userchoices = []; //records user's guess until ship is sunk or user chickens out
var issunk = false; //status of ship
var hits = 0; //number of hits
var guesses = 0; //number of guesses
var randomloc = Math.floor(Math.random() * 5); //Random Number Generator
var location1 = randomloc; 
var location2 = location1 + 1;
var location3 = location2 + 1;

while(issunk == false)
{
    guess = prompt("Ready,Aim,Fire! (Enter a number 0-6):");
    console.log("users input = " + guess);

    if(guess == null) // If users presses 'OK' without entering anything or the 'Cancel' this would break the loop.
        break;

    if (guess < 0 || guess > 6){
        alert("Please enter a valid cell number. No of guesses has been incremented.");
guesses++; //Gotta punish the player.
    }
    else if (userchoices.includes(guess) == false) /*instead of doing what i did yo u 
can change this line to "else if (userchoices.includes(guess)) and then put the 
following oprations in its else clause. */
    {
        guesses++;
        userchoices[guesses] = guess;
        console.log("User choices = " + userchoices);

        if (guess == location1 || guess == location2 || guess == location3)
        {
            alert("Enemy Battleship HIT");
            hits = hits + 1;
            if (hits == 3)
            {
                issunk = true;
                alert("Enemy battleship sunk");
            }
        }
            else
            {
                alert("You Missed");
            }
    }
         else
        {
            alert("you have already fired at this location.")
        }
    if (issunk) //writing issunk == true is overkill
    {
        var stats = "you took " + guesses + " guesses to sink the battleship. You 
accuracy was " + (3/guesses);
        alert(stats);
    }
}
if(guess == null && issunk == false)
console.log("You failed");  //Humiliate the user for chickening out.
userchoices = []; //Empties the array so user can start over again without relaoding the page
issunk = false; //sets issunk to false for a new game
var randomloc = Math.floor(Math.random() * 5); //creates new random numbers for ship coordinates
}

2D 7X7 版本即将推出。请问这里post