What is 2048 game? It is a classical puzzle game, that is easy and fun to play. The objective of the game is to move the numbers (tiles in the matrix/board) in a way to combine them to create a tile with the number 2048.

Install the game

Go to Github repository: https://github.com/tomaztk/2048_sql_game and get the content of the all three T-SQL files.

You will need to create a board of desired size (e.g.: 4x4):

 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- CREATE MATRIX (board) for @dim dimension -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CREATE OR ALTER PROCEDURE dbo.CREATE_matrix 	 	@dim INT  AS BEGIN 	DECLARE @i INT = 1 	DECLARE @j INT = 1  	DECLARE @TableCreate NVARCHAR(2000) =  	'DROP TABLE IF EXISTS dbo.T_2048;  	CREATE TABLE dbo.T_2048 (ID INT IDENTITY(1,1), '  	WHILE (@dim >= @i)  	BEGIN 		SET @TableCreate = @TableCreate + 'V' + CAST(@i AS VARCHAR(10)) + ' SMALLINT ,' 		SET @i = @i + 1 	END 	SET @TableCreate = STUFF(@TableCreate, LEN(@TableCreate), 1, ');')  	WHILE (@dim >= @j) 	BEGIN 		SET @TableCreate = @TableCreate + '  		INSERT INTO dbo.T_2048 VALUES (' 		+ STUFF(REPLICATE('0,',@dim), LEN(REPLICATE('0,',@dim)), 1, ');')  		SET @j = @j+1 	END  	EXEC sp_executesql @tableCreate END; GO 

The next step is to create a procedure that will add a new number (number 2) into empty tile:

  -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- ADD NUMBER (board) for @dim dimension -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CREATE OR ALTER PROCEDURE dbo.ADD_number 	@dim INT AS BEGIN 	 DECLARE @nofTry INT = 10*@dim DECLARE @i INT = 1  WHILE @nofTry > @i 	BEGIN  			declare @a int = 1 			declare @b int = @dim  			declare @x int, @y int = 1  			SET @x = (SELECT FLOOR(RAND()*(@b-@a+1))+@a) 			SET @y = (SELECT FLOOR(RAND()*(@b-@a+1))+@a)  				DECLARE @COL NVARCHAR(100) = ( 						SELECT  							COLUMN_NAME  						FROM INFORMATION_SCHEMA.COLUMNS 						WHERE 								TABLE_NAME = 'T_2048' 							AND TABLE_SCHEMA = 'dbo' 							AND ORDINAL_POSITION = @x+1 )   			--- CHECK if 0 			DECLARE @check0 NVARCHAR(1000) = 'SELECT ' +@COL+ ' FROM dbo.T_2048 WHERE ID =' + CAST(@y AS varchar(10)) 			PRINT @check0  			DECLARE @temp TABLE (RES INt)  			INSERT INTO @temp 			EXEC sp_executesql @check0   			IF (SELECT res FROM @temp) = 0 				BEGIN 						SET @i = @nofTry 						DECLARE @Sq NVARCHAR(2000) =   							'UPDATE dbo.T_2048 								SET ' + CAST(@COL AS VARCHAR(100)) + ' = CASE WHEN ' + CAST(@COL AS VARCHAR(100)) + '  = 0 THEN 2  ELSE ' + CAST(@COL AS VARCHAR(100)) + '  END 								WHERE	 									ID = '+CAST(@y AS VARCHAR(100))  							EXEC sp_executesql @Sq 							--RETURN; 				END 		SET @i = @i + 1 	END  END; GO 

Move around the board

Once the board is created, you will need to control the moves. Four procedures that will calculate moves in all four directions are needed (UP, DOWN, LEFT, RIGHT). For the sake of brevity, I am adding only one direction:

 ---- -------------------------- ---- -------------------------- ---- MOVE UP Procedure ---- -------------------------- ---- --------------------------  CREATE OR ALTER PROCEDURE dbo.MOVE_up 		@dim INT AS BEGIN  	DECLARE @Column_counter INT = 2 	Declare @max_column INT = (SELECT @dim /* dim */ + 2)   	while @max_column > @Column_counter 	BEGIN 		-- Get first column name 		DECLARE @col_name VARCHAR(10) = (SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'T_2048' and ORDINAL_POSITION = @Column_counter) 		print @col_name  		DECLARE @sql1 NVARCHAR(2000) = 		'SELECT  id, ' + @col_name + ' as v FROM T_2048'  		DROP TABLE IF EXISTS #temp; 		create table  #temp (id int, v int) 	 		insert into #temp 		EXEC sp_executesql @sql1 		--------------------------------------- 					-- UP scenarij 						DECLARE @ii int = 1 						while @dim-1 >= @ii -- nof dimensions 						BEGIN 							declare @i int = 1  							while @dim > @i --  							--declare @i int = 1  							begin	 								declare @vv_1 int = (select v from #temp where id = @i) 								declare @vv_2 int = (select v from #temp where id = @i+1)  							IF (@vv_1 = 0 AND @vv_2 <> 0) 							BEGIN 								update #temp set v = @vv_2 where id = @i    								update #temp set v = 0     where id = @i+1  							END  							IF (@vv_1 <> 0 AND @vv_1 = @vv_2) 							BEGIN 								update #temp set v = @vv_1 + @vv_2 where id = @i 								update #temp set v = 0 where id = @i+1 							END  							IF (@vv_1 <> 0 AND @vv_2  = 0)  							BEGIN 								Print 'Do nothing' 							END  							set @i = @i + 1 							end 						  set @ii = @ii + 1 						END  				-- update back to T_2048 table from #temp table  				DECLARE @sql_temp_update NVARCHAR(500)  				SET @SQL_temp_update =  				'UPDATE T20 					SET '+@col_name+' = t.v 			 				FROM t_2048  AS T20 				JOIN #temp AS t 				ON T20.id = t.id' 			 				EXEC sp_executesql @SQL_temp_update 		SET @Column_counter = @Column_counter + 1 	   END END; GO  

All moves are available on Github: https://github.com/tomaztk/2048_sql_game.

And the last procedure is the main procedure that will combine all the moves and adding new numbers:

  CREATE OR ALTER PROCEDURE dbo.PLAY_game 	 @move CHAR(1) -- U, D, L, R (Up, Down, Left, Right) 	,@dim  INT  -- size of the matrix AS BEGIN  	IF @move = 'U' 		BEGIN 			EXEC dbo.MOVE_up @dim;             EXEC [dbo].[ADD_number] @dim; 			SELECT * FROM T_2048 		END  	IF @move = 'D' 		BEGIN 			EXEC dbo.MOVE_down @dim;             EXEC [dbo].[ADD_number] @dim; 			SELECT * FROM T_2048 		END   	IF @move = 'L' 		BEGIN 			EXEC dbo.MOVE_left @dim;             EXEC [dbo].[ADD_number] @dim; 			SELECT * FROM T_2048 		END  	IF @move = 'R' 		BEGIN 			EXEC dbo.MOVE_right @dim;             EXEC [dbo].[ADD_number] @dim; 			SELECT * FROM T_2048 		END  END; GO 

Now that you are all set, let's start the game.

Start the game

If you want to play 4x4 board, you will need to create a bord and add two numbers that will be placed at the random position. This can be done with:

 EXEC  [dbo].[CREATE_matrix] 4; GO EXEC [dbo].[ADD_number] 4; GO 2 

Creating an empty table in first procedure and adding two numbers in second procedure.

table

And now you can start playing by calling the move procedures. In this case, player is taking first move to go UP.

 EXEC [dbo].[PLAY_game] 'U', 4 
table

The UP move summed the two 2 numbers into 4 and added a new number 2 on blank tile (blank tile = 0).

As always, the complete code is available on Github repository. And feel free to collaborate.

Happy T-SQLing!


This post is ad-supported