Giter Site home page Giter Site logo

connect4_sql_game's Introduction

Connect 4 game written in T-SQL

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.

What is Connect 4 game?

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.

Connect 4
Source: Wikipedia

Game and T-SQL Procedures

Game 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:

  1. Procedure dbo.Init to create and initialize the game (start procedure)
  2. Procedure dbo.DisplayResults to preetify the board and corresponding tokens (helper procedure)
  3. Procedure dbo.CheckWin to check for game stop and verify the user inputs (helper procedure)
  4. Procedure dbo.AddToken to play the game by both users (main procedure)

Create initialized table (matrix)

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.

Displaying the results

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

Prettify board

This is a helper procedure and is internally run by other procedures.

Procedures to check for win (finding the solution)

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.

Procedures to add tokens

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

Generic code for playing the game

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.

game play

Forking or cloning the repository

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

Code of Conduct

Collaboration on this code is welcome and so are any additional questions or comments.

License

Code is licensed under the MIT license.

Blogpost

Here is also the blogpost for the game: blogpost

ToDO

  • AI to play against the computer

connect4_sql_game's People

Contributors

tomaztk avatar

Stargazers

Robert Smith avatar Sergio Govoni avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.