You are on page 1of 33




Title of the Seminar: Tic Tac Toe Game Implementation Course Leader: Khondokar Fida Hasan Lecturer, Department of ICT, Comilla University Autrhors: Group C 1.Morshed Alam (Group Leader) 2.Faisal Hoque 3.Sanjay Debnath email: 4.Roksana Rahman email: Date of Submission: April 15,2012

Table of Contents
1.1 Introducton......3 1.1(a) Objective...3 1.1(b) Motivation.........3 1.1(c) About Tic Tac Toe....3 1.1(d) Stratgy se fort the game....4 1.2 Literatuare Review......6 1.3 Methodology...7 1.4 Source Code............................................................................................8 1.5 Output.................................................................................................. .20 1.6 Results................21 1.7 Discussion .26 1.7(a) Limitations....... ..26 1.7(b) Future Plan..........26 1.8 Conclusion.....27 1.8(a) Recommendation....27 1.9 Bibliography........................................................................................ .28 2.0. Appendix..............................................................................................29

1.1(a) Objective:
Reading this paper you will be able to know What is Tic Tac Toe game? Is it possible to run the game into Computer?

1.1(b) Motivation for Research:

During the playing the game in paper,we got the motivation to run the game into Computer.

1.1(c) About Tic Tac Toe:

Tic-Tac-Toe game is a silly game. This game is very popular among the young children and is fairly simple by itself. It is actually a two player game. Tic-tac-toe, also called wick wack woe (in some Asian countries) and noughts and crosses (in the British Commonwealth countries) and X's and O's in the Republic of Ireland, is a pencil-and-paper game for two players, X and O, who take turns marking the spaces in a 33 grid. The X player usually goes first. The player who succeeds in placing three respective marks in a horizontal, vertical, or diagonal row wins the game.
The following example game is won by the first player, X:

Players soon discover that best play from both parties leads to a draw (where no one wins).

Hence, tic-tac-toe is most often played by young children. In this game, there is a board with n x n squares. In our game, it is 3 x 3 squares.The goal of Tic-Tac-Toe is

to be one of the players to get three same symbols in a row - horizontally, vertically or diagonally - on a 3 x 3 grid. In our game two humans can play it.

1.1(d) Strategy used for the game:

A player can play perfect tic-tac-toe if they choose the move with the highest priority in the following table1) Win: If you have two in a row, play the third to get three in a row. 2) Block: If the opponent has two in a row, play the third to block them. 3) Fork: Create an opportunity where you can win in two ways. 4) Block Opponent's Fork: Option 1: Create two in a row to force the opponent into defending, as long as it doesn't result in them creating a fork or winning. For example, if "X" has a corner, "O" has the center, and "X" has the opposite corner as well, "O" must not play a corner in order to win. (Playing a corner in this scenario creates a fork for "X" to win.) Option 2: If there is a configuration where the opponent can fork, block that fork.

5) Center: Play the center. 6) Opposite Corner: If the opponent is in the corner, play the opposite corner. 7) Empty Corner: Play an empty corner. 8) Empty Side: Play an empty side. The first player, whom we shall designate "X", has 3 possible positions to mark during the first turn. Superficially, it might seem that there are 9 possible positions, corresponding to the 9 squares in the grid. However, by rotating the board, we will

find that in the first turn, every corner mark is strategically equivalent to every other corner mark. The same is true of every edge mark. For strategy purposes, there are therefore only three possible first marks: corner, edge, or center. Player X can win or force a draw from any of these starting marks; however, playing the corner gives the opponent the smallest choice of squares which must be played to avoid losing. The second player, whom we shall designate "O", must respond to X's opening mark in such a way as to avoid the forced win. Player O must always respond to a corner opening with a center mark, and to a center opening with a corner mark. An edge opening must be answered either with a center mark, a corner mark next to the X, or an edge mark opposite the X. Any other responses will allow X to force the win. Once the opening is completed, O's task is to follow the above list of priorities in order to force the draw, or else to gain a win if X makes a weak play. To guarantee a tie if you're O though, if X doesn't play center (playing a corner is the best opening move), take center, and then a side middle. This will stop any forks from happening. If you play a corner, a perfect X player has already played the corner opposite his first and proceeds to play a 3rd corner, stopping your 3-in-a-row and making his own fork. If X plays center opening move, just keep your eyes open and he won't be able to fork you. If you are X, play a corner first. If O takes center (best move for him), take the corner opposite your original, and proceed as detailed above. If O plays a corner or side-middle first, you are guaranteed to win. If corner, simply take any of the other 2 corners, and then the last. You've forked him. If he plays a side-middle, take the only corner that his blocking won't make 2 in a row. He'll block, but the best of the other two, you'll see which one, and you'll fork him. The only way for X not to win is for O to play middle and then a side-middle.

1.2 Literature Review:

An early variant of Tic-tac-toe was played in the Roman Empire, around the first century BC. It was called Terni Lapilli and instead of having any number of pieces,

each player only had three, thus they had to move them around to empty spaces to keep playing. The game's grid markings have been found chalked all over Rome.
However, according to Claudia Zaslavsky's book Tic Tac Toe: And Other Three-In-A Row Games from Ancient Egypt to the Modern Computer, Tic-Tac-Toe could originate back to ancient Egypt.

1864: The first print reference to "noughts and crosses", the British name. 1884: The first print reference to a game called "tick-tack-toe" occurred , but referred to "a children's game played on a slate, consisting in trying with the eyes shut to bring the pencil down on one of the numbers of a set, the number hit being scored". "Tic-tac-toe" may also derive from "tick-tack", the name of an old version of backgammon first described in 1558. The U.S. renaming of noughts and crosses as tic-tac-toe occurred in the 20th century.

1952: Tic-tac-toe became the first known video game, OXO (or Noughts and Crosses) for the EDSAC computer. The computer player could play perfect games of tic-tac-toe against a human opponent.

1975: Tic-Tac-Toe was also used by MIT students to demonstrate the computational power of Tinkertoy elements. The Tinkertoy computer, made out of (almost) only Tinkertoys, is able to play Tic-Tac-Toe perfectly. It is currently on display at the Museum of Science, Boston.

1.3. Methodology:
Tic-Tac-Toe(board[i][j]) 1. (Initialize the nine spaces in the board) board[i] [j] = *.

2. Repeat steps for i<3 & j<3. 3. If key = * then: set key = X for player 1. 4. for i<3 5. if (board [i][0] == board [i] [1] && board [i][0] == board [i] [2] && board[i][0]==key) else if(board [0][i] == board [1] [i] && board [0][i] == board [2] [i] && board[0][i] == key) else if(board [0][0] == board [1] [1] && board [1][1] == board[2] [2] && board[2][2]== key) then: Print : Player 1 wins & exit. 6. If key = * then: set key = 0 for player 2. 7. for i<3 8. if (board [i][0] == board [i] [1] && board [i][0] == board [i] [2] && board[i][0]==key) else if(board [0][i] == board [1] [i] && board [0][i] == board [2] [i] && board[0][i] == key) else if(board [0][0] == board [1] [1] && board [1][1] == board[2] [2] && board[2][2]== key) then: Print : Player 2 wins & exit.

1.4 Source


/* A Simple Tic Toc Toe Game made for the students of ICT Department */ /* turbo c++ version */ #include <iostream.h> #include <stdio.h> #include <graphics.h> #include <conio.h> #include <ctype.h>

#include <stdlib.h> char key; char board [3] [3]; // Remeber Indexing Starts at zero /******************** Function Parameters ************************/ void play(); void front_page(); void new_game(); void resume(); void instruction(); void drawcircle(int position); void drawcross(int position); void mapboard(); void initiateboard(); void player_1(); void player_2(); void check(); /******************** End of Function Parameters *****************/

/*********************** Main Function *************************/ int main() { int gdriver = DETECT, gmode; initgraph (&gdriver, &gmode, "C:\\TC\\BGI"); clrscr(); front_page(); closegraph(); return 0; } /****************** End of Main() *****************************/

/******************* front_page() ******************************/ void front_page() { char opt; int gdriver=DETECT,gmode; // closegraph(); initgraph(&gdriver,&gmode,"C:\\TC\\BGI"); clrscr(); setcolor(RED); settextstyle(1,0,6); outtextxy(5,30,"MAIN MENU"); outtextxy(5,50,"___________"); setcolor(BLUE); settextstyle(2,0,7); outtextxy(5,150,"1.New Game"); outtextxy(5,200,"2.Resume"); outtextxy(5,250,"3.Instruction"); outtextxy(5,300,"4.Exit "); opt=getch(); if(opt==49) //Hence 49 is the ASCII value of decimal 1 new_game(); else if(opt==50) //Hence 50 is the ASCII value of decimal 2 resume(); else if(opt==51) instruction(); else exit(0); } /****************** End of front_page() ************************/


/******************* Draw Circle ******************************/ void drawcircle(int position) { int centrex; int centrey; centrex = 245 + (50*(position%3)) + 25; centrey = 165 + (50*(position/3)) + 25; fillellipse (centrex,centrey, 10,10); } /******************* End of Draw Circle **********************/

/***************** Draw Cross ********************************/ void drawcross(int position) { int centrex; int centrey; centrex = 245 + (50*(position%3)) + 25; centrey = 165 + (50*(position/3)) + 25; line (centrex-5,centrey+5,centrex+5,centrey-5); line (centrex+5,centrey+5,centrex-5,centrey-5); } /***************** End of Draw Cros **************************/

/****************** Board Mapping ****************************/ void mapboard() { int gdriver = DETECT, gmode; int i,j; closegraph(); initgraph (&gdriver, &gmode, "C:\\TC\\BGI "); clrscr(); // Draw 4 Lines to Form Board line (295,165,295,315); // 2nd column


line (345,165,345,315); // 3rd column line (245,215,395,215); // 2nd row line (245,265,395,265); // 3rd row line (245,165,245,315); // 1st column line (395,165,395,315); // 4th column line (245,165,395,165); // 1st row line (245,315,395,315); // 4th row setcolor(RED); setfillstyle( SOLID_FILL,WHITE); floodfill(12,375,getmaxcolor()); for (i = 0; i <= 2; i++) { for (j = 0; j <= 2; j++) { if ( board [i] [j] == 'X') drawcross (3*i + j); if ( board [i] [j] == 'O') drawcircle (3*i + j); } } bar3d(150,8,500,56,8,1); setcolor(RED); settextstyle(1,0,5); outtextxy(200,8,"Tic Tac Toe"); setcolor(RED); settextstyle(4,0,4); outtextxy(150,420,"Developed by Group-C"); } /*************** End of Board Mapping ****************************/


/**************** Initialize Board ******************************/ void initiateboard() { int i,j; for (i = 0; i < 3 ; i++) { for (j = 0; j < 3; j++) { board [i] [j] = '*'; } } mapboard(); } /******************* End of Initialization *********************/

/***************************** Player-1 () *********************/ void player_1 () { int a,b; mapboard(); settextstyle(2,0,8); outtextxy(5,70,"Turn:Player-1"); outtextxy(5,80,"_____________"); settextstyle(2,0,6); // outtextxy(10,400,"Press any key for coordinates."); outtextxy(500,400,"S->STOP"); char stop; stop=getch(); if(stop=='s' || stop=='S') front_page();


settextstyle(2,0,6); outtextxy(5,345,"Enter X Coordinate:"); gotoxy(25,23); cin>>a; outtextxy(5,380,"Enter Y Coordinate:"); gotoxy(25,25); cin>>b; if(board [a-1] [b-1] == '*') { key = '*'; board [a-1] [b-1] = 'X'; mapboard(); } else { // mapboard (); outtextxy(5,100,"Wrong Coordinates.Please Enter coordinates correctly."); mapboard(); player_1 (); } } /****************************** End of Player-1 () ***************/

/************************** Player-2 () **************************/ void player_2 () { int x,y; mapboard(); settextstyle(2,0,8); outtextxy(5,70,"Turn:Player-2"); outtextxy(5,80,"_____________"); settextstyle(2,0,6);


// outtextxy(10,400,"Press any key for coordinates."); outtextxy(500,400,"S->STOP"); char stop; stop=getch(); if(stop=='s' || stop=='S') front_page(); settextstyle(2,0,6); outtextxy(5,345,"Enter X Coordinate:"); gotoxy(25,23); cin>>x; outtextxy(5,380,"Enter Y Coordinate:"); gotoxy(25,25); cin>>y; /* Stop Operation outtextxy(400,400,"S->STOP"); char stop; stop=getch(); if(stop=='s' || stop=='S') front_page(); */

if (board [x-1] [y-1] == '*') { key = '*'; board [x-1] [y-1] = 'O'; mapboard(); } else { mapboard (); outtextxy(5,100,"Wrong Coordinates.Please Enter coordinates correctly."); player_2();


} } /******************************* End of Player-2 () ******************/

/**************************** Check () **********************************/ void check () { int i; // Check Rows for (i=0; i<3;i++) if (board [i][0] == board [i] [1] && board [i][0] == board [i] [2] && board [i] [0] != '*') key = board [i] [0]; // check Columns for (i=0; i<3;i++) if (board [0][i] == board [1] [i] && board [0][i] == board [2] [i] && board [0] [i] != '*') key = board [0] [i]; // Check Diagonals if (board [0][0] == board [1] [1] && board [1][1] == board [2] [2] && board [1] [1] != '*') key = board [1] [1]; if (board [0][2] == board [1] [1] && board [1][1] == board [2] [0] && board [1] [1] != '*') key = board [1] [1]; if (key == 'X') { setcolor(GREEN); settextstyle(2,0,8); outtextxy(210,120,"Yeah!Player-1 Win"); // getch(); // clrscr();


settextstyle(2,0,8); outtextxy(5,350,"N->New Game"); outtextxy(450,350,"M->Main Menu"); char anoth; anoth=getch(); if(anoth=='n' || anoth=='N') new_game(); else if(anoth=='m'||anoth=='M') front_page(); } if (key == 'O') { setcolor(GREEN); settextstyle(2,0,8); outtextxy(210,120,"Yeah!Player-2 Win"); // getch(); // clrscr(); settextstyle(2,0,7); outtextxy(5,350,"N->New Game"); outtextxy(450,350,"M->Main Menu"); char another; another=getch(); if(another=='n' || another=='N') new_game(); else if(another=='m'||another=='M') front_page(); } } /*************************** End of Check() **********************************/

/************************* Play() *******************************************/ void play ()


{ int i=0; clrscr(); // initiateboard(); for(;;) { if(i==9) break; // player_1 (); mapboard(); check(); i++;

if(i==9) break; player_2 (); // mapboard (); check (); i++; } i=0; setcolor(GREEN); settextstyle(2,0,8); outtextxy(230,100,"Oops!Game Drawn"); settextstyle(2,0,7); outtextxy(5,380,"N->New Game"); outtextxy(480,380,"M->Main Menu"); char another_game; another_game=getch(); if(another_game=='n' || another_game=='N') new_game(); else if(another_game=='m'||another_game=='M') front_page(); } /************************ End of Play() ************************************/


/****************** New Game ****************/ void new_game() { initiateboard(); play(); clrscr(); closegraph(); } /************* End of New Game() **************/

/************** Instruction() ***********/ void instruction() { clrscr(); int gdriver=DETECT,gmode; initgraph(&gdriver,&gmode,"C:\\TC\\BGI"); char r; clrscr(); settextstyle(1,0,6); setcolor(RED); outtextxy(5,10,"Instruction"); outtextxy(5,35,"___________"); settextstyle(2,0,6); setcolor(BLUE); outtextxy(5,130,"1.Always try to win the game positioning your key in a Trio."); outtextxy(5,170,"2.Try to block the cells let not to win the game for oponents."); settextstyle(2,0,8); setcolor(RED); outtextxy(5,400,"M->Main Menu"); outtextxy(500,400,"E->Exit"); r=getch();


if(r=='m'||r=='M') front_page(); else exit(0); } /********************* End of Instruction () **************/ / /******************** Resume() ***************************/ void resume() { play(); } /******************* end of Resume() *********************/

1.5 Output:
Outputs of this project are follows: a)Player 1 Win


b) Player 2 Win


1.6 Results:
Using the algorithm a Tic Tac Toe game is made, which can be played by two players. The Player-1 plays with key X and Player-2 plays with key O. Hence, we show some steps of tic tac toe game,where player-1 wins.


a) the board for tic tac toe game:


b) the X for player 1:


c) the next pass 0 for player 2:


d) the pass for player 1 again:


e) player 2 blocks player 1 :


f) player 1 makes a fork:


g) player 2 block player 1 in one way:


h) player 1 wins the game:

1.7 Discussion:
1.7(a) Limitations:
1. Graphics is not so attractive. 2. Only keyboard is used as default playing device , mouse is not used. Mouse interface is not implemented in the game.


1.7(b) Future plan:

1. Well have tried to give a mouse interrupt. 2. We want to design more complex boards for the game at future. 3. We want to improve the graphics of the game at future. 4.Well try our best to give the shape of the board in a 3D manner. 5. It will always remain in our consideration to turn the game from single 33 grids into multiple 33 grids.

1.8 Conclusion:
At the beginning of this project, we want to make a Tic Tac Toe game and at the end of this project we made this. After preparing the papers, we got the answers of the following questions What is Tic Tac Toe game? Is it possible to run the game into Computer?


But still we have some limitations such as Graphics is not so attractive. Mouse interrupt is not used.

1.8(a) Recommendation:
Graphics quality should be improved. Mouse interrupt should be used. GUI should be used more etc.

1.9 Bibliography:
1)Tic-tac-toe From Wikipedia, the free encyclopedia Web: 2)C++ Tutorial Web: 3) How to make a flowchart for Tic Tac Toe


Web: 4)Tic Tac Toe Web: 5) C graphics tutorial Web: 6) Graphics in C Language Web: 7)Turbo C++ graphics programming Web: 8)Jeff Kent(2004), C++ Demystified,McGraw-Hill Publications,New York.

2.0 Appendix:
After reading this Appendix ,you will be able to know and play Tic Tac Toe.

There are at most two players in a game. It is possible that one player is human and other is computer.Two humans may also be players. It is not recommended to use one cell more than once.


Each player should try to block the best cell to avoid the winning of opponent player.