一个简单的猜单词的小游戏

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

忘了从哪看到的这游戏,睡觉前想起来就立马写了个,
没什么好的思路,样式什么的也就没追求了,
用的SESSION配合AJAX弄的,
小码农,求建议指导,
PS:没怎么优化,勿喷哈,
直接复制本地就能运行了,

一个简单的猜单词的小游戏_第1张图片

view source print ?
001.
002. session_start();
003. header("Content-type:text/html;charset=utf-8");
004. $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
005. function get_word(){
006. $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 following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 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 permission. 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 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 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.";
007. $words=preg_split("/[\s,]+/",$wordtext);
008. do{
009. $i=rand(0,count($words)-1);
010. $word=strtoupper($words[$i]);
011. }while(strlen($word)<5 || !ctype_alpha($word));
012. return $word;
013. }
014. function guess($word){
015. return str_repeat('_',strlen($word));
016. }
017. function output($word){
018. $str='';
019. for($i=0;$i
020. $str.=$word[$i]." ";
021. }
022. return rtrim($str);
023. }
024. if(isset($_GET['op']) && $_GET['op']=='start'){
025. $k=$_GET['k'];
026. $_SESSION['num']=$k;
027. exit;
028. }
029. if(isset($_GET['restart']) && $_GET['restart']==1){
030. session_unset();
031. header("location:$url");
032. exit;
033. }
034. if(!isset($_SESSION['word'])){
035. $word=get_word();
036. $_SESSION['word']=$word;
037. }else{
038. $word=$_SESSION['word'];
039. }
040. $guessguess_word=guess($word);
041. if(isset($_GET['op']) && $_GET['op']=='ajax'){
042. $k=$_GET['k'];
043. $re='';
044. if(!isset($_SESSION['already'])){
045. $_SESSION['already']=guess($_SESSION['word']);
046. }
047. if(!isset($_SESSION['count'])){
048. $_SESSION['count']=0;
049. }
050. if(strpos($_SESSION['word'],$k)!==false){
051. for($i=0;$i
052. if($_SESSION['word'][$i]!=$k){
053. $re.='_';
054. }else{
055. $re.=$_SESSION['word'][$i];
056. }
057. }
058. for($i=0;$i
059. if($_SESSION['already'][$i]=='_'){
060. $_SESSION['already'][$i]=$re[$i];
061. }
062. }
063. }else{
064. $_SESSION['count']+=1;
065. }
066. $return=output($_SESSION['already']);
067. if($_SESSION['count'] <= $_SESSION['num']){
068. if($_SESSION['already']==$_SESSION['word']){
069. $return.="|1";
070. }else{
071. $return.="|0";
072. }
073. }else{
074. $return.="|2";
075. }
076. echo $return;
077. exit;
078. }
079. ?>
080.
133.
155.
156. if(!isset($_SESSION['num'])){
157. echo ' easy: wrong 10
';
158. echo ' normal: wrong 5
';
159. echo ' hard: wrong 3
';
160. }else{
161. //echo $word;
162. echo "最多可以猜错 ".$_SESSION['num']." 次";
163. echo "
";
164. echo "";
165. echo output($guess_word);
166. echo "
";
167. echo '
';
168. echo '';
169. for($i=ord('A');$i<=ord('Z');++$i){
170. echo "\n";
171. $letter=chr($i);
172. echo ''.$letter.'';
173. }
174. echo '
';
175. echo '
';
176. echo '
';
177. echo '
';
178. echo '
';
179. echo '
';
180. echo '重新开始';
181. }
182. ?>

转载于:https://my.oschina.net/u/1259981/blog/166780

你可能感兴趣的:(一个简单的猜单词的小游戏)