如果正确猜到了,我怎么能用字母替换字母占位符?

时间:2018-03-02 15:47:49

标签: javascript function console.log text-based

每次我使用此代码时,我都会猜到错误的字母。我需要它能够用下划线替换猜到的字母,如果它在单词中。

function guess(){
  for (var j = 0; j < words.length; j++) {
    if (words[j] === userGuesses) {
      underScores[j] = userGuesses
      randomWord--
    } else {
      return 'wrong letter'
    }
  }
}

这是我的完整代码,或者至少我到目前为止所获得的代码。我需要留下来的信件,我需要弄清楚如何让他们输掉游戏,然后在3个错误的猜测之后显示这个词而没有得到这个词。

console.log('HANGMAN\nTry to solve the puzzle by guessing letters using guess(letter).\nIf you miss a letter you get a strike.\nGet 3 strikes and you lose the game.\nTo select difficulty, type difficulty(difficulty).\nDifficulties:\nEasy\nMedium\nHard\nExtreme\nTo start game type start().')

const easyWords = [
  'DOG',
  'CAT',
  'HELLO',
  'FISH',
  'RED',
  'FOOD'
]

const mediumWords = [
  'I LIKE THE COLOR PINK',
  'MY FISHES NAME IS BEN',
  'THE GREATEST SHOWMAN IS THE BEST MOVIE',
  'OK GOOGLE HOW TO PASS IMD',
  'I WORK AT LANDMARK CINEMAS',
  'LEGO BATMAN IS THE ONLY GOOD BATMAN MOVIE'
]

const hardWords = [
  'THIS IS AN EXAMPLE OF A HARDER PHRASE THIS PROJECT IS SO HARD',
  'IVE BEEN STARING AT THIS PROJECT FOR 4 HOURS TODAY I DONT KNOW IF I CAN DO THIS ANYMORE',
  'I REALLY MISS MY DOG HER NAME IS CASSY AND SHES A SHIH TZU AND BARKS A LOT',
  'MY FAVOURITE SONG IS CALLED MASTERPIECE THEATRE PART 3 BY MARIANAS TRENCH',
  'I BOUGHT THE HEDLEY TICKETS 5 MONTHS BEFORE THE ALLEGATIONS CAME OUT',
  'CAN SOMEONE PLEASE HELP ME WITH THIS PROJECT OH  MY GOD'
]

const extremeWords = [
  'LOREM IPSUM DOLOR SIT AMET, CONSECTETUR ADIPISCING ELIT, SED DO EIUSMOD TEMPOR INCIDIDUNT UT LABORE ET DOLORE MAGNA ALIQUA. UT ENIM AD MINIM VENIAM, QUIS NOSTRUD EXERCITATION ULLAMCO LABORIS NISI UT ALIQUIP EX EA COMMODO CONSEQUAT. DUIS AUTE IRURE DOLOR IN REPREHENDERIT IN VOLUPTATE VELIT ESSE CILLUM DOLORE EU FUGIAT NULLA PARIATUR. EXCEPTEUR SINT OCCAECAT CUPIDATAT NON PROIDENT, SUNT IN CULPA QUI OFFICIA DESERUNT MOLLIT ANIM ID EST LABORUM',
  'According to all known laws of aviation, there is no way a bee should be able to fly. Its wings are too small to get its fat little body off the ground. The bee, of course, flies anyway because bees don`t care what humans think is impossible. Yellow, black. Yellow, black. Yellow, black. Yellow, black.'
]

const words = [
  'DOG',
  'CAT',
  'HELLO',
  'FISH',
  'RED',
  'FOOD',
  'I LIKE THE COLOR PINK',
  'MY FISHES NAME IS BEN',
  'THE GREATEST SHOWMAN IS THE BEST MOVIE',
  'OK GOOGLE HOW TO PASS IMD',
  'I WORK AT LANDMARK CINEMAS',
  'LEGO BATMAN IS THE ONLY GOOD BATMAN MOVIE',
  'THIS IS AN EXAMPLE OF A HARDER PHRASE THIS PROJECT IS SO HARD',
  'IVE BEEN STARING AT THIS PROJECT FOR 4 HOURS TODAY I DONT KNOW IF I CAN DO THIS ANYMORE',
  'I REALLY MISS MY DOG HER NAME IS CASSY AND SHES A SHIH TZU AND BARKS A LOT',
  'MY FAVOURITE SONG IS CALLED MASTERPIECE THEATRE PART 3 BY MARIANAS TRENCH',
  'I BOUGHT THE HEDLEY TICKETS 5 MONTHS BEFORE THE ALLEGATIONS CAME OUT',
  'CAN SOMEONE PLEASE HELP ME WITH THIS PROJECT OH  MY GOD',
  'LOREM IPSUM DOLOR SIT AMET, CONSECTETUR ADIPISCING ELIT, SED DO EIUSMOD TEMPOR INCIDIDUNT UT LABORE ET DOLORE MAGNA ALIQUA. UT ENIM AD MINIM VENIAM, QUIS NOSTRUD EXERCITATION ULLAMCO LABORIS NISI UT ALIQUIP EX EA COMMODO CONSEQUAT. DUIS AUTE IRURE DOLOR IN REPREHENDERIT IN VOLUPTATE VELIT ESSE CILLUM DOLORE EU FUGIAT NULLA PARIATUR. EXCEPTEUR SINT OCCAECAT CUPIDATAT NON PROIDENT, SUNT IN CULPA QUI OFFICIA DESERUNT MOLLIT ANIM ID EST LABORUM',
  'ACCORDING TO ALL KNOWN LAWS OF AVIATION, THERE IS NO WAY A BEE SHOULD BE ABLE TO FLY. ITS WINGS ARE TOO SMALL TO GET ITS FAT LITTLE BODY OFF THE GROUND. THE BEE, OF COURSE, FLIES ANYWAY BECAUSE BEES DON`T CARE WHAT HUMANS THINK IS IMPOSSIBLE. YELLOW, BLACK. YELLOW, BLACK. YELLOW, BLACK. YELLOW, BLACK.'
]

var randomWord = []
const underScores = []
const remainingLetters = words.length
const wrongLetter = []
const wrongGuessesLeft = 3
const userGuesses = []
const letter = []

function start(){
  return('Select difficulty by typing [difficulty](), else if type play() to randomize.')
}

function easy(){
  randomWord = easyWords[Math.floor(Math.random() * easyWords.length)]
  for (var i = 0; i < randomWord.length; i++) {
    underScores[i] = '_'
  }
  return(underScores)
}

function medium(){
  randomWord = mediumWords[Math.floor(Math.random() * mediumWords.length)]
  for (var i = 0; i < randomWord.length; i++) {
    underScores[i] = '_'
  }
  return(underScores)
}

function hard(){
  randomWord = hardWords[Math.floor(Math.random() * hardWords.length)]
  for (var i = 0; i < randomWord.length; i++) {
    underScores[i] = '_'
  }
  return(underScores)
}

function extreme(){
  randomWord = extremeWords[Math.floor(Math.random() * extremeWords.length)]
  for (var i = 0; i < randomWord.length; i++) {
    underScores[i] = '_'
  }
  return(underScores)
}

function play(){
  randomWord = words[Math.floor(Math.random() * words.length)]
  for (var i = 0; i < randomWord.length; i++) {
    underScores[i] = '_'
  }
  return(underScores)
}

function guess(){
  for (var j = 0; j < words.length; j++) {
    if (words[j] === userGuesses) {
      underScores[j] = userGuesses
      randomWord--
    } else {
      return 'wrong letter'
    }
  }
}

1 个答案:

答案 0 :(得分:0)

正则表达式解决方案可能如下所示:

  • 当用户猜到一封信时,您可以push进入userGuesses数组。
  • 构造一个正则表达式,用于搜索不在数组中的任何字母:[^ABC ]将匹配字符其他而不是A,B,C和空格。
  • replace所有匹配下划线

&#13;
&#13;
var str = "IVE BEEN STARING AT THIS PROJECT FOR 4 HOURS TODAY I DONT KNOW IF I CAN DO THIS ANYMORE";
var userGuesses = ['A', 'B', 'C', ' '];

var regex = new RegExp('[^' + userGuesses.join("") + ']', 'g');

document.body.innerHTML = str.replace(regex, '_');
&#13;
&#13;
&#13;

循环方法(看起来像你正在走向)可能看起来像这样:

  • 当用户猜到一封信时,您可以push进入userGuesses数组。
  • 循环播放实际的答案字符串
    • 如果该字符包含在userGuesses数组中,请将该字符放入finalString
    • 否则加下划线

&#13;
&#13;
var str = "IVE BEEN STARING AT THIS PROJECT FOR 4 HOURS TODAY I DONT KNOW IF I CAN DO THIS ANYMORE";
var userGuesses = ['A', 'B', 'C', ' '];
var finalString = "";

for (var i = 0; i < str.length; i++) {
  if (userGuesses.includes(str[i])) {
    finalString += str[i];
  } else {
    finalString += "_";
  }
}

document.body.innerHTML = finalString;
&#13;
&#13;
&#13;