PHP guessing word game _php instance

Source: Internet
Author: User
Tags ord php programming

Direct Copy Local run is OK

<?php session_start ();

Header ("content-type:text/html;charset=utf-");

$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 perm Itted provided that 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 n Otice, this list of conditions and the following disclaimer in the documentation and/or other materials with the Distribution neither the name of the Yii Software LLC nor the names of it contributors may is used to endorse or promote PROD Ucts derived from this software without specific prior written permission this software was provided by the COPYRIGHT Holde RS and CONTRIBUTORS as are 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 is liable F or any DIRECT, INDIRECT, incidental, SPECIAL, exemplary, or consequential damages (including, BUT not LIMITED to, Procurem ENT 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 (I ncluding negligence OR otherwise) arising in no WAY out of the "use of" this SOFTWARE, even IF advised of the possibility O

F SUCH DAMAGE ";

$words =preg_split ("/[\s,]+/", $wordtext);

do{$i =rand (, Count ($words)-);

$word =strtoupper ($words [$i]);

}while (strlen ($word) < | | |!ctype_alpha ($WORD));

return $word;

function Guess ($word) {return Str_repeat (' _ ', strlen ($word));

function output ($word) {$str = ';

for ($i =; $i <strlen ($word); $i + +) {$str = $word [$i]  ;

return RTrim ($STR); } if (Isset $_get[' op ') && $_get[' op ']== ' start ') {$k =$_get[' K '];

$_session[' num ']= $k;

Exit

} if (Isset ($_get[' restart ')) && $_get[' restart ']==) {session_unset ();

Header ("Location: $url");

Exit

} if (!isset ($_session[' word ')) {$word =get_word ();

$_session[' word ']= $word;

}else{$word =$_session[' word '];

$guessguess _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 ']=; } if (Strpos $_session[' word ', $k)!==false) {for ($i =; $i <strlen ($_session[' word '); $i + +) {if ($_session[' word '][$ i]!= $k) {$re = ' _ ';}

else{$re =$_session[' word ' [$i];} for ($i =; $i <strlen ($_session[' word '), $i + +) {if ($_session[' already '] [$i]== ' _ ') {$_session[' already '] [$i]= $re [$

I];

}}else{$_session[' count ']+=;

} $return =output ($_session[' already ')); if ($_session[' count '] <= $_session[' num ']) {if ($_session[' already ']==$_session[' word ']) {$return = ' | ';

}else{$return = "|";}}

else{$return = "|";}

Echo $return;

Exit

}?> <script> function Ajax (recvtype) {var aj=new Object (); Ajrecvtype=recvtype?

Recvtypetouppercase (): ' HTML ';

Ajtargeturl= ';

Ajsendstring= ';

Ajresulthandle=null;

Ajcreatexmlhttprequest=function () {var xmlHttp = false; if (windowxmlhttprequest) {xmlHttp = new XMLHttpRequest ();} else if (windowactivexobject) {try{xmlHttp = new ActiveXObject ("Msxmlxmlhttp"); catch (Error) {try{xmlHttp = new ActiveXObject ("Microsoftxmlhttp");}
catch (Error) {xmlHttp = false;}}
return xmlHttp;
} ajxmlhttprequest=ajcreatexmlhttprequest (); Ajprocesshandle=function () {if (ajxmlhttprequestreadystate = =) {if (Ajxmlhttprequeststatus = =) {if (ajrecvtype== "HTML") ) {Ajresulthandle (ajxmlhttprequestresponsetext);}
else if (ajrecvtype== "XML") {Ajresulthandle (ajxmlhttprequestresponsexml);}}
} ajget=function (TargetUrl, resulthandle) {ajtargeturl=targeturl; if (resulthandle!=null) { Ajxmlhttprequestonreadystatechange=Ajprocesshandle;
Ajresulthandle=resulthandle; } if (windowxmlhttprequest) {Ajxmlhttprequestopen ("get", Ajtargeturl); Ajxmlhttprequestsend (null);
else{Ajxmlhttprequestopen ("Get", Ajtargeturl, True); Ajxmlhttprequestsend ();}
return to AJ;
} </script> <script> var ajax=ajax (); function Select (v) {Documentgetelementbyid (v) styledisplay= ' none '; Ajaxget ("guessphp?op=ajax&k=" +v, function (r) {var t=rsplit (' | '); Documentgetelementbyid (' word ') innerhtml=t[]; if (t[] = =) {Documentgetelementbyid (' select ')
Styledisplay= ' None ';
Documentgetelementbyid (' result ') innerhtml= ' success '; }else if (t[] = =) {Documentgetelementbyid (' select ') styledisplay= ' none '; Documentgetelementbyid (' result ') innerhtml= '
Failure ';
}
}); function Check (v) {ajaxget ("guessphp?op=start&k=" +v, function (r) {windowlocationhref= "<?php echo $url;?
> ";
}); } </script> <?php if (!isset ($_session[' num ')) {echo ' <input type= ' radio ' onclick= ' Check (); "/> Easy:
Wrong <br/> '; Echo ' <input type= ' radio ' onclick= "Check ();"/> Normal:wrong <br/> ';
Echo ' <input type= ' Radio "onclick=" Check (); "/> Hard:wrong; <br/>"; }else{//echo $word echo "Can guess the most wrong" $_session[' num '] "times"; echo "<br/>"; echo "<div id= ' word ' >"; Echo Output ($g
Uess_word);
echo "</div>";
echo ' <br/> ';
Echo ' <div id= ' select ' > '; For ($i =ord (' A '); $i <=ord (' Z '); + + $i) {echo \ n; $letter =chr ($i); Echo ' <span id= ' ' $letter ' style= ' Display: ' > <a href= "javascript:void ();" onclick= "Select (\ ' $letter ');"
> ' $letter ' </a></span> ';
Echo ' </div> ';
echo ' <br/> ';
echo ' <br/> ';
Echo ' <div id= ' result ' ></div> ';
echo ' <br/> ';
echo ' <br/> ';
Echo ' <a href= ', ' $url '? restart= ' > Start again </a> ';
 }?>

This is a PHP to realize the guessing word game, I hope you can extrapolate, realize other games, proficient in PHP programming.

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.