Files
intecsync/cgi-bin/uebung08/hangman_lib.php
2019-06-30 19:01:36 +02:00

53 lines
1.2 KiB
PHP

<?php
//Aufgabe 1
//Bringt ein Wort in das richtige Format
function transformWord($word) {
$word = str_replace("ä", "AE", $word);
$word = str_replace("ö", "OE", $word);
$word = str_replace("ü", "UE", $word);
$word = str_replace("ß", "SS", $word);
$word = strtoupper($word);
return $word;
}
//Maskiert ein Wort mit _
function maskWord($word) {
$arr = array();
for ($i = 0; $i < strlen($word); $i++) {
$arr[] = "_";
}
return $arr;
}
function getAllWords() {
$str = file_get_contents("words-array.php");
$str = str_replace('"', '', $str);
$str = str_replace('[', '', $str);
$str = str_replace(']', '', $str);
$str = str_replace(';', '', $str);
return explode(", ", $str);
}
//Aufgabe 2
function getRandomWord() {
$arr = getAllWords();
return array_rand($arr);
}
// startet die Session und initialisiert das tasks-Array
function initGame()
{
//session_name(get_current_user() . "hangman");
//session_start();
$randword = getRandomWord();
$_SESSION['toGuess'] = $randword;
$_SESSION['mask'] = maskWord($randword);
$_SESSION['guessedLetters'] = [];
$_SESSION['errorCount'] = 0;
$_SESSION['state'] = 0;
}
?>