Q:

AS3: Creating A Multiple Choice game with Random Questions

Is it possible to create a multiple choice game that asks questions in random? or any quiz game in which the questions comes in random, just using only flash and actionscript 3.0? Any answers will be very much appreciated.

A:

First, Just randomize Quiz, you should create an Array that stores stuff like quiz sentence . and this array shuffle using a following algorthims.
var quiz:Array = 
[
  "1+1 = ?",
  "2+2 = ?",
  "3+3 = ?",
  "4+4 = ?",
  "5+5 = ?"
];

function shuffleArray($arr:Array):Array
{
    var l:Number = $arr.length - 1;

    for (var it:uint = 0; it<l; it++)
    {
        var r:int = Math.round(Math.random() * l);
        var tmp:String = $arr[it];
        $arr[it] = $arr[r];
        $arr[r] = tmp;
    }
    return $arr;
}

var refreshQuiz:Array = shuffleArray(quiz);

trace(refreshQuiz);

If you want build a really quiz game, refer a following skeleton code. This code show to user random quiz. Enter each answer in the Textfield, then the correct answer, the correct answer. Otherwise, it shows the wrong answer is a simple game.
var quizModel:Array = [{q:"1+1 = ?", a:"2"}, {q:"5+5 = ?", a:"10"}, {q:"2+2 = ?", a:"4"}, {q:"6+6 = ?", a:"12"},{q:"8-7 = ?",a:"1"}];

var user_ans:Array = new Array();
var newQuizModel:Array = shuffleArray(quizModel);
stage.addEventListener(KeyboardEvent.KEY_DOWN, go);

function shuffleArray(arr:Array):Array
{
    var l:Number = arr.length - 1;

    for (var it:uint = 0; it<l; it++)
    {
        var r:int = Math.round(Math.random() * l);
        var tmp:Object = arr[it];
        arr[it] = arr[r];
        arr[r] = tmp;
    }
    return arr;
}

var index:int = 0;
questions_txt.text = newQuizModel[index]["q"];
var userAnswer:String = "";
function go(k:KeyboardEvent):void
{
    if (k.keyCode != Keyboard.ENTER)
    {
        return;
    }

    userAnswer = answers_txt.text;

    if (userAnswer == newQuizModel[index]["a"])
    {
        answers_txt.text = "Your answer is correct!";
    }
    else
    {
        answers_txt.text = "Your answer is wrong";
        return;
    }

    index++;
    index%= quizModel.length;
    showQuiz(index);
}

function showQuiz(idx:int):void
{
    questions_txt.text = newQuizModel[idx]["q"];
}

showQuiz(index);

