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 相关文章推荐
smarty+adodb+部分自定义类的php开发模式
Dec 31 PHP
新手学PHP之数据库操作详解及乱码解决!
Jan 02 PHP
PHP获取当前文件所在目录 getcwd()函数
May 13 PHP
php下通过curl抓取yahoo boss 搜索结果的实现代码
Jun 10 PHP
国外PHP程序员的13个好习惯小结
Feb 20 PHP
PHP 只允许指定IP访问(允许*号通配符过滤IP)
Jul 08 PHP
PHP strtotime函数用法、实现原理和源码分析
Feb 04 PHP
PHP数组操作类实例
Jul 11 PHP
ThinkPHP3.2.3实现分页的方法详解
Jun 03 PHP
php微信公众号开发模式详解
Nov 28 PHP
Laravel中encrypt和decrypt的实现方法
Sep 24 PHP
laravel中的一些简单实用功能
Nov 03 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
PHP DataGrid 实现代码
2009/08/12 PHP
php获取后台Job管理的实现代码
2011/06/10 PHP
php使用curl检测网页是否被百度收录的示例分享
2014/01/31 PHP
php有道翻译api调用方法实例
2014/12/22 PHP
php打造智能化的柱状图程序,用于报表等
2015/06/19 PHP
深入理解Yii2.0乐观锁与悲观锁的原理与使用
2017/07/26 PHP
禁止直接访问php文件代码分享
2020/05/05 PHP
JQuery模板插件 jquery.tmpl 动态ajax扩展
2011/11/10 Javascript
浅谈javascript六种数据类型以及特殊注意点
2013/12/20 Javascript
jQuery操作表格(table)的常用方法、技巧汇总
2014/04/12 Javascript
jquery获得同源iframe内body下标签的值的方法
2014/09/25 Javascript
使用console进行性能测试
2015/04/27 Javascript
JavaScript中字符串拼接的基本方法
2015/07/07 Javascript
jQuery实现弹出带遮罩层的居中浮动窗口效果
2016/09/12 Javascript
详解js的事件代理(委托)
2016/12/22 Javascript
JS实现二叉查找树的建立以及一些遍历方法实现
2017/04/17 Javascript
vue2 v-model/v-text 中使用过滤器的方法示例
2019/05/09 Javascript
python使用心得之获得github代码库列表
2014/06/25 Python
Python中的对象,方法,类,实例,函数用法分析
2015/01/15 Python
Python实现删除Android工程中的冗余字符串
2015/01/19 Python
Python实现豆瓣图片下载的方法
2015/05/25 Python
python动态参数用法实例分析
2015/05/25 Python
详解利用Python scipy.signal.filtfilt() 实现信号滤波
2019/06/05 Python
python操作文件的参数整理
2019/06/11 Python
python3.5 cv2 获取视频特定帧生成jpg图片
2019/08/28 Python
python urllib爬虫模块使用解析
2019/09/05 Python
pandas和spark dataframe互相转换实例详解
2020/02/18 Python
英国高档百货连锁店:John Lewis
2017/11/20 全球购物
Perfume’s Club英国官网:购买香水和护肤品
2019/11/02 全球购物
教学改革实施方案
2014/03/31 职场文书
电子信息工程专业求职信
2014/06/28 职场文书
2014年学生会工作总结
2014/11/07 职场文书
教师辞职信范文
2015/02/28 职场文书
雷锋之歌观后感
2015/06/10 职场文书
python如何获取网络数据
2021/04/11 Python
海贼王十大潜力果实,路飞仅排第十,第一可毁世界(震震果实)
2022/03/18 日漫