SQL Server Central is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

Tic Tac Toe TSQL Style

By adam haines, 2008/03/11

Total article views: 955 | Views in the last 30 days: 148

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.

 

Thanks,

Adam

By adam haines, 2008/03/11

Total article views: 955 | Views in the last 30 days: 148
Your response
 
 
Related tags

games     TSQL    
SQL 2005     T-SQL    
SQL Server 2005    
 
Like this? Try these...

Get dependant Objects

By BeoWulf . | Category: T-SQL
(not yet rated) | 570 reads
Already registered?  

Free registration required

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Register

E-mail address:
Password:
Password (confirm):

  

Subscriptions

We ask you to register on the site and subscribe to our newsletters. Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

We ask that you give the newsletter a try for a week. Over 200,000 SQL Server Professionals a day find it entertaining and useful. If not, you are welcome to unsubscribe at anytime.

Steve Jones
Editor, SQLServerCentral.com