Hello My first game of TicTacToe and thought I will put the same here after the Game of Mojo and Max 8 Note :- This will compile in Turbo C 3 Only and the zip contains the executable Version also for you to see. Code: #include <iostream.h> #include <conio.h> #include <stdlib.h> //For exit(0) function #include <dos.h> //For sleep function #include <string.h> //For strlen /*************************\ TicTacToe's Game \*************************/ void rules(void); //Rules are defined void help(void); //Help menu void play(void); //Play menu void restart(void); //Restarting game again void credits(void); //Displays credits void draw(void); //Drawing the required table void printplayer(int); //Prints the symbol 'X' void printcomp(int); //Prints the symbol 'O' void result(void); //Displays result char name[100]; //Takes user name /***************************/ /***** Main function *****/ /***************************/ void main() { int ch; //Choice of the user top: cout<<"\nEnter your name\n"; cin.getline(name,100); clrscr(); gotoxy(10,2); cout<<"This program is a program for the game \"TIC TAC TOE\"\n\n"; cout<<"1. Rules\n" <<"2. Help\n" <<"3. play\n" <<"4. Restart\n" <<"5. Credits\n" <<"6. Quit\n"; cin>>ch; switch(ch) { case(1): rules(); goto top; case(2): help(); goto top; case(3): draw(); play(); result(); break; case(4): restart(); goto top; case(5): credits(); goto top; case(6): exit(0); default: play(); break; } cout<<"\nPress any key to continue\t"; getch(); } /**************************************/ /***** Function displaying rules *****/ /**************************************/ void rules(void) { cout<<"The rules of the game are as follows:-\n\n" <<"Press the number indicated near the playing box \n" <<"to put the symbol \" X \" in the playing box.\n" <<"\nDo not press a number in which a symbol such as\n" <<"\"'X' or 'O'\" already exist. If you do so the\n" <<"program will terminate due to illegal inputs\n" <<"\nYour aim is to get a line containing 3 'X'\n" <<"either hrizontally or vertically or diagonally\n" <<"with alternative inputs by you and computer\n" <<"\npress any key to continue\t"; getch(); } /**************************************/ /***** Function displaying help *****/ /**************************************/ void help(void) { cout<<"As the rule of the game get a line containing 3 'X'\n" <<"either hrizontally or vertically or diagonally\n" <<"with alternative inputs by you and computer.\n" <<"\nAlways try to occupy the middle box and\n" <<"then the corners for confusing the computer\n" <<"\nPress any key to continue\t"; getch(); } int place[9]={0,0,0, //User inputs the number 0,0,0, //to print the symbol 0,0,0}; //at the desired place int flag[9]={0,0,0, //Checks for the 0,0,0, //double entry 0,0,0}; //of the same place /*******************************************/ /***** Function invoking user to play *****/ /*******************************************/ void play(void) { int i,j,k,l; //Looping variables int print; //Checks for the computer move int win; //Checks for the computer winning condition static int first=0; //Checks for first move not executed twice static int one23=0; //Checks for 1 2 3 row output static int four56=0; //Checks for 4 5 6 row output static int seven89=0; //Checks for 7 8 9 row output static int one47=0; //Checks for 1 4 7 column output static int two58=0; //Checks for 2 5 8 column output static int three69=0; //Checks for 3 6 9 column output static int one59=0; //Checks for 1 5 9 diagonal output static int three57=0; //Checks for 3 5 7 diagonal output gotoxy(10,20); for(i=0;i<9;i+=2) { cin>>place[i]; print=0; if(flag[(place[i]-1)]!=0) { cout<<"Read the rules before you play again\n"; cout<<"Sorry press any key to exit\t"; getch(); exit(0); } printplayer(place[i]); flag[place[i]-1]=1; if(place[0]==5 && flag[0]==0) { printcomp(1); flag[0]=2; print++; first=1; } if(place[0]!=5 && first==0) { printcomp(5); flag[4]=2; print++; first=1; } for(win=0;win<=6;win+=3) //Loop of winning condition by row { if(flag[0+win]==0 && flag[1+win]==2 && flag[2+win]==2) { printcomp(1+win); flag[0+win]=2; print++; if(win==0) one23=1; if(win==3) four56=1; if(win==6) seven89=1; result(); } if(flag[0+win]==2 && flag[1+win]==0 && flag[2+win]==2) { printcomp(2+win); flag[1+win]=2; print++; if(win==0) one23=1; if(win==3) four56=1; if(win==6) seven89=1; result(); } if(flag[0+win]==2 && flag[1+win]==2 && flag[2+win]==0) { printcomp(3+win); flag[2+win]=2; print++; if(win==0) one23=1; if(win==3) four56=1; if(win==6) seven89=1; result(); } } for(win=0;win<=2;win++) //Loop of winning condition by column { if(flag[0+win]==0 && flag[3+win]==2 && flag[6+win]==2) { printcomp(1+win); flag[0+win]=2; print++; if(win==0) one47=1; if(win==1) two58=1; if(win==2) three69=1; result(); } if(flag[0+win]==2 && flag[3+win]==0 && flag[6+win]==2) { printcomp(4+win); flag[3+win]=2; print++; if(win==0) one47=1; if(win==1) two58=1; if(win==2) three69=1; result(); } if(flag[0+win]==2 && flag[3+win]==2 && flag[6+win]==0) { printcomp(7+win); flag[6+win]=2; print++; if(win==0) one47=1; if(win==1) two58=1; if(win==2) three69=1; result(); } } for(win=0;win<=2;win+=2) //Loop of winning condition diagonally { if(flag[0+win]==0 && flag[4]==2 && flag[8-win]==2) { printcomp(1+win); flag[0+win]=2; print++; if(win==0) one59=1; if(win==2) three57=1; result(); } if(flag[0+win]==2 && flag[4]==0 && flag[8-win]==2) { printcomp(5); flag[4]=2; print++; if(win==0) one59=1; if(win==2) three57=1; result(); } if(flag[0+win]==2 && flag[4]==2 && flag[8-win]==0) { printcomp(9-win); flag[8-win]=2; print++; if(win==0) one59=1; if(win==2) three57=1; result(); } } for(k=0;k<=4;k+=4) //Loop for testing 1 5 9 output for(j=0;j<=4;j+=4) if((place[0]==1+j-k && place[2]==5+j|| place[0]==1+j-k && place[4]==5+j|| place[0]==1+j-k && place[6]==5+j|| place[0]==1+j-k && place[8]==5+j|| place[2]==1+j-k && place[0]==5+j|| place[2]==1+j-k && place[4]==5+j|| place[2]==1+j-k && place[6]==5+j|| place[2]==1+j-k && place[8]==5+j|| place[4]==1+j-k && place[0]==5+j|| place[4]==1+j-k && place[2]==5+j|| place[4]==1+j-k && place[6]==5+j|| place[4]==1+j-k && place[8]==5+j|| place[6]==1+j-k && place[0]==5+j|| place[6]==1+j-k && place[2]==5+j|| place[6]==1+j-k && place[4]==5+j|| place[6]==1+j-k && place[8]==5+j|| place[6]==1+j-k && place[8]==5+j|| place[6]==1+j-k && place[8]==5+j|| place[8]==1+j-k && place[0]==5+j|| place[8]==1+j-k && place[2]==5+j|| place[8]==1+j-k && place[4]==5+j|| place[8]==1+j-k && place[6]==5+j) && one59==0 && print==0) { one59=1; if(j==0 && k==0 && flag[8]==0) { printcomp(9); flag[8]=2; print++; } if(j==4 && k==0 && flag[0]==0) { printcomp(1); flag[0]=2; print++; } if(j==4 && k==4 && flag[4]==0) { printcomp(5); flag[4]=2; print++; } } for(k=0;k<=2;k+=2) //loop for testing 3 5 7 output for(j=0;j<=2;j+=2) if((place[0]==3+j-k && place[2]==5+j|| place[0]==3+j-k && place[4]==5+j|| place[0]==3+j-k && place[6]==5+j|| place[0]==3+j-k && place[8]==5+j|| place[2]==3+j-k && place[0]==5+j|| place[2]==3+j-k && place[4]==5+j|| place[2]==3+j-k && place[6]==5+j|| place[2]==3+j-k && place[8]==5+j|| place[4]==3+j-k && place[0]==5+j|| place[4]==3+j-k && place[2]==5+j|| place[4]==3+j-k && place[6]==5+j|| place[4]==3+j-k && place[8]==5+j|| place[6]==3+j-k && place[0]==5+j|| place[6]==3+j-k && place[2]==5+j|| place[6]==3+j-k && place[4]==5+j|| place[6]==3+j-k && place[8]==5+j|| place[6]==3+j-k && place[8]==5+j|| place[6]==3+j-k && place[8]==5+j|| place[8]==3+j-k && place[0]==5+j|| place[8]==3+j-k && place[2]==5+j|| place[8]==3+j-k && place[4]==5+j|| place[8]==3+j-k && place[6]==5+j) && three57==0 && print==0) { three57=1; if(j==0 && k==0 && flag[6]==0) { printcomp(7); flag[6]=2; print++; } if(j==2 && k==0 && flag[2]==0) { printcomp(3); flag[2]=2; print++; } if(j==2 && k==2 && flag[4]==0) { printcomp(5); flag[4]=2; print++; } } for(l=0;l<7;l+=3) //Loop for testing row output for(k=0;k<2;k++) for(j=0;j<2;j++) if((place[0]==1+j-k+l && place[2]==2+j+l|| place[0]==1+j-k+l && place[4]==2+j+l|| place[0]==1+j-k+l && place[6]==2+j+l|| place[0]==1+j-k+l && place[8]==2+j+l|| place[2]==1+j-k+l && place[0]==2+j+l|| place[2]==1+j-k+l && place[4]==2+j+l|| place[2]==1+j-k+l && place[6]==2+j+l|| place[2]==1+j-k+l && place[8]==2+j+l|| place[4]==1+j-k+l && place[0]==2+j+l|| place[4]==1+j-k+l && place[2]==2+j+l|| place[4]==1+j-k+l && place[6]==2+j+l|| place[4]==1+j-k+l && place[8]==2+j+l|| place[6]==1+j-k+l && place[0]==2+j+l|| place[6]==1+j-k+l && place[2]==2+j+l|| place[6]==1+j-k+l && place[4]==2+j+l|| place[6]==1+j-k+l && place[6]==2+j+l|| place[8]==1+j-k+l && place[0]==2+j+l|| place[8]==1+j-k+l && place[2]==2+j+l|| place[8]==1+j-k+l && place[4]==2+j+l|| place[8]==1+j-k+l && place[6]==2+j+l) && (one23==0) && (four56==0) && (seven89)==0 && print==0) { if(j==0 && k==0 && l==0 && flag[2]==0) { printcomp(3); flag[2]=2; print++; one23=1; } if(j==1 && k==0 && l==0 && flag[0]==0) { printcomp(1); flag[0]=2; print++; one23=1; } if(j==1 && k==1 && l==0 && flag[1]==0) { printcomp(2); flag[1]=2; print++; one23=1; } if(j==0 && k==0 && l==3 && flag[5]==0) { printcomp(6); flag[5]=2; print++; four56=1; } if(j==1 && k==0 && l==3 && flag[3]==0) { printcomp(4); flag[3]=2; print++; four56=1; } if(j==1 && k==1 && l==3 && flag[4]==0) { printcomp(5); flag[4]=2; print++; four56=1; } if(j==0 && k==0 && l==6 && flag[8]==0) { printcomp(9); flag[8]=2; print++; seven89=1; } if(j==1 && k==0 && l==6 && flag[6]==0) { printcomp(7); flag[6]=2; print++; seven89=1; } if(j==1 && k==1 && l==6 && flag[7]==0) { printcomp(8); flag[7]=2; print++; seven89=1; } } for(l=0;l<3;l++) //Loop for testing column output for(k=0;k<4;k+=3) for(j=0;j<4;j+=3) if((place[0]==1+j-k+l && place[2]==4+j+l|| place[0]==1+j-k+l && place[4]==4+j+l|| place[0]==1+j-k+l && place[6]==4+j+l|| place[0]==1+j-k+l && place[8]==4+j+l|| place[2]==1+j-k+l && place[0]==4+j+l|| place[2]==1+j-k+l && place[4]==4+j+l|| place[2]==1+j-k+l && place[6]==4+j+l|| place[2]==1+j-k+l && place[8]==4+j+l|| place[4]==1+j-k+l && place[0]==4+j+l|| place[4]==1+j-k+l && place[2]==4+j+l|| place[4]==1+j-k+l && place[6]==4+j+l|| place[4]==1+j-k+l && place[8]==4+j+l|| place[6]==1+j-k+l && place[0]==4+j+l|| place[6]==1+j-k+l && place[2]==4+j+l|| place[6]==1+j-k+l && place[4]==4+j+l|| place[6]==1+j-k+l && place[8]==4+j+l|| place[8]==1+j-k+l && place[0]==4+j+l|| place[8]==1+j-k+l && place[2]==4+j+l|| place[8]==1+j-k+l && place[4]==4+j+l|| place[8]==1+j-k+l && place[6]==4+j+l) && (one47==0) && (two58==0) && (three69==0) && print==0) { if(j==0 && k==0 && l==0 && flag[6]==0) { printcomp(7); flag[6]=2; print++; one47=1; } if(j==3 && k==0 && l==0 && flag[0]==0) { printcomp(1); flag[0]=2; print++; one47=1; } if(j==3 && k==3 && l==0 && flag[3]==0) { printcomp(4); flag[3]=2; print++; one47=1; } if(j==0 && k==0 && l==1 && flag[7]==0) { printcomp(8); flag[7]=2; print++; two58=1; } if(j==3 && k==0 && l==1 && flag[1]==0) { printcomp(2); flag[1]=2; print++; two58=1; } if(j==3 && k==3 && l==1 && flag[4]==0) { printcomp(5); flag[4]=2; print++; two58=1; } if(j==0 && k==0 && l==2 && flag[8]==0) { printcomp(9); flag[8]=2; print++; three69=1; } if(j==3 && k==0 && l==2 && flag[2]==0) { printcomp(3); flag[2]=2; print++; three69=1; } if(j==3 && k==3 && l==2 && flag[5]==0) { printcomp(6); flag[5]=2; print++; three69=1; } } if(print==0 && flag[0]==0) { printcomp(1); flag[0]=2; print++; } if(print==0 && flag[1]==0) { printcomp(2); flag[1]=2; print++; } if(print==0 && flag[2]==0) { printcomp(3); flag[2]=2; print++; } if(print==0 && flag[3]==0) { printcomp(4); flag[3]=2; print++; } if(print==0 && flag[4]==0) { printcomp(5); flag[4]=2; print++; } if(print==0 && flag[5]==0) { printcomp(6); flag[5]=2; print++; } if(print==0 && flag[6]==0) { printcomp(7); flag[6]=2; print++; } if(print==0 && flag[7]==0) { printcomp(8); flag[7]=2; print++; } if(print==0 && flag[8]==0) { printcomp(9); flag[8]=2; print++; } if(print==0 && flag[0]==0) { gotoxy(60,22); cout<<"Sorry game cannot advance\n"; gotoxy(60,23); cout<<"press any key to exit\n"; getch(); exit(0); } //Checks for player's winning condition if(flag[0]==1 && flag[1]==1 && flag[2]==1|| flag[3]==1 && flag[4]==1 && flag[5]==1|| flag[6]==1 && flag[7]==1 && flag[8]==1|| flag[0]==1 && flag[3]==1 && flag[6]==1|| flag[1]==1 && flag[4]==1 && flag[7]==1|| flag[2]==1 && flag[5]==1 && flag[8]==1|| flag[1]==1 && flag[4]==1 && flag[8]==1|| flag[2]==1 && flag[4]==1 && flag[6]==1) result(); } } /**********************************************/ /***** Player output displaying function *****/ /**********************************************/ void printplayer(int place) //Passing place inputed by the user { if(place==1) { gotoxy(30,6); cout<<"X"; gotoxy(10,20); } else if(place==2) { gotoxy(40,6); cout<<"X"; gotoxy(10,20); } else if(place==3) { gotoxy(50,6); cout<<"X"; gotoxy(10,20); } else if(place==4) { gotoxy(30,11); cout<<"X"; gotoxy(10,20); } else if(place==5) { gotoxy(40,11); cout<<"X"; gotoxy(10,20); } else if(place==6) { gotoxy(50,11); cout<<"X"; gotoxy(10,20); } else if(place==7) { gotoxy(30,16); cout<<"X"; gotoxy(10,20); } else if(place==8) { gotoxy(40,16); cout<<"X"; gotoxy(10,20); } else if(place==9) { gotoxy(50,16); cout<<"X"; gotoxy(10,20); } } /*************************************************/ /***** Computer output displaying function *****/ /*************************************************/ void printcomp(int place) //Passing place for output by computer { if(place==1) { gotoxy(30,6); cout<<"O"; gotoxy(10,20); } else if(place==2) { gotoxy(40,6); cout<<"O"; gotoxy(10,20); } else if(place==3) { gotoxy(50,6); cout<<"O"; gotoxy(10,20); } else if(place==4) { gotoxy(30,11); cout<<"O"; gotoxy(10,20); } else if(place==5) { gotoxy(40,11); cout<<"O"; gotoxy(10,20); } else if(place==6) { gotoxy(50,11); cout<<"O"; gotoxy(10,20); } else if(place==7) { gotoxy(30,16); cout<<"O"; gotoxy(10,20); } else if(place==8) { gotoxy(40,16); cout<<"O"; gotoxy(10,20); } else if(place==9) { gotoxy(50,16); cout<<"O"; gotoxy(10,20); } } /****************************************/ /***** Result displaying function *****/ /****************************************/ void result(void) { int l; //Length of players name l=strlen(name); //Checks for user's winning condition if(flag[0]==1 && flag[1]==1 && flag[2]==1|| flag[3]==1 && flag[4]==1 && flag[5]==1|| flag[6]==1 && flag[7]==1 && flag[8]==1|| flag[0]==1 && flag[3]==1 && flag[6]==1|| flag[1]==1 && flag[4]==1 && flag[7]==1|| flag[2]==1 && flag[5]==1 && flag[8]==1|| flag[0]==1 && flag[4]==1 && flag[8]==1|| flag[2]==1 && flag[4]==1 && flag[6]==1) { gotoxy(40,22); cout.write(name,l); cout<<" You are victorious\n"; cout<<"\n\npress any key to continue\t"; getch(); } else { gotoxy(40,22); cout.write(name,l); cout<<" Sorry better luck next time\n"; cout<<"\n\npress any key to continue\t"; getch(); } exit(0); } /*********************************/ /***** restarting function *****/ /*********************************/ void restart(void) { sleep(1); } /*****************************************/ /***** Credits displaying function *****/ /*****************************************/ void credits(void) { clrscr(); cout<<"Unauthorised copying of this program is not permitted\n\n" <<"Created in \"BORLAND C++\" version 5.02\n\n" <<"Created by \"SHABBIR\"\n\n" <<"Press any key to continue\t"; getch(); } /*************************************/ /***** Figure drawing function *****/ /*************************************/ void draw(void) { int i,j; //Looping variables int num=1; //Number displaying function char h=205; //Horizontal lines char v=186; //Vertical lines char c=206; //center symbol like + for(i=6;i<12;i+=5) { gotoxy(25,i+3); for(j=0;j<=30;j++) cout<<h; } for(j=10;j<=20;j+=10) { for(i=2;i<17;i++) { gotoxy(25+j,i+3); cout<<v; } } for(i=35;i<=45;i+=10) for(j=9;j<=14;j+=5) { gotoxy(i,j); cout<<c; } for(j=0;j<=10;j+=5) for(i=0;i<=20;i+=10) { gotoxy(34+i,8+j); cout<<num++; } }
Hello Shabbir, Its been a long time since I downloaded & ran the Tic Tac Toe program (& perhaps longer time since this program was posted) - so, this may seem a little off-the-mark. But, we have been taught Tic Tac Toe's basic algorithm as an application of Trees. i.e., we were taught how tree as a data structure can be used as a game tree to predict the best possible move that can be made by the computer. However, ur program doesn't involve setting up of any Game Tree, yet, almost every time I lose to the PC!! Can u tell me what method hav u adopted in coding?? Also, please tell me how to use "ikbhijit( )" or 2D pointers?? Ciao, Rajiv
How you managed this. Yes but when I wrote that I just knew printf and scanf in C and nothing more. No knowledge of Data structure. Simple logic is I want to win and so first I detect where if any saves I need to do so the opponent does not win then places where I can win and then if both the above fails I put them at hard coded position.
GR8 Logical coding, but what bout "ikbhijit( )" ok. So u used pure logic, which is pretty good work - but what bout "ikbhijit( )". If u could just explain it & its usage in brief, then I will be very thankful to you. (Also, kindly recommend some books for learning such functions & advanced C++) Ciao, Rajiv
Can you tell me what you meant by module ikbhijit(). I dont see any module with that name in my code. Also for learning C the book I used can be found here. Also I prefer online resources and I have clubed them in C++ books and tutorial thread for everyone.
oops - sorry, maybe mixed up someone else's code with yours. My apologies. Thx for the suggesting the book. Ciao, Rajiv
As a matter of interest, I recently coded a game (mostly in Python) that takes a non-traditional approach. It performs no logical evaluation whatsoever, but plays entirely based on history (experience). I does use a tree, sort of a bastardized general tree. Since there are many equivalent positions (a single 'X' in the upper left corner is equivalent to a single 'X' in any corner), the tree contains only a set of Basic Positions. This actually amounts to fewer than 800. The effectiveness of any response is proportional to the number of games, in which it was used, that resulted, ultimately, in a win, loss, or draw. There is a standalone version written in Python with wxWidgets. The bulk of the Python code is also used (server side) in a web version which uses asynchronous http-xml requests to send the player's move and return the machine's response. Each move/response is treated as a single, detached problem, except in training mode. The web version can, of course, be played using any browser that supports asynchronous http-xml (often referred to as AJAX). The standalone version requires that you have Python and wxWidgets available. If there's any interest I will post a link to the web game and another link to a zip file with the code. I notice that there's no Python (or Other Languages) forum, so I'm not sure exactly where it should be posted.
DaWei You can post that in the Programming section and soon we are planning to have python section as we have some resources being added in that direction and it will be moved accordingly.
awesome game... i played it and liked it very much.... AND I need a help...... I have to make a project in c language usiing file handling.....and mouse driven menus.......the project is of about Student management system.... we have to save records about students and also calculate their final assessment, fees, personal data, academic data, and also a search menu in which we can locate a particular student or particular information........plzzzz help me on this plz send me the code on my mail........my id is..........." rishu_sharma3533@yahoo.com " plz help me i need the code within 15 days......