-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTower_Hanoi.sql
More file actions
226 lines (170 loc) · 6.25 KB
/
Tower_Hanoi.sql
File metadata and controls
226 lines (170 loc) · 6.25 KB
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
USE [master];
GO
CREATE DATABASE TowerHanoi;
GO
USE TowerHanoi;
GO
CREATE OR ALTER PROCEDURE dbo.INIT_Hanoi
/**************************************************************
Procedure: dbo.INIT_Hanoi
Create Date: 2021-12-25
Author: Tomaz Kastrun
Description: Creates a table that stores the number of
rings used in the game with three rods.
Table name is dbo.Hanoi and is used to
to store the moves.
Procedure output: [dbo].[Hanoi]
Parameter(s): @rings - Number of rings; e.g.: 5 = 5 rings
on 3 rods; Type: TINYINT (max 255 rings!)
Usage: EXEC dbo.INIT_Hanoi
@rings = 5
ChangeLog:
ToDO:
Align Drawing!
************************************************************* */
@rings TINYINT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @j INT = 1
DROP TABLE IF EXISTS dbo.Hanoi;
DECLARE @TableCreate NVARCHAR(1000) =
'DROP TABLE IF EXISTS dbo.Hanoi;
CREATE TABLE dbo.Hanoi (
ID TINYINT IDENTITY(1,1) NOT NULL
,T1 TINYINT NOT NULL
,T2 TINYINT NOT NULL
,T3 TINYINT NOT NULL
)
'
WHILE (@rings >= @j)
BEGIN
SET @TableCreate = @TableCreate + '
INSERT INTO dbo.Hanoi(T1, T2, T3) VALUES ('+CAST(@j AS varchar(10))+',0,0)
'
SET @j = @j+1
END
EXEC sp_executesql @tableCreate
DECLARE @max INT = @rings*4
SELECT
REPLICATE(' ',(@max - T1*2)/2) + REPLICATE('#', T1*2) + REPLICATE(' ',(@max - T1*2)/2) AS T1
,REPLICATE(' ',(@max - T2*2)/2) + REPLICATE('#', T2*2) + REPLICATE(' ',(@max - T2*2)/2) AS T2
,REPLICATE(' ',(@max - T3*2)/2) + REPLICATE('#', T3*2) + REPLICATE(' ',(@max - T3*2)/2) AS T3
FROM hanoi
ORDER BY ID ASC
END;
GO
CREATE OR ALTER PROCEDURE [dbo].[PLAY_Hanoi]
/**************************************************************
Procedure: dbo.PLAY_Hanoi
Create Date: 2021-12-25
Author: Tomaz Kastrun
Description: Creates a table that stores the number of
rings used in the game with three rods.
Table name is dbo.Hanoi and is used to
to store the moves.
Procedure output: [dbo].[Hanoi]
Parameter(s): @from - rod number taking the first ring
@to - rod number putting the same ring
Usage:
EXEC dbo.PLAY_Hanoi
@from = 1
,@to = 2
ToDO:
- Align Drawing of tower!
- Stop / finish the game
- Rings different ASCII Char!
************************************************************* */
@from INT
,@to INT
AS
BEGIN
SET NOCOUNT ON;
-- internal values
DECLARE @from_variable VARCHAR(10) = (select column_name from information_Schema.columns where table_name = 'hanoi' and table_Schema = 'dbo' and ordinal_position = (@from + 1))
print @from_variable
DECLARE @to_variable VARCHAR(10) = (select column_name from information_Schema.columns where table_name = 'hanoi' and table_Schema = 'dbo' and ordinal_position = (@to + 1))
print @to_variable
-- FROM position
DECLARE @from_position NVARCHAR(1000)
SET @from_position = 'SELECT top 1 ID FROM dbo.hanoi where '+@from_Variable+' <> 0 order by id asc'
DROP TABLE IF EXISTS #from_pos
CREATE table #from_pos (val int)
INSERT INTO #from_pos
EXEC sp_executesql @from_position
-- FROM value
DECLARE @from_value NVARCHAR(1000)
SET @from_value = 'SELECT top 1 '+@from_variable+' FROM dbo.hanoi where '+@from_Variable+' <> 0 order by id asc'
DROP TABLE IF EXISTS #from_val
CREATE table #from_val (val int)
INSERT INTO #from_val
EXEC sp_executesql @from_value
IF (SELECT COUNT(*) FROM #from_val) = 0
BEGIN
INSERT INTO #from_val VALUES (0)
END
-- TO position
DECLARE @to_position NVARCHAR(1000)
SET @to_position = 'SELECT top 1 ID FROM dbo.hanoi where '+@to_variable+' = 0 order by id desc'
DROP TABLE IF EXISTS #to_pos
CREATE table #to_pos (val int)
INSERT INTO #to_pos
EXEC sp_executesql @to_position
-- TO value
DECLARE @to_value NVARCHAR(1000)
SET @to_value = 'SELECT top 1 '+@to_variable+' FROM dbo.hanoi where '+@to_variable+' = 0 order by id desc'
DROP TABLE IF EXISTS #to_val
CREATE table #to_val (val int)
INSERT INTO #to_val
EXEC sp_executesql @to_value
-- TO Prev Value
DECLARE @prev_to_val NVARCHAR(1000)
SET @prev_to_val = 'select top 1 '+@to_variable+' from hanoi where '+@to_variable +' <> 0 order by id asc'
DROP TABLE IF EXISTS #to_prev_val
CREATE table #to_prev_val (val int)
INSERT INTO #to_prev_val
EXEC sp_executesql @prev_to_val
-- number of rings!
declare @rings int = (select COUNT(*) from dbo.hanoi)
declare @max int = @rings*4
--- internal update
-- add rules for update!!!!
IF ((SELECT ISNULL(val,0) FROM #to_prev_val) < (SELECT val FROM #from_val))
BEGIN
SELECT 'Wrong Move'
END
ELSE
BEGIN
IF ((SELECT ISNULL(val,0) FROM #to_val) = 0 AND (SELECT ISNULL(val,0) FROM #from_val) = 0)
BEGIN
SELECT 'Invalid Move'
END
ELSE
BEGIN
--update FROM pos/value
DECLARE @update_from NVARCHAR(1000)
SET @update_from = 'update dbo.hanoi set '+@from_variable+' = (select 0 ) WHERE ID = (SELECT val FROM #from_pos) '
EXEC sp_executesql @update_from
--update TO pos/value
DECLARE @update_to NVARCHAR(1000)
SET @update_to = 'update dbo.hanoi set '+@to_variable+' = (select val from #from_Val) WHERE ID = (SELECT val FROM #to_pos)'
EXEC sp_executesql @update_to
END
END
SELECT
REPLICATE(' ',(@max - T1*2)/2) + REPLICATE('#', T1*2) + REPLICATE(' ',(@max - T1*2)/2) AS T1
,REPLICATE(' ',(@max - T2*2)/2) + REPLICATE('#', T2*2) + REPLICATE(' ',(@max - T2*2)/2) AS T2
,REPLICATE(' ',(@max - T3*2)/2) + REPLICATE('#', T3*2) + REPLICATE(' ',(@max - T3*2)/2) AS T3
FROM dbo.hanoi
ORDER BY ID ASC
-- check Tower 2 and Tower 3
DECLARE @t2 INT = (SELECT COUNT(T2) FROM Hanoi WHERE T2 <> 0)
DECLARE @t3 INT = (SELECT COUNT(T3) FROM Hanoi WHERE T3 <> 0)
IF (@T2 = @rings OR @T3 = @rings)
BEGIN
SELECT 'Game Won!'
-- Initialize New Game
EXEC dbo.INIT_Hanoi @rings
END
END;
GO