C++ Codes
Algorithms
Algorithm Analysis in C++
Beginners
Code Snippets
Graphics
Data Structures
File Manipulation
Games
Mathematics
Miscellaneous
Visual C++ Library
C++ > Games sample source codes
Graphic Tictactoe - The first ever tictactoe playing artificial intelligence.
Graphic Tictactoe - The first ever tictactoe playing artificial intelligence. None has defeated this computer 'A.I.' . The game is very flexible. Either the user or the 'A.I.' can start the game. User is free to select his own symbol #include
#include
#include
#include
#include
#include
void*message; int select(int mult) { union REGS inregs, outregs ; int bli=1,use=1,key=34,i; settextstyle(2,0,5); while(key!=28) { if(bli>0) { use=bli; setfillstyle(1,0); bli=0-bli; } else if(bli<0) { use=0-bli; setfillstyle(1,8); bli=0-bli; } floodfill(221,111+use*40,15); delay(100); if(bli<0) { key=kbhit(); if(kbhit()) { inregs.h.ah = 0 ; int86(22, &inregs, &outregs) ; key=outregs.h.ah; } } if((key==72)&&(use>1)) { bli=use-1; } if((key==80)&&(use
=2) {use=guess%3;box("Computer has the first chance to play!");} else {use=3; mad=0;box("You have the first chance to play!");} delay(2000); do { for(int j=0;j<8;j++) {dang[j]=0;my[j]=0;} count++; mad++;bre=0; if((end!=1)&&(mad!=1)) { switch(use) { case 0:{ switch(count) { case 1: col[2][2]=comps;break; case 2: {if(col[1][1]==hums) {col[0][0]=comps;play[0]=1;} else if((col[2][0]==hums)||(col[2][1]==hums)) {col[0][2]=comps;play[1]=1;} else if((col[0][1]==hums)) {col[0][2]=comps;play[3]=1;} else if((col[1][0]==hums)) {col[2][0]=comps;play[4]=1;} else if((col[0][2]==hums)||(col[1][2]==hums)) {col[2][0]=comps;play[2]=1;} else if (col[0][0]==hums) {col[0][2]=comps;play[3]=1;} else dont=1; }break; case 3:{if(play[0]==1) dont=1; else if((play[1]==1)&&(col[1][2]==hums)) {col[0][0]=comps;} else if((play[2]==1)&&(col[2][1]==hums)) {col[0][0]=comps;} else if((play[3]==1)&&((col[2][1]==hums)||(col[1][2]==hums))) {col[2][0]=comps;} else if((play[4]==1)&&(col[2][1]==hums)) {col[0][2]=comps;} else dont=1; }break; case 4:dont=1;break; } }break; case 1:{switch(count) { case 1:col[0][1]=comps;break; case 2:{if(col[2][0]==hums) col[0][0]=comps; else if(col[1][0]==hums) col[0][0]=comps; else if(col[0][2]==hums) col[1][0]=comps; else if(col[1][2]==hums) col[0][2]=comps; else if(col[0][0]==hums) col[1][2]=comps; else if(col[2][2]==hums) col[0][2]=comps; else if(q<=1) col[2][2]=comps; else col[2][0]=comps; } break; case 3:dont=1; } }break; case 2:{switch(count) { case 1:col[1][1]=comps;break; case 2:dont=1; } }break; case 3:{dont=1; }break; } if(dont==1) { for(int i=0,l=2;i<3;i++,l--) { if(col[i][i]==hums) dang[0]++; else if(col[i][i]==comps) my[0]++; if(col[i][l]==hums) dang[1]++; else if(col[i][l]==comps) my[1]++; } for(j=0;j<3;j++) { for(int k=0;k<3;k++) { if(col[j][k]==hums) dang[j+2]++; else if(col[j][k]==comps) my[j+2]++; if(col[k][j]==hums) dang[j+5]++; else if(col[k][j]==comps) my[j+5]++; } } for(int j=0;j<8;j++) { if((my[j]==3)||(dang[j]==3)||(count==5)) end=1; if((dang[j]==2)&&(my[j]!=0)) dang[j]=0; if((my[j]==2)&&(dang[j]==0)) {my[j]=3;bre=1;} } if(bre==1) {for(j=0;j<8;j++) dang[j]=0; } if((dang[0]==2)||(my[0]==3)) { for(int i=0;i<3;i++) { if(col[i][i]==' ') col[i][i]=comps; }} else if((dang[1]==2)||(my[1]==3)) { for(int i=0,l=2;i<3;i++,l--) { if(col[i][l]==' ') col[i][l]=comps; }} else if((dang[2]==2)||(my[2]==3)||(dang[3]==2)||(my[3]==3)||(dang[4]==2)||(my[4 ]==3)) { for(j=0;j<3;j++) {if((dang[j+2]==2)||(my[j+2]==3)) for(int k=0;k<3;k++) {if(col[j][k]==' ') {col[j][k]=comps;bre=1;}} } } else if((dang[5]==2)||(my[5]==3)||(dang[6]==2)||(my[6]==3)||(dang[7]==2)||(my[7 ]==3)) { for(int j=0;j<3;j++) {if((dang[j+5]==2)||(my[j+5]==3)) for(int k=0;k<3;k++) {if(col[k][j]==' ') {col[k][j]=comps;bre=1;}} } } else if(col[1][1]==' ') col[1][1]=comps; else if((use==2)&&(col[2][2]==' ')) col[2][2]=comps; else if((use==2)&&(col[0][2]==' ')) col[0][2]=comps; else if((((col[0][0]==hums)&&(col[2][2]==hums))||((col[0][2]==hums)&&(col[2][0] ==hums)))&&(col[1][2]==' ')) col[1][2]=comps; else if((col[1][1]!=hums)&&((col[0][0]==hums)||(col[2][2]==hums))&&((col[0][1]= =hums)||(col[1][2]==hums))&&(col[0][2]==' ')) col[0][2]=comps; else if((col[1][1]!=hums)&&((col[0][0]==hums)||(col[2][2]==hums))&&((col[1][0]= =hums)||(col[2][1]==hums))&&(col[2][0]==' ')) col[2][0]=comps; else if((col[1][1]!=hums)&&((col[0][2]==hums)||(col[2][0]==hums))&&((col[2][1]= =hums)||(col[1][2]==hums))&&(col[2][2]==' ')) col[2][2]=comps; else if((col[1][1]!=hums)&&((col[0][2]==hums)||(col[2][0]==hums))&&((col[0][1]= =hums)||(col[1][0]==hums))&&(col[0][0]==' ')) col[0][0]=comps; else if((col[1][1]!=comps)&&(col[2][2]==' ')) col[2][2]=comps; else if((col[1][1]!=comps)&&(col[0][2]==' ')) col[0][2]=comps; else if(col[0][0]==' ') col[0][0]=comps; else if(col[2][2]==' ') col[2][2]=comps; else if(col[0][1]==' ') col[0][1]=comps; else if(col[1][2]==' ') col[1][2]=comps; else if(col[0][2]==' ') col[0][2]=comps; else if(col[2][0]==' ') col[2][0]=comps; else if(col[1][0]==' ') col[1][0]=comps; else if(col[2][1]==' ') col[2][1]=comps; } for(int i=0;i<8;i++) {if(my[i]==3) end=1; } } star: draw(col); box(" "); if(end!=1) { box("play"); madu=getche(); if((int(madu)<49)||(int(madu)>57)) {box("INVALID ENTRY!");for(long double jk=0;jk<99999999;jk++);goto star;} inpu=int(madu)-48; p=(inpu-1)/3; switch(inpu%3) {case 0:q=2;break; case 1:q=0;break; case 2:q=1;break; } if(col[p][q]!=' ') {box("Space is already occupied!");for(long double jk=0;jk<99999999;jk++);goto star;} col[p][q]=hums; } for(j=0;j<8;j++) {dang[j]=0;my[j]=0;} for(int i=0,l=2;i<3;i++,l--) { if(col[i][i]==hums) dang[0]++; else if(col[i][i]==comps) my[0]++; if(col[i][l]==hums) dang[1]++; else if(col[i][l]==comps) my[1]++; } for(j=0;j<3;j++) { for(int k=0;k<3;k++) { if(col[j][k]==hums) dang[j+2]++; else if(col[j][k]==comps) my[j+2]++; if(col[k][j]==hums) dang[j+5]++; else if(col[k][j]==comps) my[j+5]++; } } for(j=0;j<8;j++) {if((my[j]==3)||(dang[j]==3)) end=1; } }while((end!=1)); draw(col); for(int asd=0;asd<6;asd++) {many=many+1; if((my[0]==3)||(dang[0]==3)) {exii=0; if(many%2==1) for(int m=0,n=0;m<3;m++,n++) { setfillstyle(1,BLUE);floodfill(193+m*85,118+n*85,15); } else for(int m=0,n=0;m<3;m++,n++) { setfillstyle(1,BLACK);floodfill(193+m*85,118+n*85,15); }} else if((my[1]==3)||(dang[1]==3)) {exii=0; if(many%2==1) for(int m=0,n=2;m<3;m++,n--) { setfillstyle(1,BLUE);floodfill(193+m*85,118+n*85,15); } else for(int m=0,n=2;m<3;m++,n--) { setfillstyle(1,BLACK);floodfill(193+m*85,118+n*85,15); }} else for(j=2;j<8;j++) {if(((my[j]==3)||(dang[j]==3))&&(j<5)) {exii=0; if(many%2==1) for(int m=0,n=j-2;m<3;m++) { setfillstyle(1,BLUE);floodfill(193+m*85,118+n*85,15); } else for(int m=0,n=j-2;m<3;m++) { setfillstyle(1,BLACK);floodfill(193+m*85,118+n*85,15); }} else if((my[j]==3)||(dang[j]==3)) {exii=0; if(many%2==1) for(int m=0,n=j-5;m<3;m++) { setfillstyle(1,BLUE);floodfill(193+n*85,118+m*85,15); } else for(int m=0,n=j-5;m<3;m++) { setfillstyle(1,BLACK);floodfill(193+n*85,118+m*85,15); }} } for(long double jk=0;jk<9999999;jk++); if(exii==1)break; } for(int m=0;m<8;m++) { if(my[m]==3) result=1; } for(m=0;m<8;m++) { if(dang[m]==3) result=2; } switch(result) {case 1:box("You loose! Want to try again(y/n)");break; case 2:box("You win! Want to try again(y/n)");break; default:box("The game is draw! Want to try again(y/n)");break; } input=getche(); }while(input=='Y'||input=='y'); clearviewport(); for(long double mas=0;mas<=99999999;mas++); exit(0); }
Privacy Policy
|
Link to Us
|
Links