Spela Hangman | Visa källkod | XHTML | CSS | How to become a hacker

Visa källkod

Nedanstående filer finns i denna katalogen. Klicka på en fil för att visa dess innehåll.

hangman/

CHangmanSVG.php
PHangman.php
PHangmanTemplate.php
PListDirectory.php
common.php
favicon.ico
hangman.svg

PHangman.php
<?php
// ===========================================================================================
//
// Hangman.php
//
// Description: Play the classic game of hangman.
//
// Author: Mikael Roos
//


// -------------------------------------------------------------------------------------------
//
// Error handling on/off
//
error_reporting(E_ALL);
$debug = "";
$debugEnable = FALSE; 


// -------------------------------------------------------------------------------------------
//
// Take care of GET variables
//
$word 		= empty($_GET['word'])    ? rand(0, 9) : $_GET['word'];
$guessed 	= empty($_GET['guessed']) ? "" : $_GET['guessed'];
$char		 	= empty($_GET['char'])    ? "" : $_GET['char'];

$guessed .= $char;
$guessed = count_chars($guessed, 3);


// -------------------------------------------------------------------------------------------
//
// Create a wordlist and pich the word.
//
$words = Array(
	'pellefant',
	'moped',
	'asket',
	'tant',
	'fotboll',
	'programvaruteknik',
	'webb',
	'databaser',
	'php',
	'xhmtl',
	);

$theWord = $words[$word];

$debug .= $theWord;


// -------------------------------------------------------------------------------------------
//
// Show the string and the chars that are correct, else _
//
$nrChars 			= strlen($theWord);
$currentWord 	= "";

for($i=0; $i<$nrChars; $i++) {
	
	if(stripos($guessed, $theWord[$i]) === FALSE) { 
		$currentWord .= '-';
	} else {
		$currentWord .= $theWord[$i];
	}
}


// -------------------------------------------------------------------------------------------
//
// Check state of game:
// count number of failed attempts
//
$charsGuessed = strlen(count_chars($guessed, 3));
$charsHit 		= strlen(count_chars((str_replace('-', '', $currentWord)), 3));
$failed = $charsGuessed - $charsHit;

$debug .= $failed . $charsGuessed . $charsHit;


// -------------------------------------------------------------------------------------------
//
// Check state of game:
// if all chars are right, then success!
//
$disableButton 		= "";
$gameOverMessage	= "";

if(strpos($currentWord, '-') === FALSE) {
	$disableButton 		= "disabled='disabled'";
	$gameOverMessage	= "Goesnubben, du r�ddade Mr. Hangman!<br/>Pr�va igen!";
	$debug .= "yes";
}

$debug .= $currentWord;
$debug .= strpos($currentWord, "-");

// -------------------------------------------------------------------------------------------
//
// Check state of game:
// if failed nine times, then hanged, game over.
//
if($failed >= 9) {
	$disableButton 		= "disabled='disabled'";
	$gameOverMessage	= "Trist, du lyckades inte r�dda Mr. Hangman...<br/>F�rs�ka p� nytt?";
}


// -------------------------------------------------------------------------------------------
//
// Create a form for managing input.
//
$form = <<< EOD
<form action='PHangman.php' method='get'>

<p style='font-family: monospace; font-size: xx-large; text-transform: uppercase;'>
{$currentWord} ({$guessed})
</p>

<div>
<input type='hidden' name='word'    value='{$word}' />
<input type='hidden' name='guessed' value='{$guessed}' />
<input type='text'   name='char' tabindex='1' size='1' maxlength='1' />
<button type='submit' {$disableButton} tabindex='2' >Gissa</button>
<a href='{$_SERVER['PHP_SELF']}'>Starta om</a>
</div>

<p style='font-size: x-large; color: red'>
$gameOverMessage
</p>

</form>

EOD;


// -------------------------------------------------------------------------------------------
//
// Create html for drawing the hanging man.
//
require_once('CHangmanSVG.php');

$hangman = new CHangmanSVG();

$svgCode = $hangman->DrawPartsOfPicture($failed);


// -------------------------------------------------------------------------------------------
//
// Create and print out the html-page
//
$debug = $debugEnable ? $debug : "";

require_once('common.php');

$html = <<< EOD
<?xml version="1.0" encoding="iso-8859-1" ?> 
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="sv" lang="sv">  
<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1;" />
		<title>H�nga Gubben</title>
		<link rel="shortcut icon" href="favicon.ico" />
		<style type="text/css">
		</style>
	</head>

	<body>
		{$header}
		<h1>Hangman</h1>
		<p>Spela det klassiska "Hangman". Gissa ordet, bokstav f�r bokstav.</p>
		<fieldset>
			<div style='float: left'>
				{$form}
			</div>
			<div style='float: right'>
				{$svgCode}
			</div>
		</fieldset>
		<div style='font-size: small;'>
			{$debug}
		</div>
			{$footer}
	</body>
</html>
EOD;


header("Content-Type: application/xhtml+xml; charset=iso-8859-1");
echo $html;
exit;

?>
 .                        
..:    © Mikael Roos, 2009