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 相关文章推荐
我的论坛源代码(六)
Oct 09 PHP
smarty实例教程
Nov 19 PHP
比较全的PHP 会话(session 时间设定)使用入门代码
Jun 05 PHP
PHP字符串的编码问题的详细介绍
Apr 27 PHP
解析PHP正则提取或替换img标记属性
Jun 26 PHP
CI(CodeIgniter)框架中的增删改查操作
Jun 10 PHP
php简单实现多字节字符串翻转的方法
Mar 31 PHP
CodeIgniter使用smtp服务发送html邮件的方法
Jun 10 PHP
PHP代码优化技巧小结
Sep 29 PHP
CI框架文件上传类及图像处理类用法分析
May 18 PHP
PHP中读取文件的几个方法总结(推荐)
Jun 03 PHP
PHP+Ajax实现的无刷新分页功能详解【附demo源码下载】
Jul 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新手上路(三)
2006/10/09 PHP
php escape URL编码
2008/12/10 PHP
作为PHP程序员你要知道的另外一种日志
2018/07/30 PHP
laravel框架上传图片实现实时预览功能
2019/10/14 PHP
jQuery ui 1.7更新小结
2009/08/15 Javascript
jQuery 方法大全方便学习参考
2010/02/25 Javascript
JS 自定义函数缺省值的设置方法
2010/05/05 Javascript
JavaScript高级程序设计(第3版)学习笔记3 js简单数据类型
2012/10/11 Javascript
Extjs grid panel自带滚动条失效的解决方法
2014/09/11 Javascript
Windows系统下使用Sublime搭建nodejs环境
2015/04/13 NodeJs
MVVM模式中ViewModel和View、Model有什么区别?
2015/06/19 Javascript
Angularjs material 实现搜索框功能
2016/03/08 Javascript
JS简单实现表格排序功能示例
2016/12/20 Javascript
超全面的javascript中变量命名规则
2017/02/09 Javascript
在vue中获取dom元素内容的方法
2017/07/10 Javascript
ECMAScript6变量的解构赋值实例详解
2017/09/19 Javascript
Angularjs添加排序查询功能的实例代码
2017/10/24 Javascript
node.js基于express使用websocket的方法
2017/11/09 Javascript
vue实现导航栏效果(选中状态刷新不消失)
2017/12/13 Javascript
详解Vue之父子组件传值
2019/04/01 Javascript
JavaScript面向对象编程小游戏---贪吃蛇代码实例
2019/05/15 Javascript
微信小程序如何访问公众号文章
2019/07/08 Javascript
vue iview实现动态新增和删除
2020/06/17 Javascript
Python字符串和文件操作常用函数分析
2015/04/08 Python
Python的Django框架中自定义模版标签的示例
2015/07/20 Python
Django框架设置cookies与获取cookies操作详解
2019/05/27 Python
使用Keras中的ImageDataGenerator进行批次读图方式
2020/06/17 Python
python中怎么表示空值
2020/06/19 Python
python pyg2plot的原理知识点总结
2021/02/28 Python
CSS3的column-fill属性对齐列内容高度的用法详解
2016/07/01 HTML / CSS
Marc Jacobs彩妆官网:Marc Jacobs Beauty
2017/07/03 全球购物
卡西欧B级产品官方网站:Casio Outlet
2018/05/22 全球购物
Allen Edmonds官方网站:一家美国优质男士鞋类及配饰制造商
2019/03/12 全球购物
业务员年终工作总结2015
2015/05/28 职场文书
2016年6月份红领巾广播稿
2015/12/21 职场文书
JavaScript offset实现鼠标坐标获取和窗口内模块拖动
2021/05/30 Javascript