1
0

Added DB Access + Error page

This commit is contained in:
2014-05-12 10:11:20 +02:00
parent 3efba81ebc
commit ffdc17efd5
124 changed files with 6346 additions and 82 deletions

View File

7
demos/hangman/index.php Normal file
View File

@@ -0,0 +1,7 @@
<?php
$yii=dirname(__FILE__).'/../../framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';
require_once($yii);
Yii::createWebApplication($config)->run();

View File

@@ -0,0 +1 @@
deny from all

View File

@@ -0,0 +1,14 @@
<?php
return array(
'name'=>'Hangman Game',
'defaultController'=>'game',
'components'=>array(
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'game/guess/<g:\w>'=>'game/guess',
),
),
),
);

View File

@@ -0,0 +1,222 @@
<?php
/**
* GameController class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008-2013 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* GameController implements the {@link http://en.wikipedia.org/wiki/Hangman_(game) Hangman game}.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CController.php 131 2008-11-02 01:32:57Z qiang.xue $
* @package demos.hangman
* @since 1.0
*/
class GameController extends CController
{
/**
* @var string sets the default action to be 'play'
*/
public $defaultAction='play';
/**
* The 'play' action.
* In this action, users are asked to choose a difficulty level
* of the game.
*/
public function actionPlay()
{
static $levels=array(
'10'=>'Easy game; you are allowed 10 misses.',
'5'=>'Medium game; you are allowed 5 misses.',
'3'=>'Hard game; you are allowed 3 misses.',
);
// if a difficulty level is correctly chosen
if(isset($_POST['level']) && isset($levels[$_POST['level']]))
{
$this->word=$this->generateWord();
$this->guessWord=str_repeat('_',strlen($this->word));
$this->level=$_POST['level'];
$this->misses=0;
$this->setPageState('guessed',null);
// show the guess page
$this->render('guess');
}
else
{
$params=array(
'levels'=>$levels,
// if this is a POST request, it means the level is not chosen
'error'=>Yii::app()->request->isPostRequest,
);
// show the difficulty level page
$this->render('play',$params);
}
}
/**
* The 'guess' action.
* This action is invoked each time when the user makes a guess.
*/
public function actionGuess()
{
// check to see if the letter is guessed correctly
if(isset($_GET['g'][0]) && ($result=$this->guess($_GET['g'][0]))!==null)
$this->render($result ? 'win' : 'lose');
else // the letter is guessed correctly, but not win yet
{
$guessed=$this->getPageState('guessed',array());
$guessed[$_GET['g'][0]]=true;
$this->setPageState('guessed',$guessed,array());
$this->render('guess');
}
}
/**
* The 'guess' action.
* This action is invoked when the user gives up the game.
*/
public function actionGiveup()
{
$this->render('lose');
}
/**
* Checks to see if a letter is already guessed.
* @param string the letter
* @return boolean whether the letter is already guessed.
*/
public function isGuessed($letter)
{
$guessed=$this->getPageState('guessed',array());
return isset($guessed[$letter]);
}
/**
* Generates a word to be guessed.
* @return string the word to be guessed
*/
protected function generateWord()
{
$wordFile=dirname(__FILE__).'/words.txt';
$words=preg_split("/[\s,]+/",file_get_contents($wordFile));
do
{
$i=rand(0,count($words)-1);
$word=$words[$i];
} while(strlen($word)<5 || !ctype_alpha($word));
return strtoupper($word);
}
/**
* Checks to see if a letter is guessed correctly.
* @param string the letter
* @return mixed true if the word is guessed correctly, false
* if the user has used up all guesses and the word is guessed
* incorrectly, and null if the letter is guessed correctly but
* the whole word is guessed correctly yet.
*/
protected function guess($letter)
{
$word=$this->word;
$guessWord=$this->guessWord;
$pos=0;
$success=false;
while(($pos=strpos($word,$letter,$pos))!==false)
{
$guessWord[$pos]=$letter;
$success=true;
$pos++;
}
if($success)
{
$this->guessWord=$guessWord;
if($guessWord===$word)
return true;
}
else
{
$this->misses++;
if($this->misses>=$this->level)
return false;
}
}
/**
* @return integer the difficulty level. This value is persistent
* during the whole game session.
*/
public function getLevel()
{
return $this->getPageState('level');
}
/**
* @param integer the difficulty level. This value is persistent
* during the whole game session.
*/
public function setLevel($value)
{
$this->setPageState('level',$value);
}
/**
* @return string the word to be guessed. This value is persistent
* during the whole game session.
*/
public function getWord()
{
return $this->getPageState('word');
}
/**
* @param string the word to be guessed. This value is persistent
* during the whole game session.
*/
public function setWord($value)
{
$this->setPageState('word',$value);
}
/**
* @return string the word being guessed. This value is persistent
* during the whole game session.
*/
public function getGuessWord()
{
return $this->getPageState('guessWord');
}
/**
* @param string the word being guessed. This value is persistent
* during the whole game session.
*/
public function setGuessWord($value)
{
$this->setPageState('guessWord',$value);
}
/**
* @return integer the number of misses. This value is persistent
* during the whole game session.
*/
public function getMisses()
{
return $this->getPageState('misses');
}
/**
* @param integer the number of misses. This value is persistent
* during the whole game session.
*/
public function setMisses($value)
{
$this->setPageState('misses',$value);
}
}

View File

@@ -0,0 +1,28 @@
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
Neither the name of Yii Software LLC nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

View File

View File

@@ -0,0 +1,21 @@
<h2>Please make a guess</h2>
<h3 style="letter-spacing: 4px;"><?php echo $this->guessWord; ?></h3>
<p>You have made <?php echo $this->misses; ?> bad guesses out of a maximum of <?php echo $this->level; ?>.</p>
<?php echo CHtml::statefulForm(); ?>
<p>Guess:
<?php
for($i=ord('A');$i<=ord('Z');++$i)
{
if(!$this->isGuessed(chr($i)))
echo "\n".CHtml::linkButton(chr($i),array('submit'=>array('guess','g'=>chr($i))));
}
?>
</p>
<p><?php echo CHtml::linkButton('Give up?',array('submit'=>array('giveup'))); ?></p>
</form>

View File

@@ -0,0 +1,5 @@
<h2>You Lose!</h2>
<p>The word was: <?php echo $this->word; ?>.</p>
<p><?php echo CHtml::link('Start Again',array('play')); ?></p>

View File

@@ -0,0 +1,15 @@
<p>This is the game of Hangman. You must guess a word, a letter at a time.
If you make too many mistakes, you lose the game!</p>
<?php echo CHtml::beginForm(); ?>
<?php echo CHtml::radioButtonList('level', null, $levels); ?>
<br/>
<?php echo CHtml::submitButton('Play!'); ?>
<?php if($error): ?>
<span style="color:red">You must choose a difficulty level!</span>
<?php endif; ?>
<?php echo CHtml::endForm(); ?>

View File

@@ -0,0 +1,5 @@
<h2>You Win!</h2>
<p>The word was: <?php echo $this->word; ?>.</p>
<p><?php echo CHtml::link('Start Again',array('play')); ?></p>

View File

@@ -0,0 +1,14 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hangman Game</title>
</head>
<body>
<h1>Hangman Game</h1>
<?php echo $content; ?>
</body>
</html>