php猜单词游戏


Posted in PHP onSeptember 29, 2015

直接复制本地运行就可以了

<?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 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";

$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 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='成功';
}else if(t[] == ){
documentgetElementById('select')styledisplay='none';
documentgetElementById('result')innerHTML='失败';
}
});
}
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 "最多可以猜错 "$_SESSION['num']" 次";
echo "<br />";
echo "<div id='word'>";
echo output($guess_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=">重新开始</a>';
}
?>

这是一款php实现的猜单词游戏,希望大家可以举一反三,实现其他小游戏,熟练掌握php编程。

PHP 相关文章推荐
PHP.MVC的模板标签系统(二)
Sep 05 PHP
海河写的 Discuz论坛帖子调用js的php代码
Aug 23 PHP
php zend解密软件绿色版测试可用
Apr 14 PHP
mac下使用brew配置环境的步骤分享
May 23 PHP
解决php使用异步调用获取数据时出现(错误c00ce56e导致此项操作无法完成)
Jul 03 PHP
php小技巧之过滤ascii控制字符
May 14 PHP
Codeigniter整合Tank Auth权限类库详解
Jun 12 PHP
php遍历目录方法小结
Mar 10 PHP
Zend Framework实现自定义过滤器的方法
Dec 09 PHP
详谈PHP面向对象中常用的关键字和魔术方法
Feb 04 PHP
PHP给前端返回一个JSON对象的实例讲解
May 31 PHP
PHP微信支付结果通知与回调策略分析
Jan 10 PHP
PHP代码优化技巧小结
Sep 29 #PHP
php提取身份证号码中的生日日期以及验证是否为成年人的函数
Sep 29 #PHP
PHP类的封装与继承详解
Sep 29 #PHP
PHP比较运算符的详细介绍
Sep 29 #PHP
php提高网站效率的技巧
Sep 29 #PHP
四个PHP非常实用的功能
Sep 29 #PHP
PHP实现二叉树的深度优先与广度优先遍历方法
Sep 28 #PHP
You might like
星际争霸任务指南——神族
2020/03/04 星际争霸
PHP开发中常用的8个小技巧
2008/08/27 PHP
PHP的array_diff()函数在处理大数组时的效率问题
2011/11/27 PHP
对象失去焦点时自己动提交数据的实现代码
2012/11/06 PHP
PHP curl 或 file_get_contents 获取需要授权页面的方法
2017/05/05 PHP
PHP chr()函数讲解
2019/02/11 PHP
让回调函数 showResponse 也带上参数的代码
2007/08/13 Javascript
单击按钮显示隐藏子菜单经典案例
2013/01/04 Javascript
jquery实现tr元素的上下移动示例代码
2013/12/20 Javascript
JavaScript每天定时更换皮肤样式的方法
2015/07/01 Javascript
基于Arcgis for javascript实现百度地图ABCD marker的效果
2015/09/12 Javascript
13个PHP函数超实用
2015/10/21 Javascript
js实现文字闪烁特效的方法
2015/12/17 Javascript
javascript实现平滑无缝滚动
2020/08/09 Javascript
Javascript数组循环遍历之forEach详解
2016/11/07 Javascript
浅述节点的创建及常见功能的实现
2016/12/15 Javascript
JS实现选定指定HTML元素对象中指定文本内容功能示例
2017/02/13 Javascript
从零学习node.js之express入门(六)
2017/02/25 Javascript
ES5 ES6中Array对象去除重复项的方法总结
2017/04/27 Javascript
更强大的vue ssr实现预取数据的方式
2019/07/19 Javascript
JS实现json数组排序操作实例分析
2019/10/28 Javascript
Python日期时间对象转换为字符串的实例
2018/06/22 Python
PyQt 图解Qt Designer工具的使用方法
2019/08/06 Python
python用quad、dblquad实现一维二维积分的实例详解
2019/11/20 Python
Python 使用threading+Queue实现线程池示例
2019/12/21 Python
python实现用户名密码校验
2020/03/18 Python
Python在线和离线安装第三方库的方法
2020/10/31 Python
香港时尚女装购物网站:ZAFUL
2017/07/19 全球购物
销售人员个人求职信
2013/09/26 职场文书
客户接待方案
2014/02/26 职场文书
老公爱的承诺书
2014/03/31 职场文书
产品设计开发计划书
2014/05/07 职场文书
优秀团员自我评价
2015/03/10 职场文书
亮剑观后感600字
2015/06/05 职场文书
Jackson 反序列化时实现大小写不敏感设置
2021/06/29 Java/Android
Java数组与堆栈相关知识总结
2021/06/29 Java/Android