PHP Development Framework YII Framework Tutorial (4) Hangman guessing Word game instances

Source: Internet
Author: User
With an example of the previous "Hello,world" and an introduction to the Yii Framework Web Application Foundation, you can begin to introduce a simple and relatively complete Web application-hangman (guess word game), which is released with the Yii development package. This example provides an overview of the basic steps for developing YII applications.

Speaking of "Hangman", reminds me of the end of the 80 's high school CPC464 computer in the end of the "Guess Word game"-hangman, each guessed the wrong time, put a villain away from the gallows step forward. Then dos just came out:-).

Developing a Web application, first for the requirements analysis, is not included in this tutorial, but for completeness, the "Guess Word game" rule is listed below:

Guess the word Game (English: Hangman, "the person who hangs" means) is a two-person game. One player wants one word and the other tries to guess every letter in the word that the player wants.

The words to be guessed are represented by a line of horizontal lines, letting the player know how many letters the word has. If the guessing player guesses one of the letters, the other must write the letter in all places where the letter appears. If the guessed letter does not appear in the word, the other player will draw one of the hanging neck dolls. The game will end in the following situations:

"I want the T-word. "There, in the eighth and 11th place." ”

The guessing player guesses all the letters, or guesses the whole word.

Another player draws the full picture:

The example given today does not draw the "hanging Man", guessed the show "you Win", guess the wrong display "you lose". So we can design four pages:

These four pages face to the YII framework of four view, can be named play, Guess, win,lose, each page shows the title of "Hangman Game", so you can design a "MasterPage", Become a template for layout layouts in Yii for four view shares. The Yii application uses the MVC design pattern, so we can design a controller–>gamecontroller for four view.

The previous tutorial said that YII applications use the default directory structure to hold different parts of the application, and can use the tools provided by Yii to participate in a default project directory. But I personally prefer to create individual catalogs, so according to the above requirements and interface design, you can create a directory structure of the project as follows:

The created gamecontroller.php is placed under the Protected/controller directory.

Created four view guess.php, lose.php, play.php, win.php placed in the Protected/views/game directory. The directory name game corresponds to the gamecontroller used.

The shared layout created is placed in the Protected/views/layout directory with the default name of main.php

The application's configuration file is placed in the Protected/config, and the default profile is main.php

The application's entry script is index.php

In addition, the text file for the guessed word is word.txt

1. First take a look at the configuration file protected/config/main.php

Return Array (' name ' = ' Hangman game ', ' defaultcontroller ' = ' game ', ' Components ' =>array (' urlmanager ' = Array (' urlformat ' = ' path ', ' Rules ' =>array (' game/guess/' = ' game/guess ',),),);

All writable properties of the Cwebapplication application can be defined through a configuration file, we see that the configuration file defines the app's name as "Hangman Game", and then modifies the Web App by default controller name for Game corresponding to Gamecontroller, if Defaultcontroller is not redefined, the default controller name is Sitecontroller so that the view is stored in the Protected/views/site directory. In addition, this Yii application opens the Urlmanager component, the functionality of which is described later, primarily to define the format (routing format) of the URLs that the user can access.

2. With this configuration file, it can be used in the portal script, each YII application's entry script index.php is similar, in most cases copy & Paste

3. Then define the layout file that the view uses protected/views/layout/main.php main.php as the default layout template, the application can modify the layout used by the view, this example is the default layout name of Main.

The layout is basically an HTML file, which acts as the placeholder of the view, that is, when displaying a specific view, such as play.php with play.php content instead of $content. Thus the function of similar "MasterPage" is realized.
4. Below you can define four view, which is not listed here, take play.php as an example:

You can see basically HTML, where cHTML is an auxiliary class supported by the YII framework to help generate HTML code. Hangman is relatively simple, so instead of using a separate model, the parameters are passed in as a render push.

You call Ccontroller::render () by passing the name of the view. This method will look for the corresponding view file in the Protected/views/controllerid directory.

Inside the view script, we can access the controller instance through the $this. We can pull any of the controller's properties in the viewport these drawings in $this->propertyname way.

We can also send data to the view in the following way:

$this->render (' edit ', array (
' Var1 ' = $value 1,
' Var2 ' = $value 2,
));

In the above way, the render () method extracts the second parameter of the array into the variable. The result is that in the view script, we can directly access the variables $var 1 and $var 2.

5. After defining the layout and view, you can write Gamecontroller,

In general, the Controller default action is index, you can modify the default action by $defaultaction, this example is modified to play. So if the URL for this example is http://127.0.0.1:8888/yii/demos/hangman/
Then use http://127.0.0.1:8888/yii/demos/hangman/index.php and use Http://127.0.0.1:8888/yii/demos/hangman/index.php?game The effect of/play is the same. The default controller is Gamecontroller,gamecontroller default action for play.

Action (action), actions can be defined as a method named with the action Word as the prefix. Hangman defines three actions, Actionplay, actionguess, Actiongiveup, Gamecontroller other methods and properties, and generates words to determine whether guessing equivalence is not related to the specific game logic and the YII framework, Don't introduce it.

6. First look at the default playaction, which is the default method called by the user, that is, when the user group address bar Input http://127.0.0.1:8888/yii/demos/hangman/index.php (or http:// 127.0.0.1:8888/yii/demos/hangman/index.php?game/play) The Action that was invoked.

This method defines the three difficulty levels of the game $levels, there are two branches, if the difficulty level is not selected, then call $this->render (' play ', $params), show the play page, $params (Array) push to the corresponding view, protected/views/play.php, refer to the definition of view above:

View uses RadioButton to display a list of $levels definitions.

If the user chooses the difficulty level, it stores levels, words, etc. into the attributes defined by Gamecontroller, such as Word,level. Gamecontroller clapping and Ccontroller is also a subclass of Ccomponent, Ccomponent supports property functions like C#,java. Detailed later introduction.
Then call $this->render (' guess '); Displays the Guess page.
The Guess page guess.php is defined as follows:

The methods and properties of the corresponding controller instance object can be accessed directly through $this in view. such as $this->guessword, $this->isguessed (Chr ($i)) and so on.
Click on the 26-letter trigger Guessaction (' Submit ' =>array (' guess ', ' G ' =>chr ($i))).

7. The following is the definition of guessaction

Where the argument ' G ' is passed by the Guess page, if the words are all guessed to show "you win" or run out of all the times to guess the wrong display "you Lose", $this->render ($result? ' Win ': ' Lose '),
If you still have a chance to guess or return to the Guess page $this->render (' guess ');

8. There is also a "Give up" button on the Guess page, and the user taps trigger giveupaction. This method is relatively simple, directly display the lose page

At this point the hangman game is basically finished. Although the game is simple, but illustrates the use of yii to develop the basic process of application, the following is the development of YII development document to give a process, hangman is relatively simple, no use of databases and internationalization.

The development process here assumes that we have completed the requirements analysis of the application and the necessary design analysis.

Create a directory structure skeleton. You can quickly implement this step by creating the Yiic tool described in the first web App.

Configure this app. This is achieved by modifying the application configuration file. This step may also require writing some application components, such as user components.

Create a model class for each type of data that you manage. The Gii tools described in Creating first YII application and Automatic Code Generation can be used to quickly create an active record class for each data table. 4. Create a controller class for each type of user request. How to classify user requests depends on the actual demand. In general, if a model class needs to be accessed by a user, he should have a corresponding controller class. The Gii tool can also automate this step.

Implement the actions and their corresponding views. This is the work that really needs to be done.

Configure the necessary action filters in the controller class.

If you need a theme feature, create a theme.

If internationalization (i18n) is required, create translation information.

Apply the appropriate caching techniques to the data points and view points that can be cached.

Final adjustment and deployment.

The above is the PHP Development Framework Yii Framework Tutorial (4) Hangman guess the content of the word game instance, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.