<?php

    session_name(get_current_user() . "hangman");
    session_start();
    
    if (!isset($_SESSION['toGuess'])) {
        header("Location: hangman-init.php");
    }
    
    $mask = implode(" ", $_SESSION['mask']);
echo <<<END
<h1>Wörter raten</h1>
<p>$mask</p>
END;

echo <<<END
    <form action="hangman-guess.php" method="post">
END;
    //Output all buttons
    $alphabet = range('A', 'Z');
    foreach($alphabet as $abc) {
echo <<<END
        <button name="letter" type="submit" value="$abc">$abc</button>
END;

    }
        
echo <<<END
    </form>
END;

//Show errorCount
$errors = $_SESSION['errorCount'];
echo <<<END
<p>$errors / 8</p>
END;

//Show the restart link
$state = $_SESSION['state'];
if ($state > 0) {
echo <<<END
    <a href="hangman-init.php">Neues Spiel starten</a>
END;
}

if ($state == 1) {
echo <<<END
    <p>Gewonnen!</p>
END;
}

if ($errors > 8) {
$guessword = $_SESSION['toGuess'];
echo <<<END
    <p>Verloren. Das richtige Wort wäre $guessword gewesen!</p>
END;
}

//HOW MUCH IS THE FISH
echo <<<END
    <img src="img/fish-$errors.svg">
END;


?>
