SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Tic Tac Toe TSQL Style

By Adam Haines,

This script allows a player to compete against a computer in tic tac toe. The game was written in SQL 2005 and may not work correctly in other versions. The game has a little more kick than a game of tic tac toe with an inadequate opponent. This is because to make the game feel more realistic; I added a layer of AI to the computer. The computer can logically determine if he should block the player from wnning the game or go for the win itself.


To Play

Playing the game is very simple. The game feeds off three main stored procedures.

  1. usp_PlayTicTacToe: marks the board with your move
  2. usp_Redraw_Board: Renders the game board, in its current state
  3. usp_New_Game: Starts a new game by clearing the tables


usp_PlayTicTacToe has 2 mandatory inputs Quadrant and Mark. Quadrant is the spot you want to mark on the game board. Examples of a quadrant are a1,a2,a3,b1,b2,b3,c1,c2,c3. Mark is a one character notation of your player. Mark can be either 'X' or 'O'.


usp_Redraw_Board simply renders the game board by reading the data from the table and redrawing the board.


usp_New_Game is used once the game has completed to start a fresh game. It just resets the necessary columns in the table to start afresh.


Other than these three stored procedures, the game itself will tell you if something is wrong with your inputs. Additionally, after each of the computer's moves; you can view where the computer moved on his last turn.


This design really has scalablity for future enhancesments. The table could be created as a Global Temp table and the game could be played by remote players.


That's about it. Enjoy.




Total article views: 2829 | Views in the last 30 days: 9
Related Articles

4 Quadrant Scatter Plot ChartUsing SSRS 2008R2

Create a 'Gartner Magic Quadrant Chart'


Power BI Custom Visuals Class (Module 63 – Quadrant Chart)

In this module you will learn how to use the Quadrant Chart Custom Visual.  The Quadrant Chart is us...


Adobe Flash Player Update Available

There is an Adobe Flash Player available to address a security issue. The bulletin shows as being re...


Computed Column

Computed Column


Computed Columns

Computed Columns

sql server 2005