【发布时间】: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