Skip to content

Instantly share code, notes, and snippets.

@ascii-dev
Created May 9, 2017 08:12
Show Gist options
  • Save ascii-dev/3fff72ac7084e710c1efb2678e5ce2eb to your computer and use it in GitHub Desktop.
Save ascii-dev/3fff72ac7084e710c1efb2678e5ce2eb to your computer and use it in GitHub Desktop.
Hangman Game (Switch Assignment)
<?php
session_start();
/**
* This is the hangman game class
*/
class Hangman
{
$words = 'words.txt';
function fetchWordArray($wordFile)
{
$file = fopen($wordFile,'r');
if ($file)
{
$random_line = null;
$line = null;
$count = 0;
while (($line = fgets($file)) !== false)
{
$count++;
if(rand() % $count == 0)
{
$random_line = trim($line);
}
}
if (!feof($file))
{
fclose($file);
return null;
}else
{
fclose($file);
}
}
$answer = str_split($random_line);
return $answer;
}
function hideCharacters($answer)
{
$noOfHiddenChars = floor((sizeof($answer)/2) + 1);
$count = 0;
$hidden = $answer;
while ($count < $noOfHiddenChars )
{
$rand_element = rand(0,sizeof($answer)-2);
if( $hidden[$rand_element] != '_' )
{
$hidden = str_replace($hidden[$rand_element],'_',$hidden,$replace_count);
$count = $count + $replace_count;
}
}
return $hidden;
}
function checkAndReplace($userInput, $hidden, $answer)
{
$i = 0;
$wrongGuess = true;
while($i < count($answer))
{
if ($answer[$i] == $userInput)
{
$hidden[$i] = $userInput;
$wrongGuess = false;
}
$i = $i + 1;
}
if (!$wrongGuess) $_SESSION['attempts'] = $_SESSION['attempts'] - 1;
return $hidden;
}
function checkGameOver($MAX_ATTEMPTS,$userAttempts, $answer, $hidden)
{
if ($userAttempts >= $MAX_ATTEMPTS)
{
echo "Game Over. The correct word was ";
foreach ($answer as $letter) echo $letter;
echo '<br><form action = "" method = "post"><input type = "submit" name' +
' = "newWord" value = "Try another Word"/></form><br>';
die();
}
if ($hidden == $answer)
{
echo "Game Over. The correct word is indeed ";
foreach ($answer as $letter) echo $letter;
echo '<br><form action = "" method = "post"><input ' +
'type = "submit" name = "newWord" value = "Try another Word"/></form><br>';
die();
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Switch's Hangman</title>
</head>
<body>
<?php
if (isset($_POST['newWord'])) unset($_SESSION['answer']);
if (!isset($_SESSION['answer']))
{
$_SESSION['attempts'] = 0;
$solution = fetchWordArray($words);
$attempts = len($solution) + 1;
$_SESSION['answer'] = $solution;
$_SESSION['hidden'] = hideCharacters($solution);
echo 'Attempts remaining: '.($attempts - $_SESSION['attempts']).'<br>';
}else
{
if (isset ($_POST['userInput']))
{
$userInput = $_POST['userInput'];
$_SESSION['hidden'] = checkAndReplace(strtolower($userInput), $_SESSION['hidden'], $_SESSION['answer']);
checkGameOver($MAX_ATTEMPTS,$_SESSION['attempts'], $_SESSION['answer'],$_SESSION['hidden']);
}
$_SESSION['attempts'] = $_SESSION['attempts'] + 1;
echo 'Attempts remaining: '.($MAX_ATTEMPTS - $_SESSION['attempts'])."<br>";
}
$hidden = $_SESSION['hidden'];
foreach ($hidden as $char) echo $char." ";
?>
<script type="application/javascript">
function validateInput()
{
var x=document.forms["inputForm"]["userInput"].value;
if (x=="" || x==" ")
{
alert("Please enter a character.");
return false;
}
if (!isNaN(x))
{
alert("Please enter a character.");
return false;
}
}
</script>
<form name = "inputForm" action = "" method = "post">
Your Guess: <input name = "userInput" type = "text" size="1" maxlength="1" />
<input type = "submit" value = "Check" onclick="return validateInput()"/>
<input type = "submit" name = "newWord" value = "Try another Word"/>
</form>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment