#include #include #include #include #include #include #define isH ==1 #define isC ==-1 #define isO ==0 //===PROTOTYPES======================================================================== void enter_graphics_mode(int num); //===CLASS BOARD======================================================================== class Board { public: int state[10]; int player; int splayer; Board () { for (int n=0;n<10;n++) state[n] = 0; player = 1; splayer = 1; } ~Board () {}; void pgame(int state[],int &player); void showboardstate(int state[]); int AImove(); int fullarray(); int evaluate(int cell,int player, int tally,int &array); int didwin(int p); void drawboard(); void intro(); void drawx(int cell); void drawo(int cell); }; //===MAIN======================================================================= int main() { clrscr(); enter_graphics_mode(1); Board gb; gb.intro(); int key; while(1) { gb.drawboard(); gb.pgame(gb.state,gb.player); getch(); cleardevice(); setcolor(GREEN); settextstyle(4,0,8); outtextxy(30,100,"PLAY AGAIN?"); setcolor(BROWN); settextstyle(4,0,6); outtextxy(200,200,"(Y) / (N)"); key = getch(); if (key == 'N' || key =='n') break; //reset game for (int n=0;n<10;n++) gb. state[n] = 0; gb.player = 1; cleardevice(); } return (0); } //===FUNCTIONS======================================================================= void Board:: pgame(int state[],int &player) { int knum, m; while (state[0] < 9) { if (player == 1) { setcolor(WHITE); settextstyle(0,0,1); outtextxy(1,15," "); outtextxy(1,15,"Choose # 1 - 9"); setcolor(BLACK); outtextxy(1,30,"Computer Turn"); setcolor(WHITE); outtextxy(1,30,"Player Turn"); while(1) { while (!kbhit()); //pause until key is hit setcolor(BLACK); outtextxy(1,50,"BAD INPUT.TRY AGAIN "); knum = getch() - 48; if (state[knum]isO) break; setcolor(WHITE); outtextxy(1,50,"BAD INPUT.TRY AGAIN"); } state[knum] = 1; drawx(knum); } else if (player == -1) { setcolor(WHITE); settextstyle(0,0,1); setcolor(BLACK); outtextxy(1,30,"Player Turn"); setcolor(WHITE); outtextxy(1,30,"Computer Turn"); m = AImove(); drawo(m); state[m] = -1; } if (didwin(player)) { setcolor(YELLOW); settextstyle(4,0,7); if (player == 1) outtextxy(165,200,"YOU WIN"); else { setcolor(MAGENTA); outtextxy(165,200,"LOOOSER"); } return; } showboardstate(state); player *= -1; state[0]++; } if (state[0] == 9) { setcolor(MAGENTA); settextstyle(8,0,10); outtextxy(100,150,"DRAW..."); } } //end function PLAY GAME int Board:: AImove() { int tm; // Make winning move if possible if (state[1]isC && state[2]isC&&state[3]isO) return 3; if (state[1]isC && state[3]isC&&state[2]isO) return 2; if (state[2]isC && state[3]isC&&state[1]isO) return 1; if (state[4]isC && state[5]isC&&state[6]isO) return 6; if (state[4]isC && state[6]isC&&state[5]isO) return 5; if (state[5]isC && state[6]isC&&state[4]isO) return 4; if (state[7]isC && state[8]isC&&state[9]isO) return 9; if (state[7]isC && state[9]isC&&state[8]isO) return 8; if (state[8]isC && state[9]isC&&state[7]isO) return 7; if (state[1]isC && state[4]isC&&state[7]isO) return 7; if (state[1]isC && state[7]isC&&state[4]isO) return 4; if (state[4]isC && state[7]isC&&state[1]isO) return 1; if (state[2]isC && state[5]isC&&state[8]isO) return 8; if (state[2]isC && state[8]isC&&state[5]isO) return 5; if (state[5]isC && state[8]isC&&state[2]isO) return 2; if (state[3]isC && state[6]isC&&state[9]isO) return 9; if (state[3]isC && state[9]isC&&state[6]isO) return 6; if (state[6]isC && state[9]isC&&state[3]isO) return 3; if (state[5]isC && state[3]isC&&state[7]isO) return 7; if (state[3]isC && state[7]isC&&state[5]isO) return 5; if (state[7]isC && state[5]isC&&state[3]isO) return 3; if (state[1]isC && state[5]isC&&state[9]isO) return 9; if (state[1]isC && state[9]isC&&state[5]isO) return 5; if (state[9]isC && state[5]isC&&state[1]isO) return 1; //------------------------------------------------------// // Prevent you of winning if (state[1]isH && state[2]isH&&state[3]isO) return 3; if (state[1]isH && state[3]isH&&state[2]isO) return 2; if (state[2]isH && state[3]isH&&state[1]isO) return 1; if (state[4]isH && state[5]isH&&state[6]isO) return 6; if (state[4]isH && state[6]isH&&state[5]isO) return 5; if (state[5]isH && state[6]isH&&state[4]isO) return 4; if (state[7]isH && state[8]isH&&state[9]isO) return 9; if (state[7]isH && state[9]isH&&state[8]isO) return 8; if (state[8]isH && state[9]isH&&state[7]isO) return 7; if (state[1]isH && state[4]isH&&state[7]isO) return 7; if (state[1]isH && state[7]isH&&state[4]isO) return 4; if (state[4]isH && state[7]isH&&state[1]isO) return 1; if (state[2]isH && state[5]isH&&state[8]isO) return 8; if (state[2]isH && state[8]isH&&state[5]isO) return 5; if (state[5]isH && state[8]isH&&state[2]isO) return 2; if (state[3]isH && state[6]isH&&state[9]isO) return 9; if (state[3]isH && state[9]isH&&state[6]isO) return 6; if (state[6]isH && state[9]isH&&state[3]isO) return 3; if (state[5]isH && state[3]isH&&state[7]isO) return 7; if (state[3]isH && state[7]isH&&state[5]isO) return 5; if (state[7]isH && state[5]isH&&state[3]isO) return 3; if (state[1]isH && state[5]isH&&state[9]isO) return 9; if (state[1]isH && state[9]isH&&state[5]isO) return 5; if (state[9]isH && state[5]isH&&state[1]isO) return 1; //------------BLOCK YOU--------------------------------// // rated by preference. Near top = more important if (state[3]isH && state[5]isO&&state[7]isO) return 5; if (state[5]isH && state[7]isO&&state[3]isO) return 3; if (state[7]isH && state[3]isO&&state[5]isO) return 5; if (state[1]isH && state[5]isO&&state[9]isO) return 5; if (state[5]isH && state[1]isO&&state[9]isO) return 9; if (state[9]isH && state[5]isO&&state[1]isO) return 5; if (state[1]isH && state[2]isO&&state[3]isO) return 3; if (state[2]isH && state[1]isO&&state[3]isO) return 3; if (state[3]isH && state[1]isO&&state[2]isO) return 1; if (state[4]isH && state[5]isO&&state[6]isO) return 5; if (state[5]isH && state[4]isO&&state[6]isO) return 4; if (state[6]isH && state[5]isO&&state[4]isO) return 5; if (state[7]isH && state[8]isO&&state[9]isO) return 9; if (state[8]isH && state[9]isO&&state[7]isO) return 9; if (state[9]isH && state[8]isO&&state[7]isO) return 7; if (state[2]isH && state[5]isO&&state[8]isO) return 5; if (state[5]isH && state[8]isO&&state[2]isO) return 8; if (state[8]isH && state[5]isO&&state[2]isO) return 5; if (state[1]isH && state[4]isO&&state[7]isO) return 7; if (state[4]isH && state[7]isO&&state[1]isO) return 7; if (state[7]isH && state[4]isO&&state[1]isO) return 1; if (state[3]isH && state[6]isO&&state[9]isO) return 9; if (state[6]isH && state[9]isO&&state[3]isO) return 9; if (state[9]isH && state[3]isO&&state[6]isO) return 3; //-----------------------------------------------------// // If still here make random move while (1) { randomize(); tm = random(9)+1; if (!state[tm]) break; } return tm; /* int a; int whosturn = 1; int maxcell = 1,maxvalue = -1000,newvalue = -1000; for (int cell = 1;cell<10;cell++) { tm = 0; a = state[0]; if (state[cell]) continue; newvalue = evaluate(cell,whosturn,tm,a); gotoxy(1,3+cell); cout << cell << ") " << newvalue; if (newvalue > maxvalue) { maxcell = cell; maxvalue = newvalue; } } return maxcell; */ } int Board:: evaluate(int cell, int player, int tally, int &array) { state[cell] = player; array++; if (didwin(player)) { state[cell] = 0; tally++; return tally; } if (array==9) {state[cell] = 0; return tally;} //draw for (int n=1;n<10;n++) if (!state[n]) evaluate(n,player * -1,tally,array); return tally; } int Board:: fullarray() { for(int n=1;n<10;n++) if(!state[n]) return 0; return 1; } int Board:: didwin(int p) { int value; value = 3*p; if (state[1]+state[2]+state[3] == value) return 1; if (state[4]+state[5]+state[6] == value) return 1; if (state[7]+state[8]+state[9] == value) return 1; if (state[1]+state[4]+state[7] == value) return 1; if (state[2]+state[5]+state[8] == value) return 1; if (state[3]+state[6]+state[9] == value) return 1; if (state[1]+state[5]+state[9] == value) return 1; if (state[7]+state[5]+state[3] == value) return 1; return 0; } void Board:: showboardstate(int state[]) { gotoxy(1,15); cout << "Moves Made: " << state[0]; gotoxy(1,17); for (int n=1;n<10;n++) cout << n << endl; gotoxy(3,17); for ( n=1;n<10;n++) {cout << state[n];gotoxy(3,17+n);} } void Board:: intro() { setbkcolor(BLACK); settextstyle(3,0,5); setcolor(RED); outtextxy(5,5,"NOTICE: "); setcolor(WHITE); settextstyle(0,0,1); outtextxy(5,100,"You are facing T.I.C.T.A.C "); outtextxy(5,125,"(The Intellectually Challenged Totally Automated Computer)"); outtextxy(5,150,"It is very possible that you will annihlate this opponent."); setcolor(DARKGRAY); outtextxy(5,460,"Complaints may be sent to: MStueben@lan.tjhsst.edu."); getch(); cleardevice(); } void Board:: drawboard() { setcolor(LIGHTBLUE); int spc = getmaxy()/3; //vertical lines for (int n = 240; n < 500; n += spc) line(n,30,n,450); //horizontal lines for ( n = 160; n < 450; n += spc) line(110,n,530,n); setcolor(WHITE); //show numbers settextstyle(0,0,1); outtextxy(230,150,"7"); outtextxy(320,150,"8"); outtextxy(402,150,"9"); outtextxy(230,240,"4"); outtextxy(320,240,"5"); outtextxy(402,240,"6"); outtextxy(230,322,"1"); outtextxy(320,322,"2"); outtextxy(402,322,"3"); } void Board:: drawx(int cell) { setcolor(RED); settextstyle(5,0,10); int tx, ty, lx; if (cell%3 == 0) lx = 3; else lx = cell%3; tx = 130 + (lx-1) * 150; if (1<=cell && cell <= 3) ty = 280; if (4<=cell && cell <= 6) ty = 135; if (7<=cell && cell <= 9) ty = 0; outtextxy(tx,ty,"X"); setcolor(BROWN); outtextxy(tx+5,ty,"X"); } void Board:: drawo(int cell) { setcolor(BLUE); settextstyle(6,0,10); int tx, ty, lx; if (cell%3 == 0) lx = 3; else lx = cell%3; tx = 140 + (lx-1) * 150; if (1<=cell && cell <= 3) ty = 280; if (4<=cell && cell <= 6) ty = 135; if (7<=cell && cell <= 9) ty = 0; outtextxy(tx,ty,"O"); setcolor(DARKGRAY); outtextxy(tx+5,ty,"O"); } void enter_graphics_mode(int num) { //--Do NOT remove the following 2 lines.--M. Stueben (Oct, 1996). int numx; float fnum; fnum = (double) sqrt(37); for (numx = 1; numx <= fnum; ++numx); //--The lines above help aviod a Turbo C++ bug. int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, "c:\\Tc\\turbocp\\bgi\\"); errorcode = graphresult(); if (errorcode != grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); fflush(stdin); getch(); exit(1); } directvideo = 0; //----------------------------------------------------------------------+ //---NOTE: You may not wish to make this call everytime the pgm is run. | // if (num) show_graphics_data(gmode, gdriver); //----------------------------------------------------------------------+ setcolor (WHITE); setbkcolor(BLUE ); setlinestyle(SOLID_LINE, 0, NORM_WIDTH); cleardevice(); } // end enter_graphics_mode function