转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 数据库 >> Sql Server >> 正文
SQL模拟达芬奇密码中文站首页数字游戏         

SQL模拟达芬奇密码中文站首页数字游戏

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1024 更新时间:2007/11/14 11:13:49

近来达芬奇密码热抄,也从网上找来看了,对中文官网首页的游戏产生了兴趣.由于是小时候看过这个数学题,现在突然看到备感亲切.所以用SQL模拟了一个.(中文官方网站见http://davincicode.leiling.com/

create proc davinci (@intarray varchar(1000))
as
--******************
--2004.7
--******************
declare @sp varchar(1)
set @sp='''',''''  

declare @i int,@j int,@maxq int

--初始化
if charindex(@sp,@intarray)=0
begin
   if object_id(''''tempdb..##t'''') is not null
      drop table ##t

   if isnumeric(@intarray)=0
   begin
      print ''''输入参数必须是数字。''''
      return
   end
   create table ##t(id int identity(1,1),q int)
   set @i=floor(10+rand()*30)
   insert into ##t(q) values(@i)
   print ''''目标:''''+cast(@i as varchar(2))
end

--轮回游戏
else
begin
   --判断游戏是否结束
   if object_id(''''tempdb..##t'''') is null
   begin
      print ''''本回合已经结束,大侠请重新来过!''''
      return
   end

   --验证用户输入数合法性
   if isnumeric(right(@intarray,charindex(@sp,reverse(@intarray))-1))=0
   begin
      print ''''输入参数必须是数字。''''
      return
   end
   set @i=cast(right(@intarray,charindex(@sp,reverse(@intarray))-1) as int)
   set @maxq=(select max(q) from ##t where id>1)
   if @i<=isnull(@maxq,0)
   begin
      print ''''输入参数必须大于当前最大数。''''
      return
   end
   if @i-isnull(@maxq,1)>3
   begin
      print ''''输入参数超过范围。''''
      return
   end
   if @i>(select q from ##t where id=1)
   begin
      print ''''输入参数不得大于目标数。''''
      return
   end

   --插入用户输入数
   insert into ##t(q) values(@i)
   print ''''你输入:''''+cast(@i as varchar(2))
 
   --判断胜负
   if @i=(select q from ##t where id=1)
   begin
      drop table ##t
      print ''''-------------------------------------''''
      print ''''胜败乃兵家常事,大侠请重新来过!''''
      return
   end
   if @i=(select q from ##t where id=1)-1
   begin
   drop table ##t
      print ''''达芬奇:''''+cast(@i+1 as varchar(2))
      print ''''-----------------''''
      print ''''恭喜!你获胜了。''''
      return
   end

   --插入达芬奇应答数
   if (select q-1 from ##t where id=1)%4=0
      set @j=(select count(id) from ##t where id%2=1)*4
   else
   begin
      set @j=(select q-1 from ##t where id=1)%4+((select count(id) from ##t where id%2=1)-1)*4
      if @j<(select max(q) from ##t where id>1)
         set @j=@j+4
   end
  
   if @i=@j
      set @j=@j+floor(1+rand()*3)
 
   insert into ##t values(@j)
   print ''''达芬奇:''''+cast(@j as varchar(2))
end

--完整版显示
select 目标数=q,你输入=null,达芬奇=null,下次输入=''''1,2,3'''' from ##t where id=1
union all
select 目标数=null,你输入,达芬奇
   ,下次输入=case when charindex('''',''''+目标数+'''','''','''',''''+下次输入+'''','''')=0 then 下次输入 else
   left(left(下次输入,charindex('''',''''+目标数+'''','''','''',''''+下次输入+'''','''')+len(目标数)),
   len(left(下次输入,charindex('''',''''+目标数+'''','''','''',''''+下次输入+'''','''')+len(目标数)))-
   charindex('''','''',reverse(left(下次输入,charindex('''',''''+目标数+'''','''','''',''''+下次输入+'''','''')+len(目标数)))))
   end
from (
select 目标数,你输入,达芬奇
   ,下次输入=cast(达芬奇+1 as varchar(2))+'''',''''
      +cast(达芬奇+2 as varchar(2))+'''',''''
      +cast(达芬奇+3 as varchar(2))
from (
select 目标数=cast((select q from ##t where id=1) as varchar(2))
   ,你输入=max(case when id%2=0 then q else 0 end)
   ,达芬奇=max(case when id%2=1 then q else 0 end)
from ##t
where id>1
group by floor(id/2)) a) b

/*
--简化版显示
select case when id=1 then ''''目标数:''''
   when id%2=0 then ''''你输入:''''
   when id%2=1 and id>1 then ''''达芬奇:'''' end
   ,q
from ##t
*/

游戏规则:系统随机产生目标数,双方从数字1开始轮回应答,数字必须连续,每回合最多可以选择3个数字,先到目标数者为负。

游戏说明:执行 exec davinci ''''2'''' 开始程序,其中数字2可以为任意数字,系统产生随机目标数。你开始应答,应答数以逗号『,』分隔,如:exec davinci ''''2,1'''',然后系统自动回应答,如4。接着开始下一轮应答:exec davinci ''''2,1,6'''' ………


[办公软件]在sybase中插入图片、PDF、文本文件  [办公软件]安装Sybase ASE
[办公软件]linux指令大全(完整篇)  [办公软件]Linux新手入门常用命令大全
[办公软件]在RedHat Linux 9里安装gaim0.80  [办公软件]浅谈Linux 下Java 1.5 汉字方块问题解决方法
[办公软件]Linux程序员必读:中文化与GB18030标准  [办公软件]linux指令大全
[办公软件]制作Linux启动盘的四种方法  [办公软件]Linux文件系统的反删除方法
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Sql Server  · MySql
    · Access  · ORACLE
    · SyBase  · 其他
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台