我一直在使用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.
}
}
答案 0 :(得分:0)
如果我理解了这个问题,那么这种方法可能会起作用:你需要的是一组数组,所以想象
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]);
答案 1 :(得分:0)
看起来{/ 1}}是从
分配的game
由于const game = Math.floor(Math.random() * (categories.length))
为4,因此您将始终获得0,1,2或3的值。之后,您的categories.length
语句会评估if...else if
而不会更改为{ {1}}或game === 1
因此它永远不会找到其他单词类别。另外,我建议game === 2
而不是所有if语句。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch