我有一个html版本的游戏,其中包含一些Java脚本代码,有一个问题和4个答案选项,其中之一是正确的,有一条生命线要求观众给出百分比,但代码的来源给出总是随机的百分比,我希望它给出始终正确的答案,我该怎么办?
function calculateGraphPercentages(){
var percentageOfDifficulty = (window.GameVariables.QuestionLevel - 1) * 5;
var beDevious = (Math.random() * 100) > 90 ? true : false;
if(window.GameVariables.AnswerAIsOut == true){
window.GameVariables.AskTheAudienceVotingPercents[0] = 0;
}
if(window.GameVariables.AnswerBIsOut == true){
window.GameVariables.AskTheAudienceVotingPercents[1] = 0;
}
if(window.GameVariables.AnswerCIsOut == true){
window.GameVariables.AskTheAudienceVotingPercents[2] = 0;
}
if(window.GameVariables.AnswerDIsOut == true){
window.GameVariables.AskTheAudienceVotingPercents[3] = 0;
}
if(beDevious != true){
if(window.GameVariables.CurrentCorrectAnswer == "A" && window.GameVariables.AnswerAIsOut == false){
window.GameVariables.AskTheAudienceVotingPercents[0] = Math.ceil(Math.random() * (100 - percentageOfDifficulty));
window.GameVariables.AskTheAudienceVotingPercents[1] = window.GameVariables.AnswerBIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[0]));
window.GameVariables.AskTheAudienceVotingPercents[2] = window.GameVariables.AnswerCIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[0] - window.GameVariables.AskTheAudienceVotingPercents[1]));
window.GameVariables.AskTheAudienceVotingPercents[3] = window.GameVariables.AnswerDIsOut == true ? 0 : 100 - window.GameVariables.AskTheAudienceVotingPercents[0] - window.GameVariables.AskTheAudienceVotingPercents[1] - window.GameVariables.AskTheAudienceVotingPercents[2];
}
if(window.GameVariables.CurrentCorrectAnswer == "B" && window.GameVariables.AnswerBIsOut == false){
window.GameVariables.AskTheAudienceVotingPercents[1] = Math.ceil(Math.random() * (100 - percentageOfDifficulty));
window.GameVariables.AskTheAudienceVotingPercents[2] = window.GameVariables.AnswerCIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[1]));
window.GameVariables.AskTheAudienceVotingPercents[3] = window.GameVariables.AnswerDIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[1] - window.GameVariables.AskTheAudienceVotingPercents[2]));
window.GameVariables.AskTheAudienceVotingPercents[0] = window.GameVariables.AnswerAIsOut == true ? 0 : 100 - window.GameVariables.AskTheAudienceVotingPercents[1] - window.GameVariables.AskTheAudienceVotingPercents[2] - window.GameVariables.AskTheAudienceVotingPercents[3];
}
if(window.GameVariables.CurrentCorrectAnswer == "C" && window.GameVariables.AnswerCIsOut == false){
window.GameVariables.AskTheAudienceVotingPercents[2] = Math.ceil(Math.random() * (100 - percentageOfDifficulty));
window.GameVariables.AskTheAudienceVotingPercents[3] = window.GameVariables.AnswerDIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[2]));
window.GameVariables.AskTheAudienceVotingPercents[0] = window.GameVariables.AnswerAIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[2] - window.GameVariables.AskTheAudienceVotingPercents[3]));
window.GameVariables.AskTheAudienceVotingPercents[1] = window.GameVariables.AnswerBIsOut == true ? 0 : 100 - window.GameVariables.AskTheAudienceVotingPercents[2] - window.GameVariables.AskTheAudienceVotingPercents[3] - window.GameVariables.AskTheAudienceVotingPercents[0];
}
if(window.GameVariables.CurrentCorrectAnswer == "D" && window.GameVariables.AnswerDIsOut == false){
window.GameVariables.AskTheAudienceVotingPercents[3] = Math.ceil(Math.random() * (100 - percentageOfDifficulty));
window.GameVariables.AskTheAudienceVotingPercents[0] = window.GameVariables.AnswerAIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[3]));
window.GameVariables.AskTheAudienceVotingPercents[1] = window.GameVariables.AnswerBIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[3] - window.GameVariables.AskTheAudienceVotingPercents[0]));
window.GameVariables.AskTheAudienceVotingPercents[2] = window.GameVariables.AnswerCIsOut == true ? 0 : 100 - window.GameVariables.AskTheAudienceVotingPercents[3] - window.GameVariables.AskTheAudienceVotingPercents[0] - window.GameVariables.AskTheAudienceVotingPercents[1];
}
}
else{
if(window.GameVariables.AnswerAIsOut == false){
window.GameVariables.AskTheAudienceVotingPercents[0] = Math.ceil(Math.random() * (100));
window.GameVariables.AskTheAudienceVotingPercents[1] = window.GameVariables.AnswerBIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[0]));
window.GameVariables.AskTheAudienceVotingPercents[2] = window.GameVariables.AnswerCIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[0] - window.GameVariables.AskTheAudienceVotingPercents[1]));
window.GameVariables.AskTheAudienceVotingPercents[3] = window.GameVariables.AnswerDIsOut == true ? 0 : 100 - window.GameVariables.AskTheAudienceVotingPercents[0] - window.GameVariables.AskTheAudienceVotingPercents[1] - window.GameVariables.AskTheAudienceVotingPercents[2];
}
if(window.GameVariables.AnswerBIsOut == false){
window.GameVariables.AskTheAudienceVotingPercents[1] = Math.ceil(Math.random() * (100));
window.GameVariables.AskTheAudienceVotingPercents[2] = window.GameVariables.AnswerCIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[1]));
window.GameVariables.AskTheAudienceVotingPercents[3] = window.GameVariables.AnswerDIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[1] - window.GameVariables.AskTheAudienceVotingPercents[2]));
window.GameVariables.AskTheAudienceVotingPercents[0] = window.GameVariables.AnswerAIsOut == true ? 0 : 100 - window.GameVariables.AskTheAudienceVotingPercents[1] - window.GameVariables.AskTheAudienceVotingPercents[2] - window.GameVariables.AskTheAudienceVotingPercents[3];
}
if(window.GameVariables.AnswerCIsOut == false){
window.GameVariables.AskTheAudienceVotingPercents[2] = Math.ceil(Math.random() * (100));
window.GameVariables.AskTheAudienceVotingPercents[3] = window.GameVariables.AnswerDIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[2]));
window.GameVariables.AskTheAudienceVotingPercents[0] = window.GameVariables.AnswerAIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[2] - window.GameVariables.AskTheAudienceVotingPercents[3]));
window.GameVariables.AskTheAudienceVotingPercents[1] = window.GameVariables.AnswerBIsOut == true ? 0 : 100 - window.GameVariables.AskTheAudienceVotingPercents[2] - window.GameVariables.AskTheAudienceVotingPercents[3] - window.GameVariables.AskTheAudienceVotingPercents[0];
}
if(window.GameVariables.AnswerDIsOut == false){
window.GameVariables.AskTheAudienceVotingPercents[3] = Math.ceil(Math.random() * (100));
window.GameVariables.AskTheAudienceVotingPercents[0] = window.GameVariables.AnswerAIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[3]));
window.GameVariables.AskTheAudienceVotingPercents[1] = window.GameVariables.AnswerBIsOut == true ? 0 : Math.ceil(Math.random() * (100 - window.GameVariables.AskTheAudienceVotingPercents[3] - window.GameVariables.AskTheAudienceVotingPercents[0]));
window.GameVariables.AskTheAudienceVotingPercents[2] = window.GameVariables.AnswerCIsOut == true ? 0 : 100 - window.GameVariables.AskTheAudienceVotingPercents[3] - window.GameVariables.AskTheAudienceVotingPercents[0] - window.GameVariables.AskTheAudienceVotingPercents[1];
}
}
var sumOfAllPercents = window.GameVariables.AskTheAudienceVotingPercents[0] + window.GameVariables.AskTheAudienceVotingPercents[1] + window.GameVariables.AskTheAudienceVotingPercents[2] + window.GameVariables.AskTheAudienceVotingPercents[3];
if(sumOfAllPercents < 100){
if(window.GameVariables.CurrentCorrectAnswer == "A" && window.GameVariables.AnswerAIsOut == false){
window.GameVariables.AskTheAudienceVotingPercents[0] += 100 - sumOfAllPercents;
}
if(window.GameVariables.CurrentCorrectAnswer == "B" && window.GameVariables.AnswerBIsOut == false){
window.GameVariables.AskTheAudienceVotingPercents[1] += 100 - sumOfAllPercents;
}
if(window.GameVariables.CurrentCorrectAnswer == "C" && window.GameVariables.AnswerCIsOut == false){
window.GameVariables.AskTheAudienceVotingPercents[2] += 100 - sumOfAllPercents;
}
if(window.GameVariables.CurrentCorrectAnswer == "D" && window.GameVariables.AnswerDIsOut == false){
window.GameVariables.AskTheAudienceVotingPercents[3] += 100 - sumOfAllPercents;
}
}
window.GameVariables.AskTheAudienceVotingPercents[0] = window.GameVariables.AskTheAudienceVotingPercents[0]/100;
window.GameVariables.AskTheAudienceVotingPercents[1] = window.GameVariables.AskTheAudienceVotingPercents[1]/100;
window.GameVariables.AskTheAudienceVotingPercents[2] = window.GameVariables.AskTheAudienceVotingPercents[2]/100;
window.GameVariables.AskTheAudienceVotingPercents[3] = window.GameVariables.AskTheAudienceVotingPercents[3]/100;
答案 0 :(得分:1)
在重构这样的代码时,我会遵循这种模式...
function calculateGraphPercentages() {
var percentageOfDifficulty = (window.GameVariables.QuestionLevel - 1) * 5;
var beDevious = (Math.random() * 100) > 90 ? true : false;
var i;
/*
* First off, code is way more readable with smaller variable names.
* Don't be afraid to store things with long names in variables
*/
var votingPercents = window.GameVariables.AskTheAudienceVotingPercents;
var currentAnswer = window.GameVariables.CurrentCorrectAnswer;
var answerAIsOut = window.GameVariables.AnswerAIsOut;
var answerBIsOut = window.GameVariables.AnswerBIsOut;
var answerCIsOut = window.GameVariables.AnswerCIsOut;
var answerDIsOut = window.GameVariables.AnswerDIsOut;
/*
* When you want to do the same thing to something multiple times, you
* want to use a loop
* You might also want to look into using switch/case over if/else
* whenever it makes sense
*/
var answers = [answerAIsOut, answerBIsOut, answerCIsOut, answerDIsOut];
for (i = 0 ; i < answers.length ; i++) {
if (answers[i]) {
votingPercents[i] = 0;
}
}
/*
* So that things are simplified to numbers, why not put A, B, C, and D
* into an array?
*/
var possibleAnswers = ["A", "B", "C", "D"];
/*
* You don't need to compare anything to true, if (true) evaluates to true
* so we can replace all those comparisons to condense the code a bit
*/
if (!beDevious) {
if (currentAnswer == possibleAnswers[0] && !answers[0]) {
votingPercents[0] = Math.ceil(Math.random() * (100 - percentageOfDifficulty));
votingPercents[1] = answers[1] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[0]));
votingPercents[2] = answers[2] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[0] - votingPercents[1]));
votingPercents[3] = answers[3] ? 0 : 100 - votingPercents[0] - votingPercents[1] - votingPercents[2];
}
if (currentAnswer == possibleAnswers[1] && !answers[1]) {
votingPercents[1] = Math.ceil(Math.random() * (100 - percentageOfDifficulty));
votingPercents[2] = answers[2] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[1]));
votingPercents[3] = answers[3] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[1] - votingPercents[2]));
votingPercents[0] = answers[0] ? 0 : 100 - votingPercents[1] - votingPercents[2] - votingPercents[3];
}
if (currentAnswer == possibleAnswers[2] && !answers[2]) {
votingPercents[2] = Math.ceil(Math.random() * (100 - percentageOfDifficulty));
votingPercents[3] = answers[3] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[2]));
votingPercents[0] = answers[0] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[2] - votingPercents[3]));
votingPercents[1] = answers[1] ? 0 : 100 - votingPercents[2] - votingPercents[3] - votingPercents[0];
}
if (currentAnswer == possibleAnswers[3] && !answers[3]) {
votingPercents[3] = Math.ceil(Math.random() * (100 - percentageOfDifficulty));
votingPercents[0] = answers[0] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[3]));
votingPercents[1] = answers[1] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[3] - votingPercents[0]));
votingPercents[2] = answers[2] ? 0 : 100 - votingPercents[3] - votingPercents[0] - votingPercents[1];
}
}
else{
if (!answers[0]) {
votingPercents[0] = Math.ceil(Math.random() * (100));
votingPercents[1] = answers[1] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[0]));
votingPercents[2] = answers[2] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[0] - votingPercents[1]));
votingPercents[3] = answers[3] ? 0 : 100 - votingPercents[0] - votingPercents[1] - votingPercents[2];
}
if (!answers[1]) {
votingPercents[1] = Math.ceil(Math.random() * (100));
votingPercents[2] = answers[2] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[1]));
votingPercents[3] = answers[3] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[1] - votingPercents[2]));
votingPercents[0] = answers[0] ? 0 : 100 - votingPercents[1] - votingPercents[2] - votingPercents[3];
}
if (!answers[2]) {
votingPercents[2] = Math.ceil(Math.random() * (100));
votingPercents[3] = answers[3] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[2]));
votingPercents[0] = answers[0] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[2] - votingPercents[3]));
votingPercents[1] = answers[1] ? 0 : 100 - votingPercents[2] - votingPercents[3] - votingPercents[0];
}
if (!answers[3]) {
votingPercents[3] = Math.ceil(Math.random() * (100));
votingPercents[0] = answers[0] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[3]));
votingPercents[1] = answers[1] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[3] - votingPercents[0]));
votingPercents[2] = answers[2] ? 0 : 100 - votingPercents[3] - votingPercents[0] - votingPercents[1];
}
}
/*
* Now let's do the same thing here as we did above and put this in a
* loop so we can easily add more items later if we need to
*/
var sumOfAllPercents = 0;
for (i = 0 ; i < votingPercents.length ; i++ ) {
sumOfAllPercents += votingPercents[i];
}
if (sumOfAllPercents < 100) {
for (i = 0 ; i < answers.length ; i++) {
if (currentAnswer == possibleAnswers[i] && !answers[i]) {
votingPercents[i] += 100 - sumOfAllPercents;
}
}
}
for (i = 0 ; i < votingPercents.length ; i++) {
votingPercents[i] = votingPercents[i] / 100;
}
}
function calculateGraphPercentages() {
var percentageOfDifficulty = (window.GameVariables.QuestionLevel - 1) * 5;
var beDevious = (Math.random() * 100) > 90 ? true : false;
var i;
/*
* Instead of referencing global variables, why not pass all this info to calculateGraphPercentages like this:
* calculateGraphPercentages(answers, votingPercents, currentAnswer)
* or use some method to manage global state like Flux
*/
var votingPercents = window.GameVariables.AskTheAudienceVotingPercents;
// Save the number of votes so we can reuse it
var numVotes = votingPercents.length
var currentAnswer = window.GameVariables.CurrentCorrectAnswer;
var answerAIsOut = window.GameVariables.AnswerAIsOut;
var answerBIsOut = window.GameVariables.AnswerBIsOut;
var answerCIsOut = window.GameVariables.AnswerCIsOut;
var answerDIsOut = window.GameVariables.AnswerDIsOut;
/*
* When you want to do the same thing to something multiple times, you
* want to use a loop
* You might also want to look into using switch/case over if/else
* whenever it makes sense
*/
var answers = [answerAIsOut, answerBIsOut, answerCIsOut, answerDIsOut];
for (i = 0 ; i < numVotes ; i++) {
if (answers[i]) {
votingPercents[i] = 0;
}
}
/*
* So that you simplify things to just numbers, why not put
* A, B, C, and D into an array?
* You could also make this extensible by iterating over the alphabet
* (or whatever set of characters you choose) and stopping at numVotes
*/
var possibleAnswers = ["A", "B", "C", "D"];
/*
* Now you can try to find patterns in the numbers and figure out how to
* create a loop or recursive function to iterate through a progression
* of numbers so all this code is easier to read and not repeated.
* DRY: Don't Repeat Yourself
*/
/*
* Let's start by putting common functionality into it's own function:
*/
/*
votingPercents[0] = Math.ceil(Math.random() * (100 - percentageOfDifficulty));
votingPercents[1] = answers[1] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[0]));
votingPercents[2] = answers[2] ? 0 : Math.ceil(Math.random() * (100 - votingPercents[0] - votingPercents[1]));
votingPercents[3] = answers[3] ? 0 : 100 - votingPercents[0] - votingPercents[1] - votingPercents[2];
*/
/*
* We'll add a difficulty parameter so we can re-use this function in both places
* We'll use the modulus operator to wrap around
*/
function calcPercents(currentIndex, numIndexes, difficulty) {
var votingPercents = [];
var lastIndex = numIndexes + currentIndex;
votingPercents[currentIndex] = Math.ceil(Math.random() * difficulty);
var totalPercents = votingPercents[currentIndex];
for (var i = currentIndex + 1; i <= lastIndex ; i++) {
totalPercents += votingPercents[i % numIndexes]
votingPercents[i % numIndexes] = answers[i % numIndexes] ? 0 : Math.ceil(Math.random() * (100 - totalPercents))
}
votingPercents[lastIndex] = answers[lastIndex] ? 0 : 100 - totalPercents;
}
/*
* Now we can reuse that function easily
*/
if (!beDevious) {
for (i = 0 ; i < numVotes ; i++) {
calcPercents(i, numVotes, 100 - percentageOfDifficulty)
}
} else {
for (i = 0 ; i < numVotes ; i++) {
calcPercents(i, numVotes, 100)
}
}
/*
* Now let's do the same thing here as we did above and put this in a
* loop so we can easily add more items later if we need to
*/
var sumOfAllPercents = 0;
for (i = 0 ; i < numVotes ; i++ ) {
sumOfAllPercents += votingPercents[i];
}
if (sumOfAllPercents < 100) {
for (i = 0 ; i < numVotes ; i++) {
if (currentAnswer == possibleAnswers[i] && !answers[i]) {
votingPercents[i] += 100 - sumOfAllPercents;
}
}
}
for (i = 0 ; i < numVotes ; i++) {
votingPercents[i] = votingPercents[i] / 100;
}
}
// A function that calculates the voting percentages based on a difficulty rating
function calcPercents(currentIndex, numIndexes, difficulty) {
var votingPercents = [];
var lastIndex = numIndexes + currentIndex;
votingPercents[currentIndex] = Math.ceil(Math.random() * difficulty);
var totalPercents = votingPercents[currentIndex];
for (var i = currentIndex + 1; i <= lastIndex ; i++) {
totalPercents += votingPercents[i % numIndexes]
votingPercents[i % numIndexes] = answers[i % numIndexes] ? 0 : Math.ceil(Math.random() * (100 - totalPercents))
}
votingPercents[lastIndex] = answers[lastIndex] ? 0 : 100 - totalPercents;
return votingPercents;
}
// A function that calculates graph percentages
function calculateGraphPercentages(answers, votingPercents, currentAnswer) {
var percentageOfDifficulty = (window.GameVariables.QuestionLevel - 1) * 5;
var beDevious = (Math.random() * 100) > 90 ? true : false;
var i;
// Save the number of votes so we can reuse it
var numVotes = votingPercents.length;
var possibleAnswers = [];
var sumOfAllPercents = 0;
for (i = 0 ; i < numVotes ; i++) {
if (answers[i]) {
votingPercents[i] = 0;
}
// Create an alpha-numeric list of answers
possibleAnswers.push(String.fromCharCode(65 + 1));
}
if (!beDevious) {
for (i = 0 ; i < numVotes ; i++) {
votingPercents[i] = calcPercents(i, numVotes, 100 - percentageOfDifficulty)
}
} else {
for (i = 0 ; i < numVotes ; i++) {
votingPercents[i] = calcPercents(i, numVotes, 100)
}
}
for (i = 0 ; i < numVotes ; i++ ) {
sumOfAllPercents += votingPercents[i];
}
if (sumOfAllPercents < 100) {
for (i = 0 ; i < numVotes ; i++) {
if (currentAnswer == possibleAnswers[i] && !answers[i]) {
votingPercents[i] += 100 - sumOfAllPercents;
}
}
}
for (i = 0 ; i < numVotes ; i++) {
votingPercents[i] = votingPercents[i] / 100;
}
}
然后我会这样称呼它:
var votingPercents = window.GameVariables.AskTheAudienceVotingPercents;
var currentAnswer = window.GameVariables.CurrentCorrectAnswer;
var answerAIsOut = window.GameVariables.AnswerAIsOut;
var answerBIsOut = window.GameVariables.AnswerBIsOut;
var answerCIsOut = window.GameVariables.AnswerCIsOut;
var answerDIsOut = window.GameVariables.AnswerDIsOut;
var answers = [answerAIsOut, answerBIsOut, answerCIsOut, answerDIsOut];
calculateGraphPercentages(answers, votingPercents, currentAnswer);
但是我会弄清楚如何将这些值放入要传递给函数的结构中,并使它们在整个应用程序中保持一致。
此代码可重复使用,并且更易于调试。希望这会有所帮助:)