改变游戏难度-Javascript



我正在用javascript制作一个hangman游戏。我现在正试图通过增加改变游戏难度的可能性来让它变得更复杂。我测试了几种不同的方法,并试图在谷歌上搜索如何做到这一点,但到目前为止,我的尝试似乎都不起作用。在我最近的尝试中,我尝试创建一个函数,根据所选的难度,将不同的可能单词数组推到一个空数组中进行猜测。作为一个新手,在我看来它应该有效,但事实并非如此,我希望有人能解释我错在哪里,或者我的推理一开始是否完全错误!

这是相关代码。

HTML:

<button id="easy" onclick="changeDifficulty(1)">Easy</button>
<button id="medium" onclick="changeDifficulty(2)">Medium</button>
<button id="hard" onclick="changeDifficulty(3)">Hard</button>

JAVASCRIPT:

// Variables
let level = 0;
let randomWords = [];
let easyWords = ['apple', 'gene'];
let mediumWords = ['article', 'trident'];
let hardWords = ['aposthrophe', 'justification'];
// Select game difficulty and push correct array 
function changeDifficulty(difficulty) {
level = difficulty
if (level === 0) {
randomWords.concat(mediumWords)
} else if (level === 1) {
randomWords.concat(easyWords)
} else if (level === 2) {
randomWords.concat(mediumWords)
} else if (level === 3){
randomWords.concat(hardWords)
}
}
// Generate random word
function getWord() {
answer = randomWords[Math.floor(Math.random()*randomWords.length)]
}
// Call functions
changeDifficulty();
getWord();

然后我在检查员中得到了一个关于另一个功能的错误

function guessedWord() {
wordStatus = answer.split('').map(letter => (guessed.indexOf(letter) >= 0 ? letter : '_')).join('');
document.getElementById('word').innerHTML = wordStatus;}

说";不能读取未定义的属性分割";,所以没有定义答案,这应该是因为我以前的代码不起作用。

关于如何修复我的代码或改变难度的更好方法,有什么建议吗?

提前感谢!

您可以使用排列运算符来简化操作。https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Operators/Spread_syntax

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>

<script>
// Variables
let level = 0;
let randomWords = [];
let easyWords = ['apple', 'gene'];
let mediumWords = ['article', 'trident'];
let hardWords = ['aposthrophe', 'justification'];
// Select game difficulty and push correct array 
function changeDifficulty(difficulty = 0) {

level = difficulty
randomWords = []; // if you remove this, your array gets bigger and bigger

if (level === 0) {
randomWords.push(...mediumWords)
} else if (level === 1) {
randomWords.push(...easyWords)
} else if (level === 2) {
randomWords.push(...mediumWords)
} else if (level === 3){
randomWords.push(...hardWords)
}

// lets see what our words are
console.log(randomWords);

getWord();

}
// Generate random word
function getWord() {
let answer = randomWords[Math.floor(Math.random()*randomWords.length)]
console.log(answer);
}
// Call functions
changeDifficulty();
</script>
</head>
<body>

<button id="easy" onclick="changeDifficulty(1)">Easy</button>
<button id="medium" onclick="changeDifficulty(2)">Medium</button>
<button id="hard" onclick="changeDifficulty(3)">Hard</button>
</body>
</html>

使用点差算子,你可以组合不同的水平,如果你这样做的话:

if (level === 0) {
randomWords.push(...mediumWords)
} else if (level === 1) {
randomWords.push(...easyWords)
} else if (level === 2) {
randomWords.push(...mediumWords, ...easyWords)
} else if (level === 3){
randomWords.push(...hardWords, ...mediumWords)
}

您错过了重新分配阵列的机会:

if (level === 0) {
randomWords = randomWords.concat(mediumWords)
}

但是顺便说一句,在这种情况下使用concat是没有用的,直接分配你的数组而不是

if (level === 0) {
randomWords = mediumWords
}

编辑:我建议你考虑一个不同的逻辑,尽可能地灵活,比如:

let level = "";
let randomWords = [];
let dictionary = {
easy: ['apple', 'gene'],
medium: ['article', 'trident'],
hard: ['aposthrophe', 'justification']
}

function changeDifficulty(difficulty) {
level = difficulty
randomWords = dictionary[level]
}

还有你的按钮:

<button id="easy" onclick="changeDifficulty('easy')">Easy</button>
<button id="medium" onclick="changeDifficulty('medium')">Medium</button>
<button id="hard" onclick="changeDifficulty('hard')">Hard</button>

我稍微修改了你的JS,它现在根据选择的难度从每个数组中生成一个随机单词

JS-

// Variables
let randomWords = [];
let easyWords = ['apple', 'gene'];
let mediumWords = ['article', 'trident'];
let hardWords = ['aposthrophe', 'justification'];
// Select game difficulty and push correct array 
function changeDifficulty(difficulty) {
// no need to use the variable level
if (difficulty === undefined || difficulty === 2) { // Calling array without passing argument passes in undefined so by default its set to choose medium words
randomWords = mediumWords.slice()
} else if (difficulty === 1) {
randomWords = easyWords.slice()
} else if (difficulty === 3){
randomWords = hardWords.slice()
}
console.log(randomWords)
}
// Generate random word
function getWord() {
answer = randomWords[Math.floor(Math.random()*randomWords.length)]
}
// Call functions
changeDifficulty();
getWord();

HTML

<button id="easy" onclick="changeDifficulty(1)">Easy</button>
<button id="medium" onclick="changeDifficulty(2)">Medium</button>
<button id="hard" onclick="changeDifficulty(3)">Hard</button>
<button id="newword" onclick="getWord()">New Word</button>

您需要声明变量答案第一个

let answer = "";

你的方法更改难度是不正确的,因为它没有设置你的randomWords的值,你需要这样做

function changeDifficulty(difficulty) {
level = difficulty
if (level === 0) {
randomWords = randomWords.concat(mediumWords)
} else if (level === 1) {
randomWords = randomWords.concat(easyWords)
} else if (level === 2) {
randomWords = randomWords.concat(mediumWords)
} else if (level === 3){
randomWords = randomWords.concat(hardWords)
}
}

为了正确地得到答案,您应该在每次单击按钮后调用getWord(或逻辑中的其他地方(
(https://jsfiddle.net/4a9sjhcq/)

最新更新