If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Quiz game in flash or HTML

I need to be able to make a quiz game in Flash or by using HTML where it will ask users a question and give them 3 possible answers. It needs to count how many questions they have correct and show a certain page depending on this at the end.

It has a list of terms/translations and randomly picks from the list you choose and continues through the end of the list, generating the wrong answers from similar terms (close in the list).

Here's the source code. Sorry it's kinda messy. This was a rough draft and I'm going to make it dymamic (multiple lists, multiple languages) soon, but the concept is sound.

I have a comment for every action that happens in the code, so the code looks VERY busy, but don't let that scare you off-- comments will definitely help. Clearly, you can remove them for the final code. Just an explanation I wrote up.

function getwords($file) { //main function for getting the trivia data, returns array$words = explode("\n",file_get_contents($file)); //grab each line of the file as an arrayforeach ($words as $l) { //for each array item$l = trim($l); //trim them to get rid of any junkif ($l[0].$l[1]=='//') { //if it starts with a title (in the form of a comment, like //title)$cat = substr($l,2); //set the category as this line starting after //$n=0; //the number in the category array set to 0} else if (substr_count($l,'|')===1&&isset($cat)) { //if there is a | in it (splitter) and the category is set$l = explode('|',$l,2); //split the answer at | and save as l$out[$cat][$n] = isset($_SESSION['invert'])?array($l[1],$l[0]):array($l[0],$l[1]);//based on if invert is set or not, save as the output variable's $cat array's $nth item, term|answer or answer|term$n++; //add to the number in the category} } return $out; //return the final array}

if (!isset($_GET['main'])&&(isset($_GET['a'])||isset($_GET['cat']))) { //if the main link was NOT click and an answer or category has been sent via getif (isset($_GET['cat'])) { //a category was picked, setup triva, etc.$_SESSION['cat'] = $_GET['cat']; //set the category from the request to the session$ger1 = getwords('list1.php'); //get the array of questions$_SESSION['qs'] = $ger1[$_SESSION['cat']]; //set the questions array in session$_SESSION['qsb'] = $ger1[$_SESSION['cat']]; //set the backup (non-deleting) questions array for making the false answers} if (isset($_SESSION['cat'])) { //if the category is set in the sessionif (!isset($_SESSION['a'])||!isset($_GET['a'])) { //nothing has been sent, so it must be a new round$msg = 'Welcome to trivia. Pick the answer that matches the term.'; //welcome/default message} else { //ok, answer has been sent--@$_SESSION['scoren']++; //add to session variable for number of questions answeredif ($_SESSION['a']==$_GET['a']) { //if the sent answer is the same as the answer stored$msg = 'Correct. <i>'.$_SESSION['q'].'</i> is <i>'.$_SESSION['a'].'</i>.'."\n";//yes, confirm answer@$_SESSION['scorec']++; //add to session variable for correct answers} else {$msg = 'No. The correct answer for <i>'.$_SESSION['q'].'</i> is <i>'.$_SESSION['a'].'</i>.'."\n";//no, and tell answer} } echo $msg.'<br>'."\n"; //echo messageif (isset($_SESSION['scoren'],$_SESSION['scorec'])) { //if the score values are setecho '<br>You have answered '.$_SESSION['scorec'].'/'.$_SESSION['scoren'].' questions correctly. (Of '.count($_SESSION['qsb']).' total questions.)<br>'."\n"; //show score/status} if (!empty($_SESSION['qs'])) { //if there are questions left$n = mt_rand(0,count($_SESSION['qs'])-1); //get a random number for the question based on the number of questions left$s = $_SESSION['qs'][$n]; //the answer set (s) is the random number nth item in the array questionsunset ($_SESSION['qs'][$n]); //remove used question from listsort($_SESSION['qs']); //reorder questions to get rid of any blanks left from removing question$q = $s[0]; //question is first part of that array$a = $s[1]; //answer is the second part of that arrayecho '<h1>'.$q.'</h1>'; //echo [big] the term/question$i=3; //remaining answers -- 3 + real answer = 4 total$as[0]=$a; //set real answer in answers array$_SESSION['a']=$a; //set answer in session var$_SESSION['q']=$q; //set question in session var$max=0; //set the max iterations for the while loopwhile ($i>0) { //while the 4 answers aren't yet set$r = $_SESSION['qsb'][mt_rand(0,count($_SESSION['qsb'])-1)]; //get random answer from question backup arrayif (!in_array($r[1],$as)&&!isset($as[$r[0]])) { //if neither the answer nor the term are already in the array$as[$i]=$r[1]; //set the answer in answers array$i--; //number of answers remaining goes down$max++; //add to max counterif ($max>100) { $i=0; } //force to end if 100 answers doesn't give 4 results} }shuffle($as); //mix up the answersecho '<div id="answers">'; //start answers divecho '<form action="?" method="get">'; //start answer formforeach ($as as $ta) { //for each answerecho '<input type="submit" name="a" value="'.$ta.'">'; //echo answer button} echo '</form></div><br><br>'; //end form/div/etcecho '<a href="?main">Main</a>'; //main/cancel linkecho '</body></html>'; //end htmldie(); //end output (don't show main page below)} else { //no more questionsecho 'You have finished all the questions in this category.<br><br>'; //echo textunset($_SESSION['scoren'],$_SESSION['scorec']); //unset score} }}unset($_SESSION['qs'],$_SESSION['qs2'],$_SESSION['a'],$_SESSION['cat'],$_SESSION['scoren'],$_SESSION['scorec']);//unset any quiz variables remaining$ger1 = getwords('list1.php'); //use getwords (see above) to set questionsecho 'Choose a category below to start:'."\n"; //echo textforeach ($ger1 as $cat=>$terms) { //foreach set within the text fileecho '<br><a href="?cat='.$cat.'">'.$cat.'</a>'."\n"; //for each category, output the link to that set of questions}echo '<br><br><a href="?invert">Switch to '.(isset($_SESSION['invert'])?$lang.' to English':'English to '.$lang).'</a>';//echo the switch lang>eng or eng>lang link, based on the current settingsecho '</body></html>'; //end html out

The format is quite rigid at the moment. Be sure you have a category set (with the //category marker) BEFORE any lines. Then put each answer/question pair on a single line without any spacing/etc., as question|answer (or inverted, depending on how you end up working with it).

umm.... if you have Flash... it comes preinstalled with Quiz templates

Open Flash.. Click File >> New... In the box that comes up click the "templates" tab and select "quiz" from the left choice box, then the style you want on the right, change it to your things, and publish