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 相关文章推荐
漂亮但不安全的CTB
Oct 09 PHP
php 三维饼图的实现代码
Sep 28 PHP
php中选择什么接口(mysql、mysqli)访问mysql
Feb 06 PHP
ThinkPHP3.1新特性之对页面压缩输出的支持
Jun 19 PHP
重新认识php array_merge函数
Aug 31 PHP
PHP答题类应用接口实例
Feb 09 PHP
php简单分页类实现方法
Feb 26 PHP
php实现html标签闭合检测与修复方法
Jul 09 PHP
php获取英文姓名首字母的方法
Jul 13 PHP
织梦sitemap地图实时推送给百度的教程
Aug 03 PHP
php使用json_decode后数字对象转换成了科学计数法的解决方法
Feb 20 PHP
php操作redis常见方法示例【key与value操作】
Apr 14 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
星际争霸, 教主第一视角, ZvT经典龙蛇演义
2020/03/02 星际争霸
php下几个常用的去空、分组、调试数组函数
2009/02/22 PHP
The specified CGI application misbehaved by not returning a complete set of HTTP headers
2011/03/31 PHP
PHP魔术引号所带来的安全问题分析
2014/07/15 PHP
php简单实现多语言切换的方法
2016/05/09 PHP
Laravel搭建后台登录系统步骤详解
2016/07/26 PHP
PHP中overload与override的区别
2017/02/13 PHP
php gethostbyname获取域名ip地址函数详解
2010/01/24 Javascript
从jQuery.camelCase()学习string.replace() 函数学习
2011/09/13 Javascript
jQuery实现随意改变div任意属性的名称和值(部分原生js实现)
2013/05/28 Javascript
JS验证日期的格式YYYY-mm-dd 具体实现
2013/06/29 Javascript
jQuery实现的产品自动360度旋转展示特效源码分享
2015/08/21 Javascript
ionic隐藏tabs的方法
2016/08/29 Javascript
angular-cli修改端口号【angular2】
2017/04/19 Javascript
微信小程序中显示html格式内容的方法
2017/04/25 Javascript
基于JavaScript实现评论框展开和隐藏功能
2017/08/25 Javascript
jQuery中each方法的使用详解
2018/03/18 jQuery
async/await地狱该如何避免详解
2018/05/10 Javascript
vue引入js数字小键盘的实现代码
2018/05/14 Javascript
JS/jQuery实现获取时间的方法及常用类完整示例
2019/03/07 jQuery
vue-router 中 meta的用法详解
2019/11/01 Javascript
Node.js path模块,获取文件后缀名操作
2020/11/07 Javascript
详解微信小程序轨迹回放实现及遇到的坑
2021/02/02 Javascript
python异步任务队列示例
2014/04/01 Python
VTK与Python实现机械臂三维模型可视化详解
2017/12/13 Python
解决Python 中英文混输格式对齐的问题
2018/07/16 Python
详解Django+uwsgi+Nginx上线最佳实战
2019/03/14 Python
pytorch动态网络以及权重共享实例
2020/01/06 Python
特罗佩亚包官方网站:Tropea
2017/01/03 全球购物
Gap工厂店:Gap Factory
2017/11/02 全球购物
生产车间主任的个人自我鉴定
2013/10/25 职场文书
鲜花方阵解说词
2014/02/13 职场文书
运动会广播稿150字
2014/02/19 职场文书
浅谈JS和Nodejs中的事件驱动
2021/05/05 NodeJs
CSS中妙用 drop-shadow 实现线条光影效果
2021/11/11 HTML / CSS
Docker部署Mysql8的实现步骤
2022/07/07 Servers