TIC TAC TOE Game
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | /******************************************************* * MYCPLUS Sample Code - https://www.mycplus.com * * * * This code is made available as a service to our * * visitors and is provided strictly for the * * purpose of illustration. * * * * Please direct all inquiries to saqib at mycplus.com * *******************************************************/ //Name: Usman Shahzada //E-Mail: [email protected] //Description: It`s Tic-Tac-Toe # include # include # include # include char player1[100],player2[100]; enum whoplays {human,machine}; enum cell {empty,full}; enum boolean{false,true}; int deep = 0; class position { private: cell mcell[9]; cell hcell[9]; whoplays player ; public: position(); boolean iswin(); boolean islegal(int); void makemove(int); void setplayer (whoplays); static void initdisplay(); void display(); int evaluate(int&); }; position::position() {for (int j=0;j<9;j++) mcell[j]=hcell[j]=empty; } boolean position::iswin() { cell *ptr; if (player==human) ptr=hcell; else ptr=mcell; if((ptr[0]&&ptr[1]&&ptr[2])||(ptr[3]&&ptr[4]&&ptr[5])||(ptr[6]&&ptr[7]&&ptr[8])||(ptr[0]&&ptr[3]&&ptr[6])||(ptr[1]&&ptr[4]&&ptr[7])||(ptr[2]&&ptr[5]&&ptr[8])||(ptr[0]&&ptr[4]&&ptr[8])||(ptr[2]&&ptr[4]&&ptr[6])) return (true); else return (false); } boolean position::islegal(int move) { if(move>=0 &&move<=8&&mcell[move]==empty&&hcell[move]==empty) return (true); else return (false); } void position::makemove(int move) { if(player==human) hcell[move]=full; else mcell[move]=full; } void position::setplayer(whoplays p) {player =p; } void position::initdisplay() {const unsigned char block= '\xB2'; void insert(unsigned char,int,int); int row,col; clrscr(); for(row=0;row<11;row++) {insert (block ,5,row); insert (block ,11,row); } for (col=0;col<17;col++) {insert (block,col,3); insert (block,col,7); } for (int j=0; j<9;j++) insert ((char)(j+'0'),(j%3)*6+4,(j/3)*4) ; } void position::display() { void insert (unsigned char,int,int); int row,col; for (int j=0;j<9;j++) {if (hcell[j]) insert('X',(j%3)*6+2,(j/3)*4+1); else if( mcell[j]) insert('0',(j%3)*6+2,(j/3)*4+1); else insert(' ',(j%3)*6+2,(j/3)*4+1); } gotoxy(1,23); } void insert(unsigned char ch,int col,int row) {gotoxy(col+50+1,row+5+1); putch(ch); } void main() {clrscr(); cout<<"Player1 enter your name:"; gets(player1); cout<<"Player2 enter your name:"; gets(player2); int move; int sc; int movecount=0; position current; int cursrow=0; position::initdisplay(); while(1) {current.setplayer(human); current.display(); gotoxy(1,++cursrow); cout<<player1<<" make your move"; cin>>move; if (!current.islegal(move)) { gotoxy(1,++cursrow); cout<<"illegal move"; continue; } current.makemove(move); current.display(); if ( current.iswin()) { gotoxy(1,++cursrow); cout<<player1<<" you win"; getch(); exit(0); } if (++movecount==9) { gotoxy(1,++cursrow); cout<<"its a draw"; getch(); exit(0); } current.setplayer(machine); current.display(); gotoxy(1,++cursrow); cout<<player2<<" make your move"; cin>>move; if (!current.islegal(move)) { gotoxy(1,++cursrow); cout<<"illegal move"; continue; } current.makemove(move); current.display(); if ( current.iswin()) { gotoxy(1,++cursrow); cout<<player2<<" wins win"; getch(); exit(0); } if (++movecount==9) { gotoxy(1,++cursrow); cout<<"its a draw"; getch(); exit(0); } }} |