{"id":21074240,"url":"https://github.com/tomaztk/2048_sql_game","last_synced_at":"2025-05-16T06:31:15.415Z","repository":{"id":45929389,"uuid":"432399432","full_name":"tomaztk/2048_sql_game","owner":"tomaztk","description":"Popular 2048 game with T-SQL","archived":false,"fork":false,"pushed_at":"2022-06-03T12:11:41.000Z","size":28,"stargazers_count":4,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2023-03-09T08:55:48.698Z","etag":null,"topics":["2048","2048-game","game","sql-game","sql-server","sqldatabase","t-sql"],"latest_commit_sha":null,"homepage":"","language":"TSQL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tomaztk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-11-27T07:43:19.000Z","updated_at":"2021-12-07T08:24:35.000Z","dependencies_parsed_at":"2022-09-05T17:01:30.816Z","dependency_job_id":null,"html_url":"https://github.com/tomaztk/2048_sql_game","commit_stats":null,"previous_names":[],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomaztk%2F2048_sql_game","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomaztk%2F2048_sql_game/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomaztk%2F2048_sql_game/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomaztk%2F2048_sql_game/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tomaztk","download_url":"https://codeload.github.com/tomaztk/2048_sql_game/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225411520,"owners_count":17470245,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["2048","2048-game","game","sql-game","sql-server","sqldatabase","t-sql"],"created_at":"2024-11-19T19:15:09.485Z","updated_at":"2024-11-19T19:15:10.162Z","avatar_url":"https://github.com/tomaztk.png","language":"TSQL","readme":"# T-SQL Code for popular 2048 Game\nThis GitHub repository contains code samples that demonstrate how to create and use the procedures for 2048 Game using SQL Server Transact SQL (T-SQL) in your favorite editor.\n\n# What is 2048 game?\nThis is the 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.\n\n\n\u003c!--![](/img/game2048.png?style=centerme) --\u003e\n\u003cdiv style=\"text-align:center\"\u003e\u003cimg src=\"/img/game2048.png\" alt=\"2048\" style=\"width:300px;\"/\u003e\u003c/div\u003e\n\n\n\n# SQL Procedures\n\nGame consists of several procedures that make the game work.\nIn order for the game to start, you will need to run all the procedures and use the main 'Play_game' procedure.\n\nTwo groups of procedure are available to start and play the game:\n  1. Procedures to create and initialize matrix (helper procedures)\n  2. Procedures to make moves (helper procedures)\n  3. Procedure to play the game (main procedure)\n\n\n\n## Create and initialize matrix\n\nThe SQL file: _01_Create_initialize.sql_  describes two procedures that create an empty matrix (board) for a given dimension and initialize the matrix by adding two start numbers at a random position [x,y].\n\nRun the code:\n```(sql)\n-- Create board of size 4\nEXEC dbo.CREATE_matrix 4\n\n-- Initialize board of size 4 with two random numbers\nEXEC dbo.FIND_ADD_number 4;\nGO 2 \n```\n\n## Procedures to make moves\n\nThe SQL file: _02_Move_procedures.sql_ describes four procedures that imitate a LEFT, RIGHT, UP and DOWN user move and calculates and moves the numbers in board accordingly.\n\nRun the code:\n\n```(sql)\n-- Make a move UP on a board of size 4x4\nEXEC dbo.MAKE_up  4\n\n-- Make a move LEFT on a board of size 4x4\nEXEC dbo.MAKE_left  4\n\n```\n\n\n# Playing the game\n\nThe last file _03_Play_game.sql_ holds the procedure that will give you the ability to play the game. You can simply execute the procedure to do the moves, accordingly.\n\n```\n-- make a LEFT move\nEXEC dbo.PLAY_game 'L', 4\n\n-- make a UP move\nEXEC dbo.PLAY_game 'U', 4\n```\n\nGeneric code:\n\n```EXEC dbo.PLAY_game {direction}, {size}```\n\nFor the {direction} use the following abbreviations:\n1. 'D' to move DOWN\n2. 'U' to move UP\n3. 'L' to move LEFT\n4. 'R' to move RIGHT\n\nAnd for the size it can be any give integer that is between 3 or above.\n\n\n# Start The game\n\nIf 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:\n\n```\nEXEC  [dbo].[CREATE_matrix] 4;\nGO\nEXEC [dbo].[FIND_ADD_number] 4;\nGO 2\n```\n\nCreating an empty table in first procedure and adding two numbers in second procedure.\n\n\u003cdiv style=\"text-align:center\"\u003e\u003cimg src=\"/img/table1.png\" alt=\"table\" style=\"width:300px;\"/\u003e\u003c/div\u003e\n\nAnd now you can start playing by calling the move procedures. In this case, player is taking first move to go UP. \n\n```\nEXEC [dbo].[PLAY_game] 'U', 4\n```\n\u003cdiv style=\"text-align:center\"\u003e\u003cimg src=\"/img/table2.png\" alt=\"table\" style=\"width:300px;\"/\u003e\u003c/div\u003e\n\nThe UP move summed the two 2 numbers into 4 and added a new number 2 on blank tile (blank tile = 0).\n\n\n## Forking or cloning the repository\nTo work in GitHub, go to https://github.com/tomaztk/2048_sql_game and fork the repository. Work in your own fork and when you are ready to submit to make a change or publish your sample for the first time, submit a pull request into the master branch of this repository. \n\nYou can also clone the repository. Note: further changes should be fetched manually.\n\n\n```\ngit clone -n https://github.com/tomaztk/2048_sql_game\n```\n\n## Code of Conduct\nCollaboration on this code is welcome and so are any additional questions or comments.\n\n\n## License\nCode is licensed under the MIT license.\n\n### ToDO\n1. Prettify T-SQL code\n2. Optimize function for vectorization of table (or even come up with new function / data type)\n3. Implementing procedures in Power BI or SSRS :-)\n   \n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomaztk%2F2048_sql_game","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomaztk%2F2048_sql_game","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomaztk%2F2048_sql_game/lists"}