This is mini projects (Tic-Tac-Toe game) for Turbo C / C++ Compiler.

Background

Hello gamer! This is my 1st semester project for graduation level that was developed in the Turbo C/C++ graphics. After be the member of codeproject community, I have seem to be that I can share my project with the game world…

Introduction

TIC-TAC-TOE is the most popular and easiest game. It is a two players(X and O) game, where each taking turns marking the spaces in a 3×3 grid. I was developed is game in the TurboC graphics with interfacing keyboard event and also shown some interactive animation. This article will be helpful for anyone who want to develop this type of game using TurboC graphics. So let’s get started…

Requirement

To run and develop TurboC graphics code, you need the following,

  • TurboC compiler to execute and run the code.
  • To graphic mode functions require a graphics monitor and adapter card such as CGA, EGA and VGA.
  • To start graphics programming then you need two files which are GRAPHICS.H and GRAPHICS.LIB. Especially, these files are provided as part of TurboC compiler.

Download & Install TurboC

You can download the TurboC++ for window 7 or 8 in 64bit from the following link:-

TurboCplusplus Compiler

After downloading and installing the TurboC++ compiler, you can automatic find the related header file GRAPHICS.H And for the GRAPHICS.LIB file can be found in the following path:-

It need not to set by you when you run a code. Only you need to set the initgraph() 3rd variable path is as following:-

N.B: Consider that your TurboC++ compiler installed in the C:\\ TurboC++

Now put the code \TurboC++\Disk\TurboC3\BIN folder and now open the TurboC++ Editor. Go to File>Open and browser the path of TIC_TAC_TOE.CPP and open the file.

Start Game

Graphics Environment Starts with the graphics initializes function initgraph() that load a graphics driver from disk then putting the system into graphics mode. It accept three arguments, one is graphics driver(gd), second is graphics mode(gm) and third is path for bgi directory that is specifies the directory path where initgraph looks for graphics drivers (*.BGI). Here I have set path c:\\turboc3\\bgi.

Next I have played an animation text that is write a string “TIC TAC TOE” where each character is animatedly appear one by one. To animatedly write the each letter, a function is called corresponding to the letter. After completing the write “TIC TAC TOE” string two animation is sparking two side of the string.

To write animated letter T a function T() is calling that draw a horizontal line and then a vertical line by the circle() function, where circle() function draw a circle in x,y coordinate with radius r. If we increases the coordinate value of x and y is constant of parameter of circle() function then it will be create a horizontal line. Same think can be apply to draw a vertical line.

Game Introduction Animation

Menu Selection

Now you need to select your options that is 1 for Start the game and 2 for Quit the game. By enter your choice by the standard input/output method, game will be go for the further process. This input is taken by the simple standard I/O function scanf(). According to the input value corresponding operation will be perform by the switch-case statement.

Game Menu Options

Enter Player

Enter the player name corresponding to the player X and Player O. Player name will be capture as string by the gets() function. Where gets() function take the string input by the standard I/O method.

Player Name Screen

Display Board

In this game two players alternate placing stones of their own color on an m×n board. Board is drown by the simply printf() function.

Game Blank Box

Player X Turn

The first player, who shall be designated “X” need to be enter the row and column number. If column or row number is out range of [1,3] like row<1 or row>3 or column<1 or column>3 then it would be a wrong position or else grid position array pos_marked[row][col] will be assign 1 according to the row and column number.

Player O Turn

The second player, who shall be designated “O” need to be enter the row and column number. If PlayerO enter row or column position that is not valid that’s means value is out of [1,3] like row<1 or row>3 or column<1 or column>3 then it will be warning through an message “WRONG POSITION!” or else it will be marked the grid position array pos_marked[row][col] assign 1 according to the value of row and column value.

Wrong Turn

Wrong turn can be made up two ways:-

  • If player give an entry that cell is already fill-up. That means pos_marked[row][col]=1 then position will be wrong.
  • If player give the row or column number that is out of 3×3 grid. To do check if row<1 or row>3 or col<1 or col>1 then wrong position will be happen.

Game Wrong Position

Result check

After each turn of a player check a calculation that the pos_marked[i][j]=1 where I and j is increment value from 1 to 3 and the value of check(chk) is increased or else it would be continue.

Win Calculation

Now time to check the result. The player who succeeds in placing three respective marks in a horizontal, vertical, or diagonal row wins the game.

To check the horizontal line for the player x if the pos_for_X[i][1]=1 and pos_for_X[i][2]=1 and pos_for_X[i][3]=1 then playerX will be win where I the increment value from 1 to 3. Same way I have checked the vertical line and diagonal line. And the code segment is as following,

Game win match scenario

Match Draws

Game will be draws by the following condition,

  • If no result is return from the board against the player X or O that means if chk variable value is 9.
  • If no result is return for the win variable that means if win variable is not equal 1.

Game draw

Quit Game

After finished the game, it will be shown is as following good bye message. This is simple text is present by the printf() function.

After complete the game

Conclusion

Tic-Tac-Toe is the most popular game. So download the source code and run it to play the game. Happy Coding…:)

SHARE
Previous articleOnline Image Storage
Next articleHow to Carve Wooden Chiseled Effect in Adobe Photoshop CS6 Tutorial
I am a professional software developer from last 4 years. I have experience in analysis, design, development, testing and implementation of desktop (using C, C++, C#) and web (using HTML5, CSS3, JavaScript, PHP, MySql) platform using. I have good exposure to object-oriented design, software architectures, design patterns, test-driven development and Project Management.

LEAVE A REPLY