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 相关文章推荐
初学者入门:细述PHP4的核心Zend
Sep 05 PHP
构建简单的Webmail系统
Oct 09 PHP
php面向对象全攻略 (一) 面向对象基础知识
Sep 30 PHP
PHP+MySQL投票系统的设计和实现分享
Sep 23 PHP
PHPThumb图片处理实例
May 03 PHP
php foreach正序倒序输出示例代码
Jul 01 PHP
php计算两个日期相差天数的方法
Mar 14 PHP
Yii实现显示静态页的方法
Apr 25 PHP
php 如何设置一个严格控制过期时间的session
May 05 PHP
laravel如何开启跨域功能示例详解
Aug 31 PHP
asp.net和php的区别点总结
Oct 10 PHP
PHP反射基础知识回顾
Sep 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
用Socket发送电子邮件(利用需要验证的SMTP服务器)
2006/10/09 PHP
在PWS上安装PHP4.0正式版
2006/10/09 PHP
php switch语句多个值匹配同一代码块的实现
2014/03/03 PHP
PHP获取网页标题的3种实现方法代码实例
2014/04/11 PHP
PHP制作图形验证码代码分享
2014/10/23 PHP
PHP实现笛卡尔积算法的实例讲解
2019/12/22 PHP
extJs 常用到的增,删,改,查操作代码
2009/12/28 Javascript
Jquery下的26个实用小技巧(jQuery tips, tricks &amp; solutions)
2010/03/01 Javascript
jQuery LigerUI 插件介绍及使用之ligerDrag和ligerResizable示例代码打包
2011/04/06 Javascript
使用JavaScript 实现的人脸检测
2015/03/24 Javascript
浅析JavaScript中的变量复制、参数传递和作用域链
2016/01/13 Javascript
jquery制做精致的倒计时特效
2016/06/13 Javascript
JS实现放大、缩小及拖拽图片的方法【可兼容IE、火狐】
2016/08/23 Javascript
AngularJS实现ajax请求的方法
2016/11/22 Javascript
BootStrap selectpicker后台动态绑定数据
2017/06/01 Javascript
Angular ng-animate和ng-cookies用法详解
2018/04/18 Javascript
解决包含在label标签下的checkbox在ie8及以下版本点击事件无效果兼容的问题
2019/10/27 Javascript
python使用scrapy解析js示例
2014/01/23 Python
自己编程中遇到的Python错误和解决方法汇总整理
2015/06/03 Python
Pandas 合并多个Dataframe(merge,concat)的方法
2018/06/08 Python
python3 字符串/列表/元组(str/list/tuple)相互转换方法及join()函数的使用
2019/04/03 Python
django中使用Celery 布式任务队列过程详解
2019/07/29 Python
Django配置MySQL数据库的完整步骤
2019/09/07 Python
分享8点超级有用的Python编程建议(推荐)
2019/10/13 Python
python烟花效果的代码实例
2020/02/25 Python
Snapfish英国:在线照片打印和个性化照片礼品
2017/01/13 全球购物
施华洛世奇日本官网:SWAROVSKI日本
2018/05/04 全球购物
外企求职信范文分享
2013/12/31 职场文书
本科毕业生专业自荐书范文
2014/02/05 职场文书
银行先进个人事迹材料
2014/05/11 职场文书
走群众路线学习笔记
2014/11/06 职场文书
开学第一天的感想
2015/08/10 职场文书
选调生挂职锻炼工作总结
2015/10/23 职场文书
大学宣传委员竞选稿
2015/11/19 职场文书
教师教育心得体会
2016/01/19 职场文书
 Redis 串行生成顺序编码的方法实现
2022/04/03 Redis