-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.sql
282 lines (231 loc) · 6.22 KB
/
Game.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
USE [TSQLWordle];
GO
CREATE OR ALTER PROCEDURE dbo.WordGuess
/*
Script : Game.sql
Procedure : dbo.WordGuess
Purpose : T-SQL stored procedure for playing Wordle in T-SQL
Date Created : 10 January 2022
Description : Popular word game called Wordle in T-SQL
for Microsoft SQL Server 2017+
Based on https://powerlanguage.co.uk/wordle/
Author : Tomaz Kastrun (Twitter: @tomaz_tsql)
(Github: github.com\tomaztk)
Parameters : Two input parameters
@lang -- defines language, thesaurus and keyboard
@guess -- 5-letter word for guessing
Output :
Result of the game:
Table: dbo.TempTable - game play and tries
Table: dbo.TempKeyboard - coloured used keys
Usage:
EXEC dbo.WordGuess
@lang='EN'
--,@guess='aabbb' --WRONG GUESS
,@guess = 'right'
--,@guess = 'hotel'
--,@guess = 'agent' -- 'hotel'
--,@guess = 'smile'
Colour denoting in first output (gameplay):
' A ' is gray
{{ A }} is Yellow
[[ A ]] is Green
Keyboard denoting in second output (keyboard):
# is grayed-out
{{ A }} is Yellow
[[ A ]] is Green
*/
@lang char(3)
,@guess NVARCHAR(10)
AS
BEGIN
-- check if the word exists / is legitt :)
IF (SELECT COUNT(*) FROM [dbo].[Words] where word = @guess AND lang = @lang) = 0
BEGIN
SELECT 'Wrong word!' AS [Message from the Game]
RETURN
END
-- create table and generate secret
IF (OBJECT_ID(N'dbo.tempTable')) IS NULL
BEGIN
-- DROP TABLE IF EXISTS dbo.tempTable
CREATE TABLE dbo.tempTable (id int identity(0,1), secrets NVARCHAR(10), nof_guess INT, guess_word NVARCHAR(100), valid INT NULL)
DECLARE @secret NVARCHAR(10) = (SELECT top 1 word from dbo.words WHERE lang= @lang ORDER By newid())
INSERT INTO dbo.tempTable (secrets, nof_guess,guess_word, valid)
SELECT
@secret AS secrets
,0 AS nof_guess
,null AS guess_word
,1 AS valid -- as valid word
END
-- create table for temp keyboard
IF (OBJECT_ID(N'dbo.tempKeyboard')) IS NULL
BEGIN
CREATE TABLE dbo.tempKeyboard (Krow INT, Kkey NVARCHAR(100))
INSERT INTO dbo.tempKeyboard (Krow, Kkey)
SELECT
--id
Krow
,Kkey
FROM dbo.Keyboard
WHERE
lang = @lang
END
-- guessing part
DECLARE @nof_guess INT = (SELECT MAX(nof_guess) FROM tempTable)
IF @nof_guess < 6
BEGIN
/*
ADD part for determing colours
*/
DROP TABLE IF EXISTS #tt
DECLARE @guess_sol NVARCHAR(100) = ''
declare @guess_sol2 nvarchar(100) = ''
SET @secret = (SELECT secrets FROM dbo.TempTable WHERE nof_guess = 0)
;WITH sec AS (
SELECT
SUBSTRING(a.b, val.number+1, 1) AS letter
,ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS RN
FROM
(SELECT @secret AS b) AS a
JOIN [master]..spt_values AS val
ON val.number < LEN(a.b)
WHERE
[type] = 'P'
), gu AS (
SELECT
substring(a.b, val.number+1, 1) AS letter
,row_number() over (order by (select 1)) as RN
FROM
(SELECT @guess AS b) AS a
JOIN [master]..spt_values AS val
ON val.number < len(a.b)
WHERE [type] = 'P'
) ,green AS (
SELECT
gu.letter as gul
,sec.letter as secl
,gu.rn as gurn
FROM gu
JOIN sec
ON gu.rn = sec.rn
AND gu.letter = sec.letter
), yellof AS (
select distinct
g.letter as gul
,g.rn as gurn
from gu as g
cross join sec
where
g.letter = sec.letter
AND g.rn <> sec.rn
AND NOT EXISTS (Select * from green as gg
where gg.gul = g.letter
and gg.gurn = g.rn)
), gray AS (
SELECT
letter as gul
,rn as gurn
FROM gu
WHERE
NOT EXISTS (SELECT * FROM green WHERE gul = gu.letter)
AND NOT EXISTS (SELECT * FROM yellof WHERE gul = gu.letter)
) ,Aaa AS (
SELECT gul AS letter, gurn AS pos , 'green' as col FROM green UNION ALL
SELECT gul AS letter, gurn AS pos , 'yellow' as col FROM yellof UNION
SELECT gul AS letter, gurn AS pos , 'Gray' as col FROM gray
) , final AS (
SELECT
a.letter
,a.col
,CASE
WHEN a.col = 'Gray' THEN ' '' ' +UPPER(a.letter)+ ' '' '
WHEN a.col ='yellow' THEN ' {{ ' +UPPER(a.letter)+ ' }} '
WHEN a.col ='green' THEN ' [[ ' +UPPER(a.letter)+ ' ]] ' END as reco
,a.pos
,g.letter as guess_letter
FROM aaa as a
LEFT JOIN gu as g
ON g.rn = a.pos
)
SELECT *
INTO #tt
From final
SELECT @guess_sol = COALESCE(@guess_sol + ' ', '') + reco
FROM #tt
ORDER BY pos ASC
SELECT @guess_sol2 = COALESCE(@guess_sol2 + ' ,', '') + reco
FROM #tt
ORDER BY pos ASC
-- store results
INSERT INTO dbo.TempTable
SELECT
(SELECT TOP 1 secrets FROM dbo.tempTable) as Secrets
,@nof_guess + 1 aS nof_guess
,@guess_sol
,1 as Valid;
SELECT
nof_guess AS [Try Number:]
,guess_word AS [Guessed Word:]
FROM TempTable
WHERE
ID > 0;
/*
ADD part for keyboard denotation
*/
DROP TABLE IF EXISTS #tt2
SELECT
kkey
,krow
,ROW_NUMBER() OVER (ORDER BY krow) AS rn
,TRIM([value]) AS [value]
into #tt2
from dbo.tempkeyboard
CROSS APPLY string_split(kkey, ';')
DROP TABLE IF EXISTS #aa
SELECT
[value]
,TRIM(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE([value], ' [[ ',''), ' ]] ',''),' {{ ',''),' }} ',''), ' '' ','')) AS kak
INTO #aa
FROM STRING_SPLIT(@guess_sol2, ',')
WHERE
[value] <> ''
-- updating values
UPDATE t
SET
t.[value] = a.[value]
FROM #tt2 AS t
JOIN #aa AS a
ON a.kak = t.[value]
UPDATE #tt2
SET
[value] = '#'
WHERE
[value] LIKE ' ''% '
-- Creating update keyboard outlook
DROP TABLE IF EXISTS dbo.tempKeyboard
SELECT
krow
,STRING_AGG([value], '; ') AS kkey
INTO dbo.tempKeyboard
FROM #tt2
GROUP BY krow
ORDER BY krow asc
-- Output the keyboard
SELECT * FROM dbo.tempKeyboard
END
DECLARE @nof_guess2 INT = (SELECT MAX(nof_guess) FROM dbo.tempTable)
IF @nof_guess2 = 6
BEGIN
SELECT 'End' AS [Message from the Game]
DROP TABLE IF EXISTS dbo.TempTable;
DROP TABLE IF EXISTS dbo.tempKeyboard;
END
IF (UPPER(@secret) = (@guess))
BEGIN
SELECT 'Yees, Won!' AS [Message from the Game]
DROP TABLE IF EXISTS dbo.TempTable;
DROP TABLE IF EXISTS dbo.tempKeyboard;
END
END;
GO