收藏本站 
广告服务 
网站地图 
>> 我们从网络和杂志上收集了近100000余篇各类电脑技术、网络技术、软件技术等方面的文章教程,我们的收录原则:不是精华拒不收录!
先飞电脑技术网技术文章
SQL模拟达芬奇密码中文站首页数字游戏
[ 作者:茫然的风    转贴自:网络转载    阅读次数:25    更新时间:2007-6-22 14:30:00   录入:刘光勇 ]        
    本文用SQL模拟在达分奇密码游戏。

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

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

--初始化

if charindex(@sp,@intarray)=0begin  
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

--轮回游戏
elsebegin

--判断游戏是否结束  
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 '胜败乃兵家常事,大侠请重新来过!' 
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 s
et @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=1union allselect 
目标数=null,你输入,达芬奇,
下次输入=case when charindex(','+目标数+',',','+下次输入+',')=0 
then 下次输入 else  left(left(下次输入,charindex
(','+目标数+',',','+下次输入+',')
+len(目标数)),  len(left(下次输入,charindex
(','+目标数+',',','+下次输入+',')
+len(目标数)))-  charindex(',',reverse(left(下次输入,
charindex(','+目标数+',',','+下次输入+',')+len(目标数)))))  
endfrom (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 ##twhere id>1group 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  ,qfrom ##t*/

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

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

上一篇:防止插入删除表造成的数据库死锁  下一篇: 用Sp_lock诊断SQL Sever的性能问题  
网站主页 | 收藏本页 | 联系我们 | 广告服务 | 站点地图 | 会员注册 | 招聘信息 | 内容指正

联系QQ:先飞电脑技术网站事务联系QQ,点击可以直接留言. 32933427 电话:13710542091 [世界排名] 鄂ICP备05005890号