0

So I'm picking a random letter from an array that looks like this:

const letters = [
  "W",
  "A",
  "S",
  "D"
]

Now I want to pick one randomly, say it picks D. Now once it picks D once, the next run around, it can't do the same thing, so it has to pick W, A, or S, but can't pick D. This isn't forever permanent, this is just so that a random pick isn't re-chosen.

I have this variable to check the last index, when the random letter is chosen, the last index will equal to the random letter.

How can I make it so that the next random letter is NOT the last index?

Code:

const RandomLetterGUI = document.getElementById("RandomLetters")
const TimerGUI = document.getElementById("Timer")
const LivesGUI = document.getElementById("Lives")
const ScoreGUI = document.getElementById("Score")
const letters = [
  "W",
  "A",
  "S",
  "D"
]
var seconds = 60;
var lives = 3;
var score = 0;
var timerId = setInterval(countdown, 1000);
var gameIsPlaying = true;
function countdown() {
  if (lives == 0) {
    clearTimeout(timerId);
  }
  if (seconds == -1) {
    clearTimeout(timerId);
    gameIsPlaying = false
  } else {
    if (seconds > 9) {
      TimerGUI.innerHTML = ':' + seconds;
    } else {
      TimerGUI.innerHTML = ':0' + seconds;
    }
    seconds--;
  }
}
countdown()
setInterval(displayLives, 0)
setInterval(updateScore, 0)
function updateScore() {
  ScoreGUI.innerHTML = "Score: " + score
}
function displayLives() {
  LivesGUI.innerHTML = "Remaining Lives: " + lives
  if (lives == 0) {
    gameIsPlaying = false
  }
}

var lastIndex
function letter() {
  var item = letters[Math.floor(Math.random() * letters.length)];
  console.log(item, lastIndex)
  if (lastIndex != item) {
    RandomLetterGUI.innerHTML = "Next Letter: " + item
  } else {
    letter()
  }
  document.onkeypress = function (e) {
    if (gameIsPlaying) {
      var key = e.key
      if (key.toUpperCase() != item) {
        lives -= 1;
      } else {
        lastIndex = item
        score += 150;
        letter()
      }
    }
  };
}
letter()

Edit for LawrenceCherone:

const RandomLetterGUI = document.getElementById("RandomLetters")
const TimerGUI = document.getElementById("Timer")
const LivesGUI = document.getElementById("Lives")
const ScoreGUI = document.getElementById("Score")
var seconds = 60;
const letters = [
  "W",
  "A",
  "S",
  "D"
]
var lives = 3;
var score = 0;
var timerId = setInterval(countdown, 1000);
var gameIsPlaying = true;
function countdown() {
  if (lives == 0) {
    clearTimeout(timerId);
  }
  if (seconds == -1) {
    clearTimeout(timerId);
    gameIsPlaying = false
  } else {
    if (seconds > 9) {
      TimerGUI.innerHTML = ':' + seconds;
    } else {
      TimerGUI.innerHTML = ':0' + seconds;
    }
    seconds--;
  }
}
countdown()
setInterval(displayLives, 0)
setInterval(updateScore, 0)
function updateScore() {
  ScoreGUI.innerHTML = "Score: " + score
}
function displayLives() {
  LivesGUI.innerHTML = "Remaining Lives: " + lives
  if (lives == 0) {
    gameIsPlaying = false
  }
}
function letterFactory(letters) {
  let live = []

  const refill = () => {
    live = [...letters]
    for (let i = live.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      const temp = live[i];
      live[i] = live[j];
      live[j] = temp;
    }
  }

  return () => {
    if (!live.length) refill()
    
    return live.shift()
  }
}
function letter() {
  console.log(item, lastIndex)
  RandomLetterGUI.innerHTML = "Next Letter: " + letterFactory(letters)
  document.onkeypress = function (e) {
    if (gameIsPlaying) {
      var key = e.key
      if (key.toUpperCase() != item) {
        lives -= 1;
      } else {
        lastIndex = item
        score += 150;
        letter()
      }
    }
  };
}
letter()
  • 1
    copy letters var into a live copy, then [shuffle the array](https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array), then on each round use [shift](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift) to grab one and remove it from the live array, on game reset repeat – Lawrence Cherone Oct 04 '22 at 22:48
  • @LawrenceCherone basically just throw the letters array into the letter function? – gamer merch Oct 04 '22 at 22:49
  • 1
    yeah or make a factory https://playcode.io/977600 your letter func is a bit messy with all the display and keypress stuff – Lawrence Cherone Oct 04 '22 at 23:03
  • @LawrenceCherone The RandomLetterGUI isn't showing now, I'll edit my post so that you can see what I have now. – gamer merch Oct 04 '22 at 23:21

1 Answers1

0

A shuffle is a good idea to prevent any repeat, but if you only want to rule out the previous rand, just hang onto it and exclude it from the array as you pick the element.

const letters = ['W', 'A', 'S', 'D'];
let previous;

const randLetter = () => {
  let array = previous ? letters.filter(el => el !== previous) : letters;
  previous = array[Math.floor(Math.random()*array.length)]; 
  return previous;
}
danh
  • 62,181
  • 10
  • 95
  • 136