php实现猜单词游戏的代码,一个简单的猜单词的小游戏

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

没什么好的思路,样式什么的也就没追求了,

用的SESSION配合AJAX弄的,

小码农,求建议指导,

PS:没怎么优化,勿喷哈,

直接复制本地就能运行了,

view sourceprint?

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("www.sm136.com",$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']www.sm136.com[$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.

081.

function Ajax(recvType){

082.

var aj=new Object();

083.

aj.recvType=recvType ?

recvType.toUpperCase() : 'HTML';

084.

aj.targetUrl='';

085.

aj.sendString='';

086.

aj.resultHandle=null;

087.

aj.createXMLHttpRequest=function(){

088.

var xmlHttp = false;

089.

if(window.XMLHttpRequest){

090.

xmlHttp = new XMLHttpRequest();

091.

}else if(window.ActiveXObject){

092.

try{

093.

xmlHttp = new

ActiveXObject("Msxml2.XMLHTTP");

094.

}catch(error1){

095.

try{

096.

xmlHttp = new

ActiveXobject("Microsoft.XMLHttp");

097.

}catch(error2){

098.

xmlHttp = false;

099.

}

100.

}

101.

}

102.

return xmlHttp;

103.

}

104.

aj.XMLHttpRequest=aj.createXMLHttpRequest();

105.

aj.processHandle=function(){

106.

if(aj.XMLHttpRequest.readyState == 4){

107.

if(aj.XMLHttpRequest.status == 200){

108.

if(aj.recvType=="HTML"){

109.

aj.resultHandle(aj.XMLHttpRequest.responseText);

110.

}else if(aj.recvType=="XML"){

111.

aj.resultHandle(aj.XMLHttpRequest.responseXML);

112.

}

113.

}

114.

}

115.

}

116.

aj.get=function(targetUrl, resultHandle){

117.

aj.targetUrl=targetUrl;

118.

if(resultHandle!=null){

119.

aj.XMLHttpRequest.onreadystatechange=aj.processHandle;

120.

aj.resultHandle=resultHandle;

121.

}

122.

if(window.XMLHttpRequest){

123.

aj.XMLHttpRequest.open("get",

aj.targetUrl);

124.

aj.XMLHttpRequest.send(null);

125.

}else{

126.

aj.XMLHttpRequest.open("get",

aj.targetUrl, true);

127.

aj.XMLHttpRequest.send();

128.

}

129.

}

130.

return aj;

131.

}

132.

133.

134.

var ajax=Ajax();

135.

function select(v){

136.

document.getElementById(v).style.display='none';

137.

ajax.get("guess.php?op=ajax&k="+v,

function(r){

138.

var t=r.split("|");

139.

document.getElementById('word').innerHTML=t[0];

140.

if(t[1] == 1){

141.

document.getElementById('select').style.display='none';

142.

document.getElementById('result').innerHTML='成功';

143.

}else if(t[1] == 2){

144.

document.getElementById('select').style.display='none';

145.

document.getElementById('result').innerHTML='失败';

146.

}

147.

});

148.

}

149.

function check(v){

150.

ajax.get("guess.php?op=start&k="+v,

function(r){

151.

window.location.href="<?php echo

$url;?>";

152.

});

153.

}

154.

155.

156.

if(!isset($_SESSION['num'])){

157.

echo '

οnclick="check(10);" /> easy: wrong 10
';

158.

echo '

οnclick="check(5);" /> normal: wrong 5
';

159.

echo '

οnclick="check(3);" /> 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 '

style="display:">

οnclick="select(\''.$letter.'\');">'.$letter.'

';

173.

}

174.

echo '

';

175.

echo '
';

176.

echo '
';

177.

echo '

id="result">

';

178.

echo '
';

179.

echo '
';

180.

echo '

href="'.$url.'?restart=1">重新开始

';

181.

}

182.

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值