This GitHub repository contains code samples that demonstrate how to create and use the procedures for Connect-4 game using SQL Server Transact SQL (T-SQL) in your favorite editor.
This is the classical puzzle game, that is easy and fun to play. The objective of the game is to assemble the line of four tokens on the board that are either vertical, horizontal, or diagonal.
Source: WikipediaGame consists of four T-SQL procedures that complete the game and can be played. Run the complete Connect4.sql file in to install the game on your database.
The procedures for the game are:
- Procedure
dbo.Init
to create and initialize the game (start procedure) - Procedure
dbo.DisplayResults
to preetify the board and corresponding tokens (helper procedure) - Procedure
dbo.CheckWin
to check for game stop and verify the user inputs (helper procedure) - Procedure
dbo.AddToken
to play the game by both users (main procedure)
The SQL Procedure: dbo.Init describes and creates an empty board for a given default dimension (7x6).
Run the code:
-- Initialize the game
EXEC dbo.Init
Procedure must be run every time, if you want to initiate the new game.
The T-SQL Procedure: dbo.DisplayResults prettifies the board game (7x6) and shows the tokens as X (for player 1) and O (for player 2).
Run the code:
-- Check the board restults
EXEC dbo.DisplayResults
This is a helper procedure and is internally run by other procedures.
The T-SQL procedure: dbo.CheckWin describes four ways to check if either of players won a game. It searches for:
- VERTICAL
- HORIZONTAL
- DIAGONAL UP and
- DIAGONAL DOWN
solutions through the board, accordingly.
The T-SQL procedure: dbo.AddToken gives the users the gameplay. The input parameters are user and column:
-- Add token to 5th column for player 1
EXEC dbo.AddToken 1, 5
-- Add token to 4th column for player 2
EXEC dbo.AddToken 2, 4
This procedure also holds some of the error handling; it checks for:
- correct input (player and column)
- checks for correct order of players (each play alternatively)
- checks for number of tokens in each column and prevent column overflow
Game is started by initializing the board and players. After the initialization, you can start playing the gaming by alternatively adding tokens from each player. The adding of tokens is done by using the procedure
EXEC dbo.AddToken {column}, {player}
.
Here is the starter and sample code:
EXEC dbo.Init
EXEC dbo.AddToken 1, 5
EXEC dbo.AddToken 2, 4
...
During the game, you will get board displayed every single time, when user is making a move.
To work in GitHub, go to https://github.com/tomaztk/Connect4_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.
You can also clone the repository. Note: further changes should be fetched manually.
git clone -n https://github.com/tomaztk/Connect4_sql_game
Collaboration on this code is welcome and so are any additional questions or comments.
Code is licensed under the MIT license.
Here is also the blogpost for the game: blogpost
- AI to play against the computer