gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 01:59:58 25 4
gpt4 key购买 nike

我一直在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com