Getting started with T-SQL programming Mini-Games
The basics of T-SQL programming include declaring variables, if judgments, while loops, and using some basic functions.
Remember when in school, wrote a two people to fight the text output game.
On the Code
Alter procUsp_paly asDeclare @paly1 nvarchar( -),@paly2 nvarchar( -)Declare @atk1 int,@atk2 intDeclare @hp1 int,@hp2 intSet @paly1 ='Mystic Arab'Set @paly2 ='Lu BU'Set @hp1 = +;Set @hp2 = +;Declare @rand int,@times int;Set @times = 0;Set @rand = 0; while @hp1 > 0 and @hp2> 0 begin Set @times = @times+1; Print 'Section'+ cast(@times as varchar(Ten))+'rounds' Set @rand = RAND()* -;--100 in random numbers, every turn is better than seeing who shoots first. --Both sides attack, each time randomly Set @atk1 = cast(RAND()* - as int)+Ten;--attack 10 to Set @atk2 = cast(RAND()* + as int)+ +;--attack 40 to if @rand> - begin --turn-based, one-man round, player 1 attacking player 2 Set @hp2 = @hp2 -@atk1; Print @paly1 +'Attack'+@paly2+' , '+@paly2+'Damage'+cast(@atk1 as varchar(Ten)) --Judging if @paly2 's dead. if @hp2<=0 begin Print @paly1 +'Victory!' Break; End --Player 2 attacking player 2 not dead Set @hp1 = @hp1 -@atk2; Print @paly2 +'Attack'+@paly1+' , '+@paly1+'Damage'+cast(@atk2 as varchar(Ten)) --Judging if @paly2 's dead. if @hp1<=0 begin Print @paly2 +'Victory!' Break; End End Else begin --Player 2 attacking player 1 not dead Set @hp1 = @hp1 -@atk2; Print @paly2 +'Attack'+@paly1+' , '+@paly1+'Damage'+cast(@atk2 as varchar(Ten)) --Judging if @paly2 's dead. if @hp1<=0 begin Print @paly2 +'Victory!' Break; End --turn-based, one-man round, player 1 attacking player 2 Set @hp2 = @hp2 -@atk1; Print @paly1 +'Attack'+@paly2+' , '+@paly2+'Damage'+cast(@atk1 as varchar(Ten)) --Judging if @paly2 's dead. if @hp2<=0 begin Print @paly1 +'Victory!' Break; End End Print '-----------------------------------------' End Go
Operating effects at a glance
nice!
T-SQL Programming basics