This is an enhanced version of Tic-Tac-Toe (TTT) game by adding one more row and column. Actually I have played this newer version of Tic-Tac-Toe (TTT) game on a video game. So, when I learnt C/C++ programming, it was my wish to program this game myself using C/C++.
As in basic Tic-Tac-Toe game game you win when you complete a line either in vertical, horizontal or diagonal orientation. Same in this game you will win when you have same sign in all four boxes, either horizontally, vertically or diagonally.
Also, this makes the game more interesting when you have also some sequence in your sign in different boxes, eg. if you have same sign say ‘x’ in the same column of 4 different boxes, or you have ‘x’ in first box of first column, second box in second column, third box in third column, fourth box in fourth column. There are 13 ways you can win. You can try the game and try to understand the winning sequence. Its very interesting game.
I developed it to play it with another player. But, when you will try to play this game with computer, the computer will give you very easy response, so that you can win easily. I am still working on computer’s play by using artificial intelligence. Still, you can play it with another player giving a tough and interesting competition.
If you want further information you can contact me at dheerajvaid[at]hotmail.com or on my mobile +91-93161-61666 & also thanks to Administrator to place my code in his site.
#include <iostream.h> #include <conio.h> #include <stdlib.h> #include <dos.h> #include <math.h> #include <string.h> #include <stdio.h> #include <time.h> #define HORZ_BAR 205 #define VERT_BAR 186 #define UP_LEFT_CORNER 201 #define LO_LEFT_CORNER 200 #define UP_RIGHT_CORNER 187 #define LO_RIGHT_CORNER 188 #define MIDDLE_JOINT 206 #define RIGHT_JOINT 185 #define LEFT_JOINT 204 #define UPPER_JOINT 203 #define LOWER_JOINT 202 #define SOLID_CURSOR 219 #define FADED_CURSOR 219 #define UP 72 #define DOWN 80 #define LEFT 75 #define RIGHT 77 #define ESC 27 void drawMainWindow(void); void playMusic(void); void drawPlayBox(int, int); void drawRect(int, int, int, int); void playGame(void); void clearCursor(); void moveCursor(int); void quitGame(void); void errorTone(void); int win(int&, int&); void resetGame(void); void machineTurn(void); int curX = 7, curY = 4, globalX = 0, globalY = 0, scoreX = 0, scoreO = 0; char mark[16][16] = {' '}; enum {TURN_X = 0, TURN_O = 1}; int turn = TURN_X, playMachine = 0; int findX[] = {7, 11, 15, 19, 25, 29, 33, 37, 43, 47, 51, 55, 61, 65, 69, 73}; int findY[] = {4, 6, 8, 10, 8, 10, 12, 14, 12, 14, 16, 18, 16, 18, 20, 22}; char name[2][10]; void resetGame(void) { globalX = 0, globalY = 0; curX = 7, curY = 4; textcolor(4); textbackground(4); for(int i = 0; i < 16; i++) for(int j = 0; j < 16; j++) mark[i][j] = ' '; for(i = 7; i <= 19; i+= 4) { for(j = 4; j <= 10; j += 2) { gotoxy(i, j); cprintf(" "); } } for(i = 25; i <= 37; i += 4) { for(int j = 8; j <= 14; j += 2) { gotoxy(i, j); cprintf(" "); } } for(i = 43; i <= 55; i += 4) { for(int j = 12; j <= 18; j += 2) { gotoxy(i, j); cprintf(" "); } } for(i = 61; i <= 73; i += 4) { for(int j = 16; j <= 22; j += 2) { gotoxy(i, j); cprintf(" "); } } gotoxy(31, 21); textcolor(4); textbackground(4); cprintf(" "); textcolor(4); textbackground(4); gotoxy(25, 22); cprintf(" "); textbackground(4); textcolor(2); gotoxy(curX, curY); cprintf("%c",SOLID_CURSOR); playGame(); } void wasteTime(void) { textcolor(0); textbackground(4); gotoxy(42, 6); cprintf("Thinking!!!"); for(int i = 100; i <= 500 + (random(15) * 100); i += 100) { sound(i); delay(100); nosound(); delay(50); } nosound(); textbackground(4); gotoxy(42, 6); cprintf(" "); } void machineTurn(void) { int machineX, machineY; wasteTime(); do { machineX = random(16); machineY = random(4); if(machineX < 4) machineY = machineY; else if(machineX < 8) machineY += 4; else if(machineX < 12) machineY += 8; else if(machineX < 16) machineY += 12; //gotoxy(36,5); //cprintf("x = %d, y = %d, mark = \'%c\'", machineX, machineY, mark[machineX][machineY]); }while(mark[machineX][machineY] == 'X' || mark[machineX][machineY] == 'O'); textcolor(0); textbackground(4); gotoxy(curX, curY); if(mark[globalX][globalY] != ' ') { cprintf("X"); } else { textcolor(4); cprintf(" "); } textcolor(0 + BLINK); textbackground(2); curX = findX[machineX]; curY = findY[machineY]; globalX = machineX; globalY = machineY; //gotoxy(36, 6); //cprintf("CurX = %d, CurY = %d", curX, curY); gotoxy(curX, curY); cprintf("O"); mark[globalX][globalY] = 'O'; sound(500);delay(100);nosound(); } void playGame(void) { int x, y, blink = 0; char keyCode, ch, c; while(1) { textcolor(0); textbackground(2); gotoxy(33, 4); cprintf("%28c",' '); gotoxy(33, 4); cprintf(" Turn of \'%s\' (%c)",((turn) ? strupr(name[1]) : strupr(name[0])), ((turn) ? 'O' : 'X')); if(playMachine == 1 && turn == TURN_O) { machineTurn(); turn = TURN_X; } else { keyCode = getch(); switch(keyCode) { case 0: moveCursor(getch()); break; case 'x': case 'X': if(turn == TURN_X && mark[globalX][globalY] != 'X' && mark[globalX][globalY] != 'O') { textcolor(0 + BLINK); textbackground(2); gotoxy(curX, curY); cprintf("X"); mark[globalX][globalY] = 'X'; turn = TURN_O; sound(500);delay(100);nosound(); } else errorTone(); break; case 'o': case 'O': if(turn == TURN_O && mark[globalX][globalY] != 'X' && mark[globalX][globalY] != 'O') { textcolor(0 + BLINK); textbackground(2); gotoxy(curX, curY); cprintf("O"); mark[globalX][globalY] = 'O'; turn = TURN_X; sound(500);delay(100);nosound(); } else errorTone(); break; case 27: quitGame(); break; } } blink = win(x, y); if(blink > 0) { c = (turn ? 'X' : 'O'); curX = findX[x]; curY = findY[y]; textcolor(0 + BLINK); textbackground(4); switch(blink) { case 1: gotoxy(curX, curY); cprintf("%c",c); gotoxy(curX + 18, curY + 4); cprintf("%c",c); gotoxy(curX + 36, curY + 8); cprintf("%c",c); gotoxy(curX + 54, curY + 12); cprintf("%c",c); break; case 2: gotoxy(curX, curY); cprintf("%c",c); gotoxy(curX + 4, curY); cprintf("%c",c); gotoxy(curX + 8, curY); cprintf("%c",c); gotoxy(curX + 12, curY); cprintf("%c",c); break; case 3: gotoxy(curX, curY); cprintf("%c",c); gotoxy(curX, curY + 2); cprintf("%c",c); gotoxy(curX, curY + 4); cprintf("%c",c); gotoxy(curX, curY + 6); cprintf("%c",c); break; case 4: gotoxy(curX, curY); cprintf("%c",c); gotoxy(curX + 4, curY + 2); cprintf("%c",c); gotoxy(curX + 8, curY + 4); cprintf("%c",c); gotoxy(curX + 12, curY + 6); cprintf("%c",c); break; case 5: gotoxy(curX, curY); cprintf("%c",c); gotoxy(curX - 4, curY + 2); cprintf("%c",c); gotoxy(curX - 8, curY + 4); cprintf("%c",c); gotoxy(curX - 12, curY + 6); cprintf("%c",c); break; case 6: gotoxy(curX, curY); cprintf("%c",c); gotoxy(curX + 22, curY + 4); cprintf("%c",c); gotoxy(curX + 44, curY + 8); cprintf("%c",c); gotoxy(curX + 66, curY + 12); cprintf("%c",c); break; case 7: gotoxy(curX, curY); cprintf("%c",c); gotoxy(curX + 18, curY + 6); cprintf("%c",c); gotoxy(curX + 36, curY + 12); cprintf("%c",c); gotoxy(curX + 54, curY + 18); cprintf("%c",c); break; case 8: gotoxy(curX, curY); cprintf("%c",c); gotoxy(curX + 14, curY + 4); cprintf("%c",c); gotoxy(curX + 28, curY + 8); cprintf("%c",c); gotoxy(curX + 42, curY + 12); cprintf("%c",c); break; case 9: gotoxy(curX, curY); cprintf("%c",c); gotoxy(curX + 18, curY + 2); cprintf("%c",c); gotoxy(curX + 36, curY + 4); cprintf("%c",c); gotoxy(curX + 54, curY + 6); cprintf("%c",c); break; case 10: gotoxy(7, 4); cprintf("%c",c); gotoxy(29, 10); cprintf("%c",c); gotoxy(51, 16); cprintf("%c",c); gotoxy(73, 22); cprintf("%c",c); break; case 11: gotoxy(19, 4); cprintf("%c",c); gotoxy(33, 10); cprintf("%c",c); gotoxy(47, 16); cprintf("%c",c); gotoxy(61, 22); cprintf("%c",c); break; case 12: gotoxy(19, 10); cprintf("%c",c); gotoxy(33, 12); cprintf("%c",c); gotoxy(47, 14); cprintf("%c",c); gotoxy(61, 16); cprintf("%c",c); break; case 13: gotoxy(7, 10); cprintf("%c",c); gotoxy(29, 12); cprintf("%c",c); gotoxy(51, 14); cprintf("%c",c); gotoxy(73, 16); cprintf("%c",c); break; } textcolor(4); textbackground(4); gotoxy(33,4); cprintf("%28c",' '); gotoxy(31, 21); textcolor(0); textbackground(2); cprintf("\'%s\' won the game",strupr((turn) ? name[0] : name[1])); if(turn) { scoreX += 100; gotoxy(18, 16); cprintf("%-4d",scoreX); turn = TURN_X; } else { scoreO += 100; gotoxy(18, 17); cprintf("%-4d",scoreO); turn = TURN_O; } while(!kbhit()) { sound(500);delay(100);nosound();delay(100); sound(400);delay(300);nosound();delay(100); sound(600);delay(100);nosound();delay(100); sound(700);delay(300);nosound();delay(100); sound(600);delay(50);nosound();delay(100); sound(500);delay(100);nosound();delay(100); } textcolor(0); textbackground(2); gotoxy(25, 22); cprintf("Do you want to play again(Y\\N)?"); do { ch = getch(); }while (!(ch == 'y' || ch == 'Y' || ch == 'n' || ch == 'N')); if (ch == 'n' || ch == 'N') { textmode(C40); _setcursortype(_NOCURSOR); delay(1000); textcolor(14 + BLINK); gotoxy(13, 12); cprintf("Thanx for playing!"); for(int i = 100; i <= 1000; i += 100) { sound(i); delay(200); } nosound(); textmode(C80); exit(1); } else { resetGame(); break; } } //textcolor(0); //textbackground(4); //gotoxy(5,20); //cprintf("x = %2d, y = %2d, value = \'%c\'", globalX, globalY, mark[globalX][globalY]); } } int win(int& x, int& y) { for(x = 0; x < 4; x++) //same position in different blocks for(y = 0; y < 4; y++) if(mark[x][y] == mark[x + 4][y + 4] && mark[x][y] == mark[x + 8][y + 8] && mark[x][y] == mark[x + 12][y + 12] && (mark[x][y] == 'X' || mark[x][y] == 'O')) return 1; for(x = 0; x < 16; x += 4) //horizontal sequence same block for(y = x; y < (x + 4); y++) if(mark[x][y] == mark[x + 1][y] && mark[x][y] == mark[x + 2][y] && mark[x][y] == mark[x + 3][y] && (mark[x][y] == 'X' || mark[x][y] == 'O')) return 2; for(y = 0; y < 16; y += 4) //vertical sequence same block for(x = y; x < (y + 4); x++) if(mark[x][y] == mark[x][y + 1] && mark[x][y] == mark[x][y + 2] && mark[x][y] == mark[x][y + 3] && (mark[x][y] == 'X' || mark[x][y] == 'O')) return 3; for(x = 0; x < 16; x += 4) //diagonal \ in same block if(mark[x][x] == mark[x + 1][x + 1] && mark[x][x] == mark[x + 2][x + 2] && mark[x][x] == mark[x + 3][x + 3] && (mark[x][x] == 'X' || mark[x][x] == 'O')) { y = x; return 4; } for(x = 3; x < 16; x += 4) //diagonal / in same block if(mark[x][x - 3] == mark[x - 1][x - 2] && mark[x][x - 3] == mark[x - 2][x - 1] && mark[x][x - 3] == mark[x - 3][x - 0] && (mark[x][x - 3] == 'X' || mark[x][x - 3] == 'O')) { y = x - 3; return 5; } for(x = 0, y = 0; y < 4; y++) //horizontal sequence in diff. bolcks if(mark[x][y] == mark[x + 5][y + 4] && mark[x][y] == mark[x + 10][y + 8] && mark[x][y] == mark[x + 15][y + 12] && (mark[x][y] == 'X' || mark[x][y] == 'O')) return 6; for(y = 0, x = 0; x < 4; x++) // vertical sequence in diff. blocks if(mark[x][y] == mark[x + 4][y + 5] && mark[x][y] == mark[x + 8][y + 10] && mark[x][y] == mark[x + 12][y + 15] && (mark[x][y] == 'X' || mark[x][y] == 'O')) return 7; for(x = 3, y = 0; y < 4; y++) //horizontal sequence backward if(mark[x][y] == mark[x + 3][y + 4] && mark[x][y] == mark[x + 6][y + 8] && mark[x][y] == mark[x + 9][y + 12] && (mark[x][y] == 'X' || mark[x][y] == 'O')) return 8; for(y = 3, x = 0; x < 4; x++) //vertical sequence backward if(mark[x][y] == mark[x + 4][y + 3] && mark[x][y] == mark[x + 8][y + 6] && mark[x][y] == mark[x + 12][y + 9] && (mark[x][y] == 'X' || mark[x][y] == 'O')) return 9; if(mark[0][0] == mark[5][5] && mark[0][0] == mark[10][10] && mark[0][0] == mark[15][15] && (mark[0][0] == 'X' || mark[0][0] == 'O')) return 10; //vertically diagonal downward from left if(mark[3][0] == mark[6][5] && mark[3][0] == mark[9][10] && mark[3][0] == mark[12][15] && (mark[3][0] == 'X' || mark[3][0] == 'O')) return 11; //vertically diagonal downward from right if(mark[3][3] == mark[6][6] && mark[3][3] == mark[9][9] && mark[3][3] == mark[12][12] && (mark[3][3] == 'X' || mark[3][3] == 'O')) return 12; //vertically diagonal upward from right if(mark[0][3] == mark[5][6] && mark[0][3] == mark[10][9] && mark[0][3] == mark[15][12] && (mark[0][3] == 'X' || mark[0][3] == 'O')) return 13; //vertically diagoanl upward from left return 0; } void errorTone(void) { sound(500);delay(50);nosound(); sound(600);delay(100);nosound(); } void quitGame() { char ch; textcolor(0); textbackground(2); gotoxy(25, 22); cprintf("Do you really want to quit(Y\\N)?"); do { ch = getch(); }while (!(ch == 'y' || ch == 'Y' || ch == 'n' || ch == 'N')); if (ch == 'y' || ch == 'Y') { textmode(C40); _setcursortype(_NOCURSOR); delay(1000); textcolor(14 + BLINK); gotoxy(13, 12); cprintf("Thanx for playing!"); for(int i = 100; i <= 1000; i += 100) { sound(i); delay(200); } nosound(); textmode(C80); exit(1); } textcolor(4); textbackground(4); gotoxy(25, 22); cprintf("%32c",' '); } void moveCursor(int move) { if(mark[globalX][globalY] != 'X' && mark[globalX][globalY] != 'O') { clearCursor(); } else { textcolor(0); textbackground(4); gotoxy(curX, curY); cprintf("%c",mark[globalX][globalY]); } switch(move) { case UP: curY -= 2; if(curY == 2 && curX >= 7) { curY = 22; curX = curX + 54; globalY = 16; globalX += 12;} if(curY == 6 && curX >= 25) { curY = 10; curX = curX - 18; globalX -= 4;} if(curY == 10 && curX >= 43) { curY = 14; curX = curX - 18; globalX -= 4;} if(curY == 14 && curX >= 61) { curY = 18; curX = curX - 18; globalX -= 4;} globalY--; break; case LEFT: curX -= 4; if(curX == 3 && curY <= 10) { curX = 73; curY = curY + 12; globalY += 12; globalX = 16;} if(curX == 21 && curY <= 14) { curX = 19; curY -= 4; globalY -= 4;} if(curX == 39 && curY <= 18) { curX = 37; curY -= 4; globalY -= 4;} if(curX == 57 && curY <= 22) { curX = 55; curY -= 4; globalY -= 4;} globalX--; break; case RIGHT: curX += 4; if(curX == 23 && curY <= 10) { curX = 25; curY += 4; globalY += 4;} if(curX == 41 && curY <= 14) { curX = 43; curY += 4; globalY += 4;} if(curX == 59 && curY <= 18) { curX = 61; curY += 4; globalY += 4;} if(curX == 77 && curY <= 22) { curX = 7; curY -= 12; globalY -= 12; globalX = -1;} globalX++; break; case DOWN: curY += 2; if(curY == 12 && curX <= 21) { curY = 8; curX = curX + 18; globalX += 4;} if(curY == 16 && curX <= 37) { curY = 12; curX = curX + 18; globalX += 4;} if(curY == 20 && curX <= 55) { curY = 16; curX = curX + 18; globalX += 4;} if(curY == 24 && curX <= 73) { curY = 4; curX = curX - 54; globalY = -1; globalX -= 12 ;} globalY++; break; } if(mark[globalX][globalY] != 'X' && mark[globalX][globalY] != 'O') { textcolor(2); textbackground(4); gotoxy(curX, curY); cprintf("%c",SOLID_CURSOR); } else { textcolor(0); textbackground(2); gotoxy(curX, curY); cprintf("%c",mark[globalX][globalY]); } } void clearCursor() { if(mark[globalX][globalY] != 'X' && mark[globalX][globalY] != 'O') { textcolor(4); textbackground(4); gotoxy(curX, curY); cprintf("%c",219); } else { textcolor(0); textbackground(2); gotoxy(curX, curY); cprintf("%c",mark[globalX][globalY]); } } void drawMainWindow() { int i, j, k, l; char option; textmode(C40); _setcursortype(_NOCURSOR); textcolor(7); textbackground(4); gotoxy(15, 1); cprintf("TIC-TAC-TOE"); gotoxy(3, 3); cprintf("Play with another player(y\\n)?"); option = getche(); gotoxy(10,5); cprintf("Player X :: "); cin.getline(name[0], 10); strupr(name[0]); fflush(stdin); fflush(stdout); if(option != 'y' && option != 'Y') { strcpy(name[1], "MACHINE"); playMachine = 1; } else { gotoxy(10, 7); cprintf("Player O :: "); cin.getline(name[1], 10); strupr(name[1]); playMachine = 0; } randomize(); int check = random(2); textcolor(15 + BLINK); textbackground(0); gotoxy(12,13); if(check == 0) { cprintf("Turn of %s",name[0]); turn = TURN_X; } else { cprintf("Turn of %s",name[1]); turn = TURN_O; } getch(); textmode(C80); delay(1000); _setcursortype(_NOCURSOR); _wscroll = -1; textbackground(4); clrscr(); textcolor(14); drawRect(1, 1, 80, 25); textcolor(0); for(i = 3 ; i <= 78; i++) { gotoxy(i, 2); cprintf("%c",SOLID_CURSOR); gotoxy(i, 24); cprintf("%c",SOLID_CURSOR); delay(5); sound(pow(i, 2) + 100); } for(i = 3 ; i <= 24; i++) { gotoxy(3, i); cprintf("%c",SOLID_CURSOR); gotoxy(78, i); cprintf("%c",SOLID_CURSOR); delay(5); sound(pow(i, 2) + 100); } playMusic(); nosound(); delay(500); textcolor(14); textbackground(4); sound(100); delay(200); drawPlayBox(5, 3); sound(200); delay(200); drawPlayBox(23, 7); sound(400); delay(200); drawPlayBox(41, 11); sound(800); delay(200); drawPlayBox(59, 15); delay(200); textcolor(14); textbackground(0); for(i = 3 ; i <= 78; i++) { gotoxy(i, 2); cprintf("%c",4); if(i <= 40) { gotoxy(i, 24); cprintf("%c",4); } } for(i = 3 ; i <= 24; i++) { gotoxy(3, i); cprintf("%c",4); gotoxy(78, i); cprintf("%c",4); } nosound(); textcolor(14); textbackground(0); gotoxy(35, 2); cprintf("TIC-TAC-TOE"); textbackground(4); textcolor(2); gotoxy(curX, curY); cprintf("%c",SOLID_CURSOR); gotoxy(5, 19); textcolor(0); textbackground(2); cprintf("ESC -> Exit "); gotoxy(5, 20); cprintf("Player X -> X "); gotoxy(5, 21); cprintf("Player O -> O "); gotoxy(5, 22); cprintf("Move -> Arrow Keys"); gotoxy(5, 14); cprintf(" S C O R E "); gotoxy(5, 16); cprintf("%-10s : %-4d",strupr(name[0]), scoreX); gotoxy(5, 17); cprintf("%-10s : %-4d",strupr(name[1]), scoreO); } void drawRect(int x1, int y1, int x2, int y2) { int i, j; for(i = x1; i <= x2; i++) { gotoxy(i, y1); cprintf("%c",HORZ_BAR); gotoxy(i, y2); cprintf("%c",HORZ_BAR); } for(i = y1; i <= y2; i++) { gotoxy(x1, i); cprintf("%c",VERT_BAR); gotoxy(x2, i); cprintf("%c",VERT_BAR); } gotoxy(x1, y1); cprintf("%c",UP_LEFT_CORNER); gotoxy(x2, y1); cprintf("%c",UP_RIGHT_CORNER); gotoxy(x1, y2); cprintf("%c",LO_LEFT_CORNER); gotoxy(x2, y2); cprintf("%c",LO_RIGHT_CORNER); } void drawPlayBox(int x, int y) { int i, j; for(i = x; i <= x + 16; i += 1) { for(j = y; j <= y + 9; j += 2) { gotoxy(i, j); cprintf("%c",HORZ_BAR); } } for(i = x; i <= x + 16; i += 4) { for(j = y; j <= y + 8; j += 1) { gotoxy(i, j); if(i == x && (j == y + 2 || j == y + 4 || j == y + 6)) cprintf("%c",LEFT_JOINT); else if(i == x + 16 && (j == y + 2 || j == y + 4 || j == y + 6)) cprintf("%c",RIGHT_JOINT); else if(j == y && (i == x + 4 || i == x + 8 || i == x + 12)) cprintf("%c",UPPER_JOINT); else if(j == y + 8 && (i == x + 4 || i == x + 8 || i == x + 12)) cprintf("%c",LOWER_JOINT); else if((i == x + 4 || i == x + 8 || i == x + 12) && (j == y + 2 || j == y + 4 || j == y + 6)) cprintf("%c",MIDDLE_JOINT); else if(i == x && j == y) cprintf("%c",UP_LEFT_CORNER); else if(i == x + 16 && j == y) cprintf("%c",UP_RIGHT_CORNER); else if(i == x && j == y + 8) cprintf("%c",LO_LEFT_CORNER); else if(i == x + 16 && j == y + 8) cprintf("%c",LO_RIGHT_CORNER); else cprintf("%c",VERT_BAR); } } } void playMusic(void) { int i, j; textbackground(0); textcolor(14); for(i = 4; i <= 54; i++) { gotoxy(i, 24); cprintf(" %s","dheerajvaid@hotmail.com"); delay(5); sound(i * 50); } textcolor(14); for(i = 4; i <= 40; i++) { gotoxy(i, 24); cprintf(" %s","Programmed By:"); delay(5); sound(i * 100); } nosound(); } void main() { randomize(); drawMainWindow(); playGame(); getch(); }