javascript - 如何修复刽子手游戏中的开始功能

标签 javascript html css function console

我一直在使用 JavaScript 在控制台中开发刽子手游戏,但我似乎无法弄清楚如何让它随机选择前 2 个类别。我知道这一定是一个简单的解决方案,但我似乎无法弄清楚。

这是我开始游戏的问题所在

function start () {


player.guessedLetters = []
  player.strikes = 0
  player.maxStrikes = 3
  player.display = []
  player.status = true
  displayIn = []



const game = Math.floor(Math.random() * (categories.length))
  if (game === 0) {
    console.log('The category is Easy Words')
    const selectEasyWords = easyWords[Math.floor(Math.random() * (6))]

player.display = selectEasyWords
for (let i = 0; i < selectEasyWords.length; i++) {
  if (selectEasyWords[i] === ' ') {
    displayIn.push(' ')
  } else {
    displayIn.push('_')
  }
}
    return ${displayIn.join(' ')}
  } else if (game === 1) {
    console.log('The category is Medium Words')
    const selectMediumWords = mediumWords[Math.floor(Math.random() * (6))]

player.display = selectMediumWords
for (let i = 0; i < selectMediumWords.length; i++) {
  if (selectMediumWords[i] === ' ') {
    displayIn.push(' ')
  } else {
    displayIn.push('_')
  }
}
return ${displayIn.join(' ')}


} else if (game === 1) {
    console.log('The category is Hard Words')
    const selectHardWords = hardWords[Math.floor(Math.random() * (6))]

player.display = selectHardWords
for (let i = 0; i < selectHardWords.length; i++) {
  if (selectHardWords[i] === ' ') {
    displayIn.push(' ')
  } else {
    displayIn.push('_')
  }
}
return ${displayIn.join(' ')}


} else if (game === 1) {
    console.log('The category is Extreme Words')
    const selectExtremeWords = extremeWords[Math.floor(Math.random() * (2))]

player.display = selectExtremeWords
for (let i = 0; i < selectExtremeWords.length; i++) {
  if (selectExtremeWords[i] === ' ') {
    displayIn.push(' ')
  } else {
    displayIn.push('_')
  }
}
return ${displayIn.join(' ')}


} else {
    return Where did you go wrong
  }
}

这是我的上下文代码的其余部分。

    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 player = {
  guessedLetters: [],
  strikes: 0,
  maxStrikes: 3,
  display: [],
  status: false
}

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 categories = ['Easy Words', 'Medium Words', 'Hard Words', 'Extreme Words']

let displayIn = []

function start () {
  player.guessedLetters = []
  player.strikes = 0
  player.maxStrikes = 3
  player.display = []
  player.status = true
  displayIn = []

  const game = Math.floor(Math.random() * (categories.length))
  if (game === 0) {
    console.log('The category is Easy Words')
    const selectEasyWords = easyWords[Math.floor(Math.random() * (6))]

    player.display = selectEasyWords
    for (let i = 0; i < selectEasyWords.length; i++) {
      if (selectEasyWords[i] === ' ') {
        displayIn.push(' ')
      } else {
        displayIn.push('_')
      }
    }
    return ${displayIn.join(' ')}
  } else if (game === 1) {
    console.log('The category is Medium Words')
    const selectMediumWords = mediumWords[Math.floor(Math.random() * (6))]

    player.display = selectMediumWords
    for (let i = 0; i < selectMediumWords.length; i++) {
      if (selectMediumWords[i] === ' ') {
        displayIn.push(' ')
      } else {
        displayIn.push('_')
      }
    }
    return ${displayIn.join(' ')}
  } else if (game === 1) {
    console.log('The category is Hard Words')
    const selectHardWords = hardWords[Math.floor(Math.random() * (6))]

    player.display = selectHardWords
    for (let i = 0; i < selectHardWords.length; i++) {
      if (selectHardWords[i] === ' ') {
        displayIn.push(' ')
      } else {
        displayIn.push('_')
      }
    }
    return ${displayIn.join(' ')}
  } else if (game === 1) {
    console.log('The category is Extreme Words')
    const selectExtremeWords = extremeWords[Math.floor(Math.random() * (2))]

    player.display = selectExtremeWords
    for (let i = 0; i < selectExtremeWords.length; i++) {
      if (selectExtremeWords[i] === ' ') {
        displayIn.push(' ')
      } else {
        displayIn.push('_')
      }
    }
    return ${displayIn.join(' ')}
  } else {
    return Where did you go wrong
  }
}

function guess (letter) {
  if (player.status) {
    if (displayIn.indexOf('_') !== -1) {
      if (player.strikes < player.maxStrikes) {
        const value = letter.toUpperCase()
        player.guessedLetters.push(value)
        const arrayPuzzle = player.display
        if (arrayPuzzle.indexOf(value) !== -1) {
          for (let d = arrayPuzzle.indexOf(value); d < player.display.length; d++) {
            if (arrayPuzzle.indexOf(value) !== -1 && arrayPuzzle[d] === value) {
              displayIn[d] = value
            }
          }
          if (displayIn.indexOf('_') !== -1) {
            console.log(There are ${value}s in the phrase.)
            return ${displayIn.join(' ')}
          } else {
            player.status = false
            console.log(${displayIn.join(' ')})
            return Congrats. You won!!!!
          }
        } else {
          player.strikes++
          if (player.strikes === player.maxStrikes) {
            player.status = false
            return You ran out of strikes. G A M E  O V E R
          } else {
            return This letter is non existant.
          }
        }
      } else {
        player.status = false
        return You ran out of strikes.  G A M E  O V E R.
      }
    } else {
      player.status = false
      console.log(${displayIn.join(' ')})
      return Congrats U WON!!!
    }
  } else {
    return Please start the game.
  }
}

最佳答案

如果我理解这个问题,那么这种方法可能会奏效:你需要的是一个数组数组,想象一下

var gameModes=[
    ['EasyWords','AnotherEasy'],['MediumWords'],['HardWords']
]

现在选择你可以做的类别:

let game=Math.floor(Math.random() * (gameModes.length))

然后选择一个词

function SelectWord(wordList)
{ 
 let wordIndex=Math.floor(Math.random() * (wordList.length))
 return wordList[wordIndex];
}

现在你可以调用这个函数了

let word=SelectWord(gameModes[game]);

关于javascript - 如何修复刽子手游戏中的开始功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49393261/

相关文章:

javascript - 如何在 Shadow DOM 中不渲染 <content> 标签中的内容?

javascript - 当 DOM 元素滚动到 View 中时动态更改它们(性能)

jQuery 图片选择

html - <ul> 列表背景图片,css

css - 手机上的网站比显示器大

php - 回显 Javascript 不发出警报

javascript - 尝试将文件上传到 Google Drive api 时收到 404,怀疑存在数据格式问题

html - firefox 不显示整个 Sprite 图像

javascript - 以相对速度的简单视差滚动

javascript - 使用 Jasny Bootstrap 事件触发两次