/yii/demos/hangman/protected/controllers/GameController.php

https://github.com/joshuaswarren/weatherhub · PHP · 222 lines · 119 code · 17 blank · 86 comment · 9 complexity · 4f8cbba540b6eac4e52b7d9108f4e8b3 MD5 · raw file

  1. <?php
  2. /**
  3. * GameController class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2011 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * GameController implements the {@link http://en.wikipedia.org/wiki/Hangman_(game) Hangman game}.
  12. *
  13. * @author Qiang Xue <qiang.xue@gmail.com>
  14. * @version $Id: CController.php 131 2008-11-02 01:32:57Z qiang.xue $
  15. * @package demos.hangman
  16. * @since 1.0
  17. */
  18. class GameController extends CController
  19. {
  20. /**
  21. * @var string sets the default action to be 'play'
  22. */
  23. public $defaultAction='play';
  24. /**
  25. * The 'play' action.
  26. * In this action, users are asked to choose a difficulty level
  27. * of the game.
  28. */
  29. public function actionPlay()
  30. {
  31. static $levels=array(
  32. '10'=>'Easy game; you are allowed 10 misses.',
  33. '5'=>'Medium game; you are allowed 5 misses.',
  34. '3'=>'Hard game; you are allowed 3 misses.',
  35. );
  36. // if a difficulty level is correctly chosen
  37. if(isset($_POST['level']) && isset($levels[$_POST['level']]))
  38. {
  39. $this->word=$this->generateWord();
  40. $this->guessWord=str_repeat('_',strlen($this->word));
  41. $this->level=$_POST['level'];
  42. $this->misses=0;
  43. $this->setPageState('guessed',null);
  44. // show the guess page
  45. $this->render('guess');
  46. }
  47. else
  48. {
  49. $params=array(
  50. 'levels'=>$levels,
  51. // if this is a POST request, it means the level is not chosen
  52. 'error'=>Yii::app()->request->isPostRequest,
  53. );
  54. // show the difficulty level page
  55. $this->render('play',$params);
  56. }
  57. }
  58. /**
  59. * The 'guess' action.
  60. * This action is invoked each time when the user makes a guess.
  61. */
  62. public function actionGuess()
  63. {
  64. // check to see if the letter is guessed correctly
  65. if(isset($_GET['g'][0]) && ($result=$this->guess($_GET['g'][0]))!==null)
  66. $this->render($result ? 'win' : 'lose');
  67. else // the letter is guessed correctly, but not win yet
  68. {
  69. $guessed=$this->getPageState('guessed',array());
  70. $guessed[$_GET['g'][0]]=true;
  71. $this->setPageState('guessed',$guessed,array());
  72. $this->render('guess');
  73. }
  74. }
  75. /**
  76. * The 'guess' action.
  77. * This action is invoked when the user gives up the game.
  78. */
  79. public function actionGiveup()
  80. {
  81. $this->render('lose');
  82. }
  83. /**
  84. * Checks to see if a letter is already guessed.
  85. * @param string the letter
  86. * @return boolean whether the letter is already guessed.
  87. */
  88. public function isGuessed($letter)
  89. {
  90. $guessed=$this->getPageState('guessed',array());
  91. return isset($guessed[$letter]);
  92. }
  93. /**
  94. * Generates a word to be guessed.
  95. * @return string the word to be guessed
  96. */
  97. protected function generateWord()
  98. {
  99. $wordFile=dirname(__FILE__).'/words.txt';
  100. $words=preg_split("/[\s,]+/",file_get_contents($wordFile));
  101. do
  102. {
  103. $i=rand(0,count($words)-1);
  104. $word=$words[$i];
  105. } while(strlen($word)<5 || !ctype_alpha($word));
  106. return strtoupper($word);
  107. }
  108. /**
  109. * Checks to see if a letter is guessed correctly.
  110. * @param string the letter
  111. * @return mixed true if the word is guessed correctly, false
  112. * if the user has used up all guesses and the word is guessed
  113. * incorrectly, and null if the letter is guessed correctly but
  114. * the whole word is guessed correctly yet.
  115. */
  116. protected function guess($letter)
  117. {
  118. $word=$this->word;
  119. $guessWord=$this->guessWord;
  120. $pos=0;
  121. $success=false;
  122. while(($pos=strpos($word,$letter,$pos))!==false)
  123. {
  124. $guessWord[$pos]=$letter;
  125. $success=true;
  126. $pos++;
  127. }
  128. if($success)
  129. {
  130. $this->guessWord=$guessWord;
  131. if($guessWord===$word)
  132. return true;
  133. }
  134. else
  135. {
  136. $this->misses++;
  137. if($this->misses>=$this->level)
  138. return false;
  139. }
  140. }
  141. /**
  142. * @return integer the difficulty level. This value is persistent
  143. * during the whole game session.
  144. */
  145. public function getLevel()
  146. {
  147. return $this->getPageState('level');
  148. }
  149. /**
  150. * @param integer the difficulty level. This value is persistent
  151. * during the whole game session.
  152. */
  153. public function setLevel($value)
  154. {
  155. $this->setPageState('level',$value);
  156. }
  157. /**
  158. * @return string the word to be guessed. This value is persistent
  159. * during the whole game session.
  160. */
  161. public function getWord()
  162. {
  163. return $this->getPageState('word');
  164. }
  165. /**
  166. * @param string the word to be guessed. This value is persistent
  167. * during the whole game session.
  168. */
  169. public function setWord($value)
  170. {
  171. $this->setPageState('word',$value);
  172. }
  173. /**
  174. * @return string the word being guessed. This value is persistent
  175. * during the whole game session.
  176. */
  177. public function getGuessWord()
  178. {
  179. return $this->getPageState('guessWord');
  180. }
  181. /**
  182. * @param string the word being guessed. This value is persistent
  183. * during the whole game session.
  184. */
  185. public function setGuessWord($value)
  186. {
  187. $this->setPageState('guessWord',$value);
  188. }
  189. /**
  190. * @return integer the number of misses. This value is persistent
  191. * during the whole game session.
  192. */
  193. public function getMisses()
  194. {
  195. return $this->getPageState('misses');
  196. }
  197. /**
  198. * @param integer the number of misses. This value is persistent
  199. * during the whole game session.
  200. */
  201. public function setMisses($value)
  202. {
  203. $this->setPageState('misses',$value);
  204. }
  205. }