【问题标题】:How to fix start function in hangman game如何修复刽子手游戏中的启动功能
【发布时间】:2018-03-20 20:09:10
【问题描述】:

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

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

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

【问题讨论】:

    标签: javascript html css function console


    【解决方案1】:

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

    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]);
    

    【讨论】:

      【解决方案2】:

      看起来game是从

      分配的
      const game = Math.floor(Math.random() * (categories.length))
      

      由于categories.length 为4,您将始终获得0、1、2 或3 的值。之后,您的if...else if 语句评估game === 1,而不是更改为game === 2game === 3所以它永远不会找到其他单词类别。另外,我建议 switch 而不是所有的 if 语句。

      https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

      【讨论】:

        猜你喜欢
        • 2015-05-29
        • 1970-01-01
        • 1970-01-01
        • 2019-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多