创建 true/false 游戏,随机化 Q 顺序?
Create true/false game, randomize the Q order?
我正在创建一个 true/false 事实游戏,它已经基本完成,只剩下一件事了。问题总是以相同的顺序出现,我不确定如何对其进行编码,以使问题始终以随机顺序出现。 感谢您以任何方式提供帮助。
代码:
div id="QA">
<h2></h2>
<span id="buttons"></span>
<p id="mkttext"></p>
<p><strong>Points</strong> : <span>0</span></p>
<div id="demo"></div>
<br>
<div id="centerbutton">
<button id="restart" onClick="history.go(0)">Restart game</button>
</div>
</div>
<script>
var questionnaire = [
{
"question" : "Q: The earth is round",
"valid" : 1, // indicates the correct array number, use 0, 1...
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is True"]
},
{
"question" : "Q: The correct answer is True",
"valid" : 1,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is True"]
},
{
"question" : "Q: Java = JavaScript",
"valid" : 0,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is False"]
},
{
"question" : "Q: Grapes explode when you put them in the microwave",
"valid" : 1,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is True"]
},
{
"question" : "Q: Shakespeare invented both chess and the basketball",
"valid" : 0,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is False"]
},
{
"question" : "Q: LoL stands for World of Warcraft",
"valid" : 0,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is False"]
},
{
"question" : "Q: The average human will shed 40 pounds of skin in a lifetime.",
"valid" : 1,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is True"]
},
{
"question" : "Q: A giraffe can clean its ears with its 121-inch tongue",
"valid" : 0,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is False"]
},
{
"question" : "Q: Nepal is the only country that doesn't have a rectangular flag",
"valid" : 1,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is True"]
},
{
"question" : "Q: The match was invented before the cigarette lighter",
"valid" : 0,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is False"]
},
{
"question" : "Game over, check your score below.",
}
];
var $qa = $('#QA'),
$question = $("h2", $qa),
$buttons = $("#buttons", $qa),
$points = $("p>span",$qa),
questionnaireLength = questionnaire.length,
qc = 0, // Current Question counter
points = 0; // Current points
function QandA(){
var quest = questionnaire[qc],
question = quest.question,
validIdx = quest.valid,
btns = quest.buttons,
answer = quest.answers;
$question.text( question );
//End of the game
if(qc >= questionnaireLength -1){
var link = document.getElementById('buttons');
link.style.display = 'none';
}
// generate buttons with text:
$buttons.empty();
var i=0; i<btns.length;{
$buttons.append("<button id='btnfalse'>"+ btns[i] +"</button>");
}
var i=1; i<btns.length;{
$buttons.append("<button id='btntrue'>"+ btns[i] +"</button>");
}
// Retrieve generated buttons
var $btn = $("button", $buttons);
// Assign click
$btn.one('click', function(){
var idx = $btn.index(this); // get button index
//var parent = document.getElementById('mkttext');
//var p = document.createElement('p');
//p.innerHTML = "Game says: "+ answer[idx] ;
//parent.appendChild(p);
points += (idx === parseInt(validIdx, 10) ? 1 : -0);
$points.text( points );
// Next question
qc++; QandA();
});
}
QandA();
</script>
添加此功能
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex ;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
然后运行你的数组就像
questionnaire = shuffle(questionnaire);
我正在创建一个 true/false 事实游戏,它已经基本完成,只剩下一件事了。问题总是以相同的顺序出现,我不确定如何对其进行编码,以使问题始终以随机顺序出现。 感谢您以任何方式提供帮助。
代码:
div id="QA">
<h2></h2>
<span id="buttons"></span>
<p id="mkttext"></p>
<p><strong>Points</strong> : <span>0</span></p>
<div id="demo"></div>
<br>
<div id="centerbutton">
<button id="restart" onClick="history.go(0)">Restart game</button>
</div>
</div>
<script>
var questionnaire = [
{
"question" : "Q: The earth is round",
"valid" : 1, // indicates the correct array number, use 0, 1...
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is True"]
},
{
"question" : "Q: The correct answer is True",
"valid" : 1,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is True"]
},
{
"question" : "Q: Java = JavaScript",
"valid" : 0,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is False"]
},
{
"question" : "Q: Grapes explode when you put them in the microwave",
"valid" : 1,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is True"]
},
{
"question" : "Q: Shakespeare invented both chess and the basketball",
"valid" : 0,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is False"]
},
{
"question" : "Q: LoL stands for World of Warcraft",
"valid" : 0,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is False"]
},
{
"question" : "Q: The average human will shed 40 pounds of skin in a lifetime.",
"valid" : 1,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is True"]
},
{
"question" : "Q: A giraffe can clean its ears with its 121-inch tongue",
"valid" : 0,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is False"]
},
{
"question" : "Q: Nepal is the only country that doesn't have a rectangular flag",
"valid" : 1,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is True"]
},
{
"question" : "Q: The match was invented before the cigarette lighter",
"valid" : 0,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is False"]
},
{
"question" : "Game over, check your score below.",
}
];
var $qa = $('#QA'),
$question = $("h2", $qa),
$buttons = $("#buttons", $qa),
$points = $("p>span",$qa),
questionnaireLength = questionnaire.length,
qc = 0, // Current Question counter
points = 0; // Current points
function QandA(){
var quest = questionnaire[qc],
question = quest.question,
validIdx = quest.valid,
btns = quest.buttons,
answer = quest.answers;
$question.text( question );
//End of the game
if(qc >= questionnaireLength -1){
var link = document.getElementById('buttons');
link.style.display = 'none';
}
// generate buttons with text:
$buttons.empty();
var i=0; i<btns.length;{
$buttons.append("<button id='btnfalse'>"+ btns[i] +"</button>");
}
var i=1; i<btns.length;{
$buttons.append("<button id='btntrue'>"+ btns[i] +"</button>");
}
// Retrieve generated buttons
var $btn = $("button", $buttons);
// Assign click
$btn.one('click', function(){
var idx = $btn.index(this); // get button index
//var parent = document.getElementById('mkttext');
//var p = document.createElement('p');
//p.innerHTML = "Game says: "+ answer[idx] ;
//parent.appendChild(p);
points += (idx === parseInt(validIdx, 10) ? 1 : -0);
$points.text( points );
// Next question
qc++; QandA();
});
}
QandA();
</script>
添加此功能
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex ;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
然后运行你的数组就像
questionnaire = shuffle(questionnaire);