SQL Simulation Da Vinci Code Chinese Station Home Digital Game

zhaozj2021-02-16  96

Recently, Da Vinci password is hot, and I have seen it from the Internet. I have an interest in the Chinese official website home page. Since I have seen this math problem when I was a child, I suddenly saw a sense of self-interested. So I simulated one with SQL. (See http://davincicode.leiling.com/) 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

- Initialization if Charindex (@ sp, @ Intarray) = 0BEGIN IF Object_ID ('Tempdb .. ## T') is not null Drop Table ## t

If isnumeric (@intarray) = 0 Begin Print 'The input parameter must be a number. 'Return End Create Table ## T (id int I = floor (10 rand () * 30) INSERT INTO ## t (q) VALUES (@i) print' Target: ' Cast (@i as varchar (2)) End

- Rendezvous game Elsebegin - Judgment whether the game ends if Object_id ('Tempdb .. ## t') is null begin print 'This turn is over, heroes please come back! 'Return End

- Verify the user input number legitimacy if ISNUMERIC (Right (@ Intarray, CHARINDEX (@ sp, reverse (@intarray)) - 1) = 0 Begin print 'The input parameter must be a number. 'Return end set @ i = cast (@ 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 'The input parameter must be greater than the current maximum. 'Return End IF @ i-isnull (@ maxq, 1)> 3 Begin Print' The input parameter exceeds the range. 'Return End IF @i> (Select Q from ## T WHERE ID = 1) Begin Print' The input parameter must not be greater than the target number. 'Return End

- Insert user input number INSERT INTO ## t (q) Values ​​(@i) print 'you enter:' cast (@i as varchar (2)) - Judgment wins = (Select Q from # # #t where id = 1) Begin Drop Table ## T Print '----------------------------------- - 'print' victory is nailing family, heroes please come back! 'Return End IF @ i = (Select Q from ## T where id = 1) -1 Begin Drop Table ## T Print' Da Vinci: ' Cast (@ i 1 as varchar (2)) Print' - ---------------- 'Print' Congratulations! You won. 'Return End - Insert Da Vinci 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 @ = @ J set @ J = @ J FLOOR (1 rand () * 3) INSERT INTO ## T Values ​​(@J) Print 'Da Vinci:' Cast (@J As Varchar (2)) End

- The full version shows the number of SELECT targets = Q, you enter = null, Da Vinci = NULL, the next input = '1, 2, 3' from ## Twhere id = 1Union allselect target number = NULL, you type, Da Vinci, next time I entered = Case When Charindex (',' target number ',', ',' Next input ',') = 0 THEN Next time I entered Else Left (LEFT (next input, Charindex (',' target number ',', ',' Next input ',') LEN (target number)), LEN (LEFT (next time, charIndex (',' target number ',', ',' Len (target number)) - Charindex (',', reverse) - Charindex (', ", REVERSE (LEFT (next time, charIndex (', number target number ), ',', ' Next ', ') LEN (Target Number))))) EndFrom (SELECT target number, you type, Da Vinci, next time = Cast (Da Vinci 1 As Varchar (2)) ',' Cast (Da Vinci 2 As Varchar (2)) ',' Cast (Daxi 3 as varchar (2)) from (SELECT target number = Cast (SELECT Q from ## t where id = 1) as varchar (2)), you enter = max (case when id% 2 = 0 Then Q else 0 end), Da Vinci = max (Case When ID% 2 = 1 q else 0 end) from # # tRE ID> 1Group by floor (id / 2))) a) b / * - Simplified version Show select case when id = 1 Then 'target number:' when id% 2 = 0 THEN ' You type: 'when id% 2 = 1 and ID> 1 Then' Da Vinci: 'end, QFrom ## t * /

Game rules: The system randomly generates the number of targets. The two sides start the turn from the number 1, and the numbers must be continuous. Each round can choose 3 numbers, first to the target number is negative.

Game Description: Execute the Exec Davinci '2' start program, where number 2 can be any number, the system generates a random target number. You start to answer, the number of answers is separated by a comma, such as: exec Davinci '2, 1', then the system automatically responds to answer, such as 4. Then start the next round of answers: Exec Davinci '2, 1, 6' .........

转载请注明原文地址:https://www.9cbs.com/read-11718.html

New Post(0)