我想改变这个我制作的刽子手游戏,这样每次按下新游戏时,它都会按照它们所处的确切顺序进行随机操作。
// constants
var POSSIBLE_WORDS = ["first", "second", "third"];
var MAX_GUESSES = 7; // number of total guesses per game
// global variables
var word = "?"; // random word user is trying to guess
var guesses = ""; // letters the player has guessed
var guessCount = MAX_GUESSES; // number of guesses player has left
var borderColor = 60;
// Chooses a new random word and displays its clue on the page.
function newGame() {
// choose a random word
var randomIndex = parseInt(Math.random() * POSSIBLE_WORDS.length);
word = POSSIBLE_WORDS[randomIndex];
guessCount = MAX_GUESSES;
guesses = "";
borderColor = 60;
document.getElementById("hangmanpic").style.border = "thin none hsla(0,0%,100%,1)";
document.getElementsByTagName("body")[0].style.backgroundColor = "hsla(0,0%,100%,1)";
updatePage(); // show initial word clue - all underscores
}
答案 0 :(得分:1)
首先,设置一个计数器变量:
var LAST_INDEX=0;
接下来,在newGame()
函数的顶部,选择LAST_INDEX
,然后将LAST_INDEX
增加1,而不是选择随机索引。
word=POSSIBLE_WORDS[LAST_INDEX];
++LAST_INDEX;
应该这样做。
答案 1 :(得分:0)
定义一个变量来保存当前单词的索引:
var currIndex = 0;
然后在newGame
中递增计数器而不是生成随机数:
word = POSSIBLE_WORDS[currIndex];
currIndex = (currIndex + 1) % POSSIBLE_WORDS.length; // for the next round
这将按顺序旋转单词。
如果你不想旋转,那么删除mod部分(%
)并只增加currIndex
并通过与POSSIBLE_WORDS.length
进行比较来检查列表的结尾