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下intval()和(int)转换使用与区别
Jul 18 PHP
PHP中通过加号合并数组的一个简单方法分享
Jan 27 PHP
PHP中防止SQL注入实现代码
Feb 19 PHP
php中mysql模块部分功能的简单封装
Sep 30 PHP
PHP整数取余返回负数的相关解决方法
May 15 PHP
教你如何解密 “ PHP 神盾解密工具 ”
Jun 20 PHP
thinkphp3.0输出重复两次的解决方法
Dec 19 PHP
浅谈php冒泡排序
Dec 30 PHP
YII Framework教程之异常处理详解
Mar 14 PHP
解析 thinkphp 框架中的部分方法
May 07 PHP
Laravel框架集成UEditor编辑器的方法图文与实例详解
Apr 17 PHP
PHP实现字符串的全排列详解
Apr 24 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大小写问题:函数名和类名不区分,变量名区分
2013/06/17 PHP
php header功能的使用
2013/10/28 PHP
百度地图API使用方法详解
2015/08/25 PHP
img的onload的另类用法
2008/01/10 Javascript
让innerText在firefox火狐和IE浏览器都能用的写法
2011/05/14 Javascript
js中方法重载如何实现?以及函数的参数问题
2013/08/01 Javascript
js处理json以及字符串的比较等常用操作
2013/09/08 Javascript
javascript中的throttle和debounce浅析
2014/06/06 Javascript
轻松创建nodejs服务器(10):处理POST请求
2014/12/18 NodeJs
Jquery树插件zTree用法入门教程
2015/02/17 Javascript
简单理解js的冒泡排序
2016/12/19 Javascript
详解vue前后台数据交互vue-resource文档
2017/07/19 Javascript
Node.js使用Express.Router的方法
2017/11/14 Javascript
Vue中的vue-resource示例详解
2018/11/02 Javascript
Vue项目引发的「过滤器」使用教程
2019/03/12 Javascript
Vue组件模板及组件互相引用代码实例
2020/03/11 Javascript
JavaScript TAB栏切换效果的示例
2020/11/05 Javascript
python文件的md5加密方法
2016/04/06 Python
浅析使用Python操作文件
2017/07/31 Python
Python中的pack和unpack的使用
2018/03/12 Python
python脚本实现验证码识别
2018/06/07 Python
Python编程flask使用页面模版的方法
2018/12/28 Python
Python操作SQLite/MySQL/LMDB数据库的方法
2019/11/07 Python
Mac 使用python3的matplot画图不显示的解决
2019/11/23 Python
Python文件读写w+和r+区别解析
2020/03/26 Python
使用Dajngo 通过代码添加xadmin用户和权限(组)
2020/07/03 Python
python hmac模块验证客户端的合法性
2020/11/07 Python
html5 sessionStorage会话存储_动力节点Java学院整理
2017/07/06 HTML / CSS
html5中嵌入视频自动播放的问题解决
2020/05/25 HTML / CSS
swtich是否能作用在byte上,是否能作用在long上,是否能作用在String上?
2013/03/30 面试题
新学期国旗下演讲稿
2014/05/08 职场文书
社会工作专业求职信
2014/07/15 职场文书
个人租房协议书样本
2014/10/01 职场文书
健康状况证明模板
2014/10/23 职场文书
防止web项目中的SQL注入
2021/12/06 MySQL
Python何绘制带有背景色块的折线图
2022/04/23 Python