public function cmp_magic($a, $b)
 {
     return game_strlen($a) + mt_rand(0, 3) - (game_strlen($b) - mt_rand(0, 1));
 }
예제 #2
0
function game_sudoku_check_number($id, $game, $attempt, $sudoku, $pos, $num, $context)
{
    global $DB;
    $correct = game_substr($sudoku->data, $pos - 1, 1);
    if ($correct != $num) {
        game_sudoku_play($id, $game, $attempt, $sudoku, false, false, $context);
        return;
    }
    $leng = game_strlen($sudoku->guess);
    $lend = game_strlen($sudoku->data);
    if ($leng < $lend) {
        $sudoku->guess .= str_repeat(' ', $lend - $leng);
    }
    game_setchar($sudoku->guess, $pos - 1, $correct);
    if (!$DB->set_field_select('game_sudoku', 'guess', $sudoku->guess, "id={$sudoku->id}")) {
        print_error('game_sudoku_check_number: Cannot update table game_sudoku');
    }
    game_sudoku_play($id, $game, $attempt, $sudoku, false, false, $context);
}
예제 #3
0
 public function loadcryptex($crossm, &$mask, &$corrects, &$language)
 {
     global $DB;
     $questions = array();
     $corrects = array();
     $mask = str_repeat('0', $crossm->cols * $crossm->rows);
     if ($recs = $DB->get_records('game_queries', array('attemptid' => $crossm->id))) {
         foreach ($recs as $rec) {
             if ($rec->questiontext == '') {
                 $rec->questiontext = ' ';
             }
             $key = $this->gethash($rec->questiontext) . '-' . $rec->answertext . '-' . $rec->id;
             $questions[$key] = $rec;
             $word = $rec->answertext;
             $pos = $crossm->cols * ($rec->row - 1) + ($rec->col - 1);
             $len = game_strlen($word);
             $found = $rec->answertext == $rec->studentanswer;
             for ($i = 0; $i < $len; $i++) {
                 $c = $found ? '1' : '2';
                 if (game_substr($mask, $pos, 1) != '1') {
                     game_setchar($mask, $pos, $c);
                 }
                 $pos += $rec->horizontal ? 1 : $crossm->cols;
             }
             if ($found) {
                 $corrects[$rec->id] = 1;
             }
             if ($language == '') {
                 $language = game_detectlanguage($rec->answertext);
             }
         }
         ksort($questions);
     }
     return $questions;
 }
예제 #4
0
function hangman_existall($str, $strfind)
{
    $n = game_strlen($str);
    for ($i = 0; $i < $n; $i++) {
        $pos = game_strpos($strfind, game_substr($str, $i, 1));
        if ($pos === false) {
            return false;
        }
    }
    return true;
}
예제 #5
0
function game_cross_unpackpuzzle($g)
{
    $ret = "";
    $len = game_strlen($g);
    while ($len) {
        for ($i = 0; $i < $len; $i++) {
            $c = game_substr($g, $i, 1);
            if ($c >= '1' and $c <= '9') {
                if ($i > 0) {
                    //found escape character
                    if (game_substr($g, $i - 1, 1) == '/') {
                        $g = game_substr($g, 0, $i - 1) . game_substr($g, $i);
                        $i--;
                        $len--;
                        continue;
                    }
                }
                break;
            }
        }
        if ($i < $len) {
            //found the start of a number
            for ($j = $i + 1; $j < $len; $j++) {
                $c = game_substr($g, $j, 1);
                if ($c < '0' or $c > '9') {
                    break;
                }
            }
            $count = game_substr($g, $i, $j - $i);
            $ret .= game_substr($g, 0, $i) . str_repeat('_', $count);
            $g = game_substr($g, $j);
            $len = game_strlen($g);
        } else {
            $ret .= $g;
            break;
        }
    }
    return $ret;
}
 function updatecrossquestions(&$rec, &$g, &$pos, &$correctletters, &$wrongletters, &$restletters, $game, $attempt, $crossrec, $loadfromdb)
 {
     global $DB, $USER;
     $word = $rec->answertext;
     $len = game_strlen($word);
     if ($loadfromdb) {
         $guess = $rec->studentanswer;
     } else {
         $guess = game_substr($g, $pos, $len);
     }
     $len_guess = game_strlen($guess);
     $pos += $len;
     $is_empty = true;
     for ($i = 0; $i < $len; $i++) {
         if ($i < $len_guess) {
             $letterguess = game_substr($guess, $i, 1);
         } else {
             $letterguess = " ";
         }
         if ($letterguess != ' ') {
             $is_empty = false;
         }
         $letterword = game_substr($word, $i, 1);
         if ($letterword != $letterguess) {
             if ($letterguess != ' ' and $letterguess != '_') {
                 $wrongletters++;
             }
             game_setchar($guess, $i, '_');
             $restletters++;
         } else {
             if ($letterguess == ' ') {
                 if ($guess == $word) {
                     $correctletters++;
                 } else {
                     //$wrongletters++;
                     //game_setchar( $guess, $i, '_');
                 }
             } else {
                 $correctletters++;
             }
         }
     }
     if ($is_empty) {
         return;
     }
     if ($rec->studentanswer == $guess) {
         return;
     }
     $rec->studentanswer = $guess;
     $updrec = new stdClass();
     $updrec->studentanswer = $guess;
     $updrec->id = $rec->id;
     if (!$DB->update_record('game_queries', $updrec, $rec->id)) {
         print_error('Update game_queries: not updated');
     }
     $score = $correctletters / $len;
     game_update_queries($game, $attempt, $rec, $score, $guess);
 }
    $allletters .= '-';
}
echo "var questions = new Array({$questions});\r";
echo "var words = new Array({$words});\r";
if ($html->type == 'hangmanp') {
    echo "var images = new Array({$images});\r";
}
?>

var to_guess = "";
var display_word = "";
var used_letters = "";
var wrong_guesses = 0;
var used_letters_all = "";
var all_letters = new Array(<?php 
$len = game_strlen($allletters);
for ($i = 0; $i < $len; $i++) {
    if ($i > 0) {
        echo ',';
    }
    echo '"' . game_substr($allletters, $i, 1) . '"';
}
?>
);

function selectLetter(l)
{
    if (can_play == false)
    {
    }
예제 #8
0
function hangman_showpage(&$done, &$correct, &$wrong, $max, &$wordline, &$wordline2, &$links, $game, &$attempt, &$hangman, &$query, $onlyshow, $showsolution, $context)
{
    global $USER, $CFG, $DB;
    $id = optional_param('id', 0, PARAM_INT);
    // Course Module ID.
    $word = $query->answertext;
    $newletter = optional_param('newletter', "", PARAM_TEXT);
    if ($newletter == '_') {
        $newletter = ' ';
    }
    if ($newletter == 'A') {
        $letters = $hangman->letters;
        if ($newletter != null) {
            if (game_strpos($letters, $newletter) === false) {
                $letters .= $newletter;
            }
            if (game_strpos($letters, 'Ã') === false) {
                $letters .= 'Ã';
            }
        }
    } elseif ($newletter == 'C') {
        $letters = $hangman->letters;
        if ($newletter != null) {
            if (game_strpos($letters, $newletter) === false) {
                $letters .= $newletter;
            }
            if (game_strpos($letters, 'Ç') === false) {
                $letters .= 'Ç';
            }
        }
    } else {
        $letters = $hangman->letters;
        if ($newletter != null) {
            if (game_strpos($letters, $newletter) === false) {
                $letters .= $newletter;
            }
        }
    }
    $links = "";
    $alpha = $hangman->allletters;
    $wrong = 0;
    if ($query->questionid) {
        $questiontext = str_replace(array("\\'", '\\"'), array("'", '"'), $query->questiontext);
        $query->questiontext = game_filterquestion($questiontext, $query->questionid, $context->id, $game->course);
    } else {
        $cmglossary = get_coursemodule_from_instance('glossary', $game->glossaryid, $game->course);
        $contextglossary = game_get_context_module_instance($cmglossary->id);
        $query->questiontext = game_filterglossary(str_replace('\\"', '"', $query->questiontext), $query->glossaryentryid, $contextglossary->id, $game->course);
    }
    if ($game->param5) {
        $s = trim(game_filtertext($query->questiontext, $game->course));
        if ($s != '.' and $s != '') {
            echo "<br/><b>" . $s . '</b>';
        }
        if ($query->attachment != '') {
            $file = "{$CFG->wwwroot}/file.php/{$game->course}/moddata/{$query->attachment}";
            echo "<img src=\"{$file}\" />";
        }
        echo "<br/><br/>";
    }
    $wordline = $wordline2 = "";
    $len = game_strlen($word);
    $done = 1;
    $answer = '';
    $correct = 0;
    for ($x = 0; $x < $len; $x++) {
        $char = game_substr($word, $x, 1);
        if ($showsolution) {
            $wordline2 .= $char == " " ? '&nbsp; ' : $char;
            $done = 0;
        }
        if (game_strpos($letters, $char) === false) {
            $wordline .= "_<font size=\"1\">&nbsp;</font>\r\n";
            $done = 0;
            $answer .= '_';
        } else {
            $wordline .= $char == " " ? '&nbsp; ' : $char;
            $answer .= $char;
            $correct++;
        }
    }
    $lenalpha = game_strlen($alpha);
    $fontsize = 5;
    for ($c = 0; $c < $lenalpha; $c++) {
        $char = game_substr($alpha, $c, 1);
        if (game_strpos($letters, $char) === false) {
            // User doesn't select this character.
            $params = 'id=' . $id . '&amp;newletter=' . urlencode($char);
            if ($onlyshow or $showsolution) {
                $links .= $char;
            } else {
                $links .= "<font size=\"{$fontsize}\"><a href=\"attempt.php?{$params}\">{$char}</a></font>\r\n";
            }
            continue;
        }
        if ($char == 'A') {
            if (game_strpos($word, $char) === false and game_strpos($word, 'Ã') === false) {
                $links .= "\r\n<font size=\"{$fontsize}\" color=\"red\">{$char} </font>";
                $wrong++;
            } else {
                $links .= "\r\n<B><font size=\"{$fontsize}\">{$char} </font></B> ";
            }
        } elseif ($char == 'C') {
            if (game_strpos($word, $char) === false and game_strpos($word, 'Ç') === false) {
                $links .= "\r\n<font size=\"{$fontsize}\" color=\"red\">{$char} </font>";
                $wrong++;
            } else {
                $links .= "\r\n<B><font size=\"{$fontsize}\">{$char} </font></B> ";
            }
        } elseif (game_strpos($word, $char) === false) {
            $links .= "\r\n<font size=\"{$fontsize}\" color=\"red\">{$char} </font>";
            $wrong++;
        } else {
            $links .= "\r\n<B><font size=\"{$fontsize}\">{$char} </font></B> ";
        }
    }
    $finishedword = ($done or $wrong >= $max);
    $finished = false;
    $updrec = new stdClass();
    $updrec->id = $hangman->id;
    $updrec->letters = $letters;
    if ($finishedword) {
        if ($hangman->finishedword == 0) {
            // Only one time per word increace the variable try.
            $hangman->try = $hangman->try + 1;
            if ($hangman->try > $hangman->maxtries) {
                $finished = true;
            }
            if ($done) {
                $hangman->corrects = $hangman->corrects + 1;
                $updrec->corrects = $hangman->corrects;
            }
        }
        $updrec->try = $hangman->try;
        $updrec->finishedword = 1;
    }
    $query->percent = ($correct - $wrong / $max) / game_strlen($word);
    if ($query->percent < 0) {
        $query->percent = 0;
    }
    if ($onlyshow or $showsolution) {
        return;
    }
    if (!$DB->update_record('game_hangman', $updrec)) {
        print_error("hangman_showpage: Can't update game_hangman id={$updrec->id}");
    }
    if ($done) {
        $score = 1;
    } else {
        if ($wrong >= $max) {
            $score = 0;
        } else {
            $score = -1;
        }
    }
    game_updateattempts($game, $attempt, $score, $finished);
    game_update_queries($game, $attempt, $query, $score, $answer);
}
예제 #9
0
function game_cryptex_play($id, $game, $attempt, $cryptexrec, $crossm, $updateattempt = false, $onlyshow = false, $showsolution = false, $context, $print = false, $showhtmlprintbutton = true)
{
    global $CFG, $DB;
    if ($game->toptext != '') {
        echo $game->toptext . '<br>';
    }
    echo '<br>';
    $cryptex = new CryptexDB();
    $language = $attempt->language;
    $questions = $cryptex->loadcryptex($crossm, $mask, $corrects, $attempt->language);
    if ($language != $attempt->language) {
        if (!$DB->set_field('game_attempts', 'language', $attempt->language, array('id' => $attempt->id))) {
            print_error("game_cross_play: Can't set language");
        }
    }
    if ($attempt->language != '') {
        $wordrtl = game_right_to_left($attempt->language);
    } else {
        $wordrtl = right_to_left();
    }
    $reverseprint = $wordrtl != right_to_left();
    if ($reverseprint) {
        $textdir = 'dir="' . ($wordrtl ? 'rtl' : 'ltr') . '"';
    } else {
        $textdir = '';
    }
    $len = game_strlen($mask);
    // The count1 means there is a guested letter.
    // The count2 means there is a letter that not guessed.
    $count1 = $count2 = 0;
    for ($i = 0; $i < $len; $i++) {
        $c = game_substr($mask, $i, 1);
        if ($c == '1') {
            $count1++;
        } else {
            if ($c == '2') {
                $count2++;
            }
        }
    }
    if ($count1 + $count2 == 0) {
        $gradeattempt = 0;
    } else {
        $gradeattempt = $count1 / ($count1 + $count2);
    }
    $finished = $count2 == 0;
    if ($finished === false && $game->param8 > 0) {
        $found = false;
        foreach ($questions as $q) {
            if ($q->tries < $game->param8) {
                $found = true;
            }
        }
        if ($found == false) {
            $finished = true;
            // Rich max tries.
        }
    }
    if ($updateattempt) {
        game_updateattempts($game, $attempt, $gradeattempt, $finished);
    }
    if ($onlyshow == false and $showsolution == false) {
        if ($finished) {
            game_cryptex_onfinished($id, $game, $attempt, $cryptexrec);
        }
    }
    ?>
<style type="text/css"><!--

.answerboxstyle  {
background-color:	#FFFAF0;
border-color:	#808080;
border-style:	solid;
border-width:	1px;
display:	block;
padding:	.75em;
width:	240pt;
}
--></style>
<?php 
    $grade = round(100 * $gradeattempt);
    echo get_string('grade', 'game') . ' ' . $grade . ' %';
    echo '<br>';
    echo '<table border=0>';
    echo '<tr><td>';
    $cryptex->displaycryptex($crossm->cols, $crossm->rows, $cryptexrec->letters, $mask, $showsolution, $textdir);
    ?>
</td>

<td width=10%>&nbsp;</td>
<td>

<form  method="get" action="<?php 
    echo $CFG->wwwroot;
    ?>
/mod/game/attempt.php">
<div id="answerbox" class="answerboxstyle" style="display:none;">
<div id="wordclue" name="wordclue" class="cluebox"> </div>
<input id="action" name="action" type="hidden" value="cryptexcheck">
<input id="q" name="q" type="hidden" >
<input id="id" name="id" value="<?php 
    echo $id;
    ?>
" type="hidden">

<div style="margin-top:1em;"><input id="answer" name="answer" type="text" size="24"
 style="font-weight: bold; text-transform:uppercase;" autocomplete="off"></div>

<table border="0" cellspacing="0" cellpadding="0" width="100%" style="margin-top:1em;"><tr>
<td align="right">
<button id="okbutton" type="submit" class="button" style="font-weight: bold;">OK</button> &nbsp;
<button id="cancelbutton" type="button" class="button" onclick="DeselectCurrentWord();">Cancel</button>
</td></tr></table>
</form>
</td>
</tr>
</table>

<?php 
    if ($showhtmlprintbutton) {
        echo '<br><button id="finishattemptbutton" type="button" onclick="OnEndGame();" >' . get_string('finish', 'game');
        echo '</button>';
        echo '<button id="printbutton" type="button" onclick="OnPrint();" >' . get_string('print', 'game');
        echo '</button><br>';
    }
    if ($showhtmlprintbutton) {
        ?>
<script>
    function PrintHtmlClick()
    {
    	document.getElementById("printbutton").style.display = "none";
    	
        window.print();     

    	document.getElementById("printbutton").style.display = "block";	
    }

    function OnPrint()
    {
<?php 
        global $CFG;
        $params = "id={$id}&gameid={$game->id}";
        echo "window.open( \"{$CFG->wwwroot}/mod/game/print.php?{$params}\");";
        ?>
    }

    function OnEndGame()
    {
<?php 
        global $CFG;
        $params = 'id=' . $id . '&action=cryptexcheck&g=&finishattempt=1';
        echo "window.location = \"{$CFG->wwwroot}/mod/game/attempt.php?{$params}\";\r\n";
        ?>
    }
</script>
<?php 
    }
    $i = 0;
    $else = '';
    $contextglossary = false;
    foreach ($questions as $key => $q) {
        $i++;
        if ($showsolution == false) {
            // When I want to show the solution a want to show the questions to.
            if (array_key_exists($q->id, $corrects)) {
                continue;
            }
        }
        $question = game_show_query($game, $q, "{$i}. " . $q->questiontext, $context);
        $question2 = strip_tags($question);
        // ADDED BY DP (AUG 2009) - fixes " breaking the Answer button for this question.
        echo "<script>var msg{$q->id}=" . json_encode($question2) . ';</script>';
        if ($onlyshow == false and $showsolution == false) {
            if ($game->param8 == 0 || $game->param8 > $q->tries) {
                $question .= ' &nbsp;<input type="submit" value="' . get_string('answer') . '" onclick="OnCheck( ' . $q->id . ",msg{$q->id});\" />";
            }
        }
        echo $question;
        if ($showsolution) {
            echo " &nbsp;&nbsp;&nbsp;{$q->answertext}<B></b>";
        }
        echo '<br>';
    }
    if ($game->bottomtext != '') {
        echo '<br><br>' . $game->bottomtext;
    }
    ?>
		<script>
			function OnCheck( id, question)
			{
				document.getElementById("q").value = id;
				document.getElementById("wordclue").innerHTML = question;

				// Finally, show the answer box.
				document.getElementById("answerbox").style.display = "block";
				try
				{
					document.getElementById("answer").focus();
					document.getElementById("answer").select();
				}
				catch (e)
				{
				}
			}
		</script>
<?php 
    if ($print) {
        echo '<body onload="window.print()">';
    } else {
        echo '<body>';
    }
}