Hangman Game Demo in Yii package


<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(); ?>

If you downloaded Yii Framework, you have this code already. It’s in yii\demo\hangman\protected\views\game directory. Yii can be any name you chose for yii framework folder in your computer.

This is the play.php in view directory, it’s the default view for this demo.

My question is what is the $error variable in the above code? I couldn’t find $error mentioned used anywhere else in this demo.

I know I must miss sth, please help me point it out. Thank you!

The first helloword demo is good for me, but in the Hangman demo, the difficulty jumps up to roof!

Nicolas

If you look at the GameController you will see this in the function actionPlay():




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);

		}

	}



This basically means that if you send a POST array to the Controller it will check if the ‘level’ parameter of the received POST array is set. If this is the case everything works as expected. If this parameter isn’t set it will jump to the second condition and will check if there even IS a POST request (even if ‘level’ is not set). If this is the case the user obviously tried to start a game but didn’t specify a level so the variable $error will be set to true and therefore the condition in the view play.php:


if($error)

is true and the code beneath will be executed (in this case displaying the error message)

P.S.: Don’t mind if you have a hard time following someone else’s code. Programming actually is the expression of thoughts and thinking like another person is always difficult :)

I see… The problem I had is the $params. I didn’t understand that by passing an associative array to render(). The the 1st string value in the pair value in the associative array will be available as individual variable in view.

Thanks,Haensel!

I hope they can put my above paragraph in the Class Reference.

It’s already there. Have a look at CController::render parameters description:




$data 	array 	data to be extracted into PHP variables and made available to the view script



yes, there are. But I failed to understand its meaning…