A simple small game of Guessing Words

Source: Internet
Author: User

I forgot where I saw this game. I wrote it right before I went to bed,
There is no good idea, and there is no pursuit of style or anything,
The SESSION is used in combination with AJAX,
CAPTCHA farmers, for advice and guidance,
PS: No optimization. Do not spray it,
Copy the local file to run the task,



<? Phpsession_start (); header ("Content-type: text/html; charset = UTF-8"); $ url = 'HTTP ://'. $ _ SERVER ['HTTP _ host']. $ _ SERVER ['php _ SELF ']; function get_word () {$ wordtext = "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 f Ollowing disclaimer. redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclawing imer 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 perm Ission. 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 special purpose are disclaimed. in no event shall the copyright owner or contributors be liable for any direct, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, or consequential damages (INCLU DING, 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. "; $ words = preg_split ("/[\ s,] +/", $ wordtext ); Do {$ I = rand (0, count ($ words)-1); $ word = strtoupper ($ words [$ I]);} while (strlen ($ word) <5 |! Ctype_alpha ($ word); return $ word;} function guess ($ word) {return str_repeat ('_', strlen ($ word);} function output ($ word) {$ str = ''; for ($ I = 0; $ I <strlen ($ word); $ I ++) {$ str. = $ word [$ I]. "& nbsp;" ;}return rtrim ($ str) ;}if (isset ($ _ GET ['op']) & $ _ GET ['op'] = 'start') {$ k = $ _ GET ['K']; $ _ SESSION ['num'] = $ k; exit;} if (isset ($ _ GET ['restart']) & $ _ GET ['restart'] = 1) {session_unset (); header ("location: $ url "); exit;} if (! Isset ($ _ SESSION ['word']) {$ word = get_word (); $ _ SESSION ['word'] = $ word ;} else {$ word =$ _ SESSION ['word'];} $ guess_word = guess ($ word); if (isset ($ _ GET ['Op ']) & $ _ GET ['op'] = 'ajax ') {$ k = $ _ GET ['K']; $ re = ''; if (! Isset ($ _ SESSION ['already']) {$ _ SESSION ['already'] = guess ($ _ SESSION ['word']);} if (! Isset ($ _ SESSION ['Count']) {$ _ SESSION ['Count'] = 0;} if (strpos ($ _ SESSION ['word'], $ k )! = False) {for ($ I = 0; $ I <strlen ($ _ SESSION ['word']); $ I ++) {if ($ _ SESSION ['word'] [$ I]! = $ K) {$ re. = '_';} else {$ re. = $ _ SESSION ['word'] [$ I] ;}} for ($ I = 0; $ I <strlen ($ _ SESSION ['word']); $ I ++) {if ($ _ SESSION ['already'] [$ I] = '_') {$ _ SESSION ['already'] [$ I] = $ re [$ I] ;}} else {$ _ SESSION ['Count'] + = 1 ;} $ return = output ($ _ SESSION ['already']); if ($ _ SESSION ['Count'] <= $ _ SESSION ['num']) {if ($ _ SESSION ['already'] == _ SESSION ['word']) {$ return. = "| 1";} else {$ return. = "| 0" ;}} else {$ return. = "| 2";} echo $ return; exit ;}?> <Script> function Ajax (recvType) {var aj = new Object (); aj. recvType = recvType? RecvType. toUpperCase (): 'html'; aj.tar getUrl = ''; aj. sendString = ''; aj. resultHandle = null; aj. createXMLHttpRequest = function () {var xmlHttp = false; if (window. XMLHttpRequest) {xmlHttp = new XMLHttpRequest ();} else if (window. activeXObject) {try {xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP");} catch (error1) {try {xmlHttp = new ActiveXobject ("Microsoft. XMLHttp ") ;}catch (error2) {xmlHttp = false ;}}} Return xmlHttp;} aj. XMLHttpRequest = aj. createXMLHttpRequest (); aj. processHandle = function () {if (aj. XMLHttpRequest. readyState = 4) {if (aj. XMLHttpRequest. status = 200) {if (aj. recvType = "HTML") {aj. resultHandle (aj. XMLHttpRequest. responseText);} else if (aj. recvType = "XML") {aj. resultHandle (aj. XMLHttpRequest. responseXML) ;}}} aj. get = function (targetUrl, resultHandle) {aj.tar getUrl = targetUrl; if (resu LtHandle! = Null) {aj. XMLHttpRequest. onreadystatechange = aj. processHandle; aj. resultHandle = resultHandle;} if (window. XMLHttpRequest) {aj. XMLHttpRequest. open ("get", aj.tar getUrl); aj. XMLHttpRequest. send (null);} else {aj. XMLHttpRequest. open ("get", aj.tar getUrl, true); aj. XMLHttpRequest. send () ;}return aj ;}</script> <script> var ajax = Ajax (); function select (v) {document. getElementById (v ). style. display = 'none'; ajax. Get ("guess. php? Op = ajax & k = "+ v, function (r) {var t = r. split ("|"); document. getElementById ('word '). innerHTML = t [0]; if (t [1] = 1) {document. getElementById ('select '). style. display = 'none'; document. getElementById ('result '). innerHTML = 'success';} else if (t [1] = 2) {document. getElementById ('select '). style. display = 'none'; document. getElementById ('result '). innerHTML = 'failed' ;}});} function check (v) {ajax. get ("guess. php? Op = start & k = "+ v, function (r) {window. location. href =" <? Php echo $ url;?> ";}) ;}</Script> <? Phpif (! Isset ($ _ SESSION ['num']) {echo '<input type = "radio" onclick = "check (10);"/> easy: wrong 10 <br/> '; echo' <input type = "radio" onclick = "check (5);"/> normal: wrong 5 <br/> '; echo '<input type = "radio" onclick = "check (3);"/> hard: wrong 3 <br/>';} else {// echo $ word; echo "you can guess at most ". $ _ SESSION ['num']. "Times"; echo "<br/>"; echo "<div id = 'word'>"; echo output ($ guess_word); echo "</div> "; echo '<br/>'; echo '<div id = "s Elect "> '; for ($ I = ord ('A'); $ I <= ord ('Z'); ++ $ I) {echo" \ n "; $ letter = chr ($ I); echo '<span id = "'. $ letter. '"style =" display: "> <a href =" javascript: void (0); "onclick =" select (\''. $ letter. '\'); "> '. $ letter. '</a> </span>';} echo '</div>'; echo '<br/> '; echo '<div id = "result"> </div>'; echo '<br/> '; echo '<a href = "'. $ url. '? Restart = 1 "> Start again </a> ';}?>

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.