Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes

C++ Programming Code Examples

C++ > Games Code Examples

Graphic Tictactoe - The first ever tictactoe playing artificial intelligence.

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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
/* 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<iostream.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> #include<string.h> #include<dos.h> 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<mult)) { bli=use+1; } } if(bli<0) bli=0-bli; return(bli); } void box(char mes[50]) { putimage(5,5,message,0); settextstyle(0,0,1); outtextxy(20,30,mes); } void draw(char mn[3][3]) { char as[3][3][3]; char num[9][3]; for(int i=0;i<10;i++) { strcpy(num[i]," "); num[i][0]=char(49+i); } for(i=0;i<3;i++) for(int j=0;j<3;j++) strcpy(as[i][j]," "); for(i=0;i<3;i++) { for(j=0;j<3;j++) {as[i][j][0]=mn[i][j];} } clearviewport(); setcolor(15); rectangle(0,0,639,479); setfillstyle(1,8); settextstyle(0,0,1); for(i=0;i<3;i++) { rectangle(192,117+i*85,267,192+i*85); outtextxy(260,185+i*85,num[0+i*3]); rectangle(277,117+i*85,352,192+i*85); outtextxy(345,185+i*85,num[1+i*3]); rectangle(362,117+i*85,437,192+i*85); outtextxy(430,185+i*85,num[2+i*3]); } floodfill(500,430,15); setcolor(15); settextstyle(1,0,4); for(i=0;i<3;i++) { outtextxy(221,135+i*85,as[i][0]); outtextxy(306,135+i*85,as[i][1]); outtextxy(391,135+i*85,as[i][2]); } } void main() { clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm,""); message=malloc(imagesize(5,5,634,55)); setcolor(15); rectangle(5,5,634,55); setfillstyle(1,RED); floodfill(30,30,15); outtextxy(10,10,"Message:-"); getimage(5,5,634,55,message); char col[3][3],input,madu,comps,hums,mess[70]={"computer has selected the symbol . Press any key to continue.."}; int exii,dang[8],my[8],hard,many,result,guess=7,bre,mad=2,count=0,dont=0,play[ 8],p,q,end=0,note,inpu,first,use; do {guess=7;mad=2;count=0;dont=0;end=0;result=0; for(int i=0;i<8;i++) play[i]=0; many=0;exii=1; clearviewport(); setcolor(15); rectangle(0,0,639,479); rectangle(20,320,620,460); rectangle(220,150,390,180); rectangle(240,155,370,175); setfillstyle(1,8); floodfill(100,100,15); setcolor(15); settextstyle(4,0,4); outtextxy(200,50,"TIC TAC TOE"); settextstyle(3,0,1); outtextxy(40,290,"How to play :-"); outtextxy(35,330,"In this Game, you may select your symbol. You must try attain"); outtextxy(26,350,"three of your symbols in a line. if you suceed you are the winner."); outtextxy(35,370," But at the same time you should prevent the computer from"); outtextxy(35,390,"getting three of its symbols in a line. To play enter the number"); outtextxy(32,410,"associated with the place where you want to play. Press any key"); outtextxy(35,430,"to start"); settextstyle(2,0,6); outtextxy(258,155,"Start Game"); select(1); hard=2; for(int j=0;j<8;j++) {dang[j]=0;my[j]=0;play[j]=0;} for(j=0;j<3;j++) { for(int k=0;k<3;k++) col[j][k]=' '; } draw(col); box("Please type in your symbol"); hums=getche(); if((hums!='X')&&(hums!='x') ) comps='X'; else comps='0'; mess[33]=comps; box(mess); getch(); randomize(); first=(int(rand()%100)); if(hard==2)guess=(int(rand()%100)); else guess=5; if((first%4)>=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); }

It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.

getimage() function copy a specific portion into memory. This specific image would be any bit image like rectangle, circle or anything else. getimage() copies an image from the screen to memory. Left, top, right, and bottom define the screen area to which the rectangle is copied. Bitmap points to the area in memory where the bit image is stored. The first two words of this area are used for the width and height of the rectangle; the remainder holds the image itself.

The header file graphics.h contains setfillstyle() function which sets the current fill pattern and fill color. Current fill pattern and fill color is used to fill the area. setfillstyle sets the current fill pattern and fill color. To set a user-defined fill pattern, do not give a pattern of 12 (USER_FILL) to setfillstyle; instead, call setfillpattern.

Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

This function waits for any character input from keyboard. And, it will also echo the input character on to the output screen. The getch() function is very useful if you want to read a character input from the keyboard. Like getch(), getche() is also character input functions. It is unformatted input function meaning it does not allow user to read input in their format. Difference between getch() and getche() is that getche() echoes pressed character. getche() also returns character pressed like getch(). It is also defined in header file conio.h.

The header file graphics.h contains imagesize() function which returns the number of bytes required to store a bit-image. This function is used when we are using getimage. imagesize() function returns the required memory area to store an image in bytes. imagesize() function returns the number of bytes needed to store the top-left corner of the screen at left, top and the bottom-right corner at right, bottom. This function is usually used in conjunction with the getimage() function. The imagesize() function only works in graphics mode.

Allocate memory block. Allocates a block of size bytes of memory, returning a pointer to the beginning of the block. The content of the newly allocated block of memory is not initialized, remaining with indeterminate values. If size is zero, the return value depends on the particular library implementation (it may or may not be a null pointer), but the returned pointer shall not be dereferenced. Malloc function in C++ is used to allocate a specified size of the block of memory dynamically uninitialized. It allocates the memory to the variable on the heap and returns the void pointer pointing to the beginning address of the memory block. The values in the memory block allocated remain uninitialized and indeterminate. In case the size specified in the function is zero then pointer returned must not be dereferenced as it can be a null pointer, and in this case, behavior depends on particular library implementation. When a memory block is allocated dynamically memory is allocated on the heap but the pointer is

outtextxy displays a text string in the viewport at the given position (x, y), using the current justification settings and the current font, direction, and size. To maintain code compatibility when using several fonts, use textwidth and textheight to determine the dimensions of the string. If a string is printed with the default font using outtext or outtextxy, any part of the string that extends outside the current viewport is truncated. outtextxy is for use in graphics mode; it will not work in text mode.

To create a program in Graphics Mode, the first step would be to include the header file graphics.h. This file is required for Graphics programming. After this, the graphics have to be initialized. C Language supports 16 Bit's MS-DOS environment. Initializing the Graphics mode is to call various functions, one such is called initgraph. initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver), and putting the system into graphics mode. To start the graphics system, first call the initgraph function. initgraph loads the graphics driver and puts the system into graphics mode. You can tell initgraph to use a particular graphics driver and mode, or to autodetect the attached video adapter at run time and pick the corresponding driver. If you tell initgraph to autodetect, it calls detectgraph to select a graphics driver and mode. initgraph also resets all graphics settings to their defaults (current position, palette, color, viewport, and so on)

delay() function is used to hold the program's execution for given number of milliseconds, it is declared in dos.h header file. There can be many instances when we need to create a delay in our programs. C++ provides us with an easy way to do so. We can use a delay() function for this purpose in our code. We can run the code after a specific time in C++ using delay() function.

putimage puts the bit image previously saved with getimage back onto the screen, with the upper left corner of the image placed at (left,top). bitmap points to the area in memory where the source image is stored. The op parameter to putimage specifies a combination operator that controls how the color for each destination pixel onscreen is computed, based on the pixel already onscreen and the corresponding source pixel in memory.

Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block. • The expression can be integer expression or a character expression. • Value-1, 2, n are case labels which are used to identify each case individually. Remember that case labels should not be same as it may create a problem while executing a program. Suppose we have two cases with the same label as '1'. Then while executing the program, the case that appears first will be executed even though you want the program to execute a second case. This creates problems in the program and

In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.

Settextstyle function is used to change the way in which text appears, using it we can modify the size of text, change direction of text and change the font of text. settextstyle sets the text font, the direction in which text is displayed, and the size of the characters. A call to settextstyle affects all text output by outtext and outtextxy.

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

floodfill function is used to fill an enclosed area. Current fill pattern and fill color is used to fill the area.(x, y) is any point on the screen if (x,y) lies inside the area then inside will be filled otherwise outside will be filled, border specifies the color of boundary of area. To change fill pattern and fill color use setfillstyle.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list; A two dimensional array is like a table; The C++ language places no limits on the number of dimensions in an array, though specific implementations may. Some texts refer to one-dimensional arrays as vectors, two-dimensional arrays as matrices, and use the general term arrays when the number of dimensions is unspecified or unimportant. (2D) array in C++ programming is also known as matrix. A matrix can be represented as a table of rows and columns. In C/C++, we can define multi dimensional arrays in simple words as array of arrays. Data in multi dimensional arrays are stored in tabular form (in row major order).

setcolor() function is used to set the foreground color in graphics mode. After resetting the foreground color you will get the text or any other shape which you want to draw in that color. setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor. The current drawing color is the value to which pixels are set when lines, and so on are drawn. The drawing colors shown below are available for the CGA and EGA, respectively.

clearviewport() function clears the current viewport. setviewport will create a new viewport by accepting left, top, right and bottom coordinates. clearviewport() function will erase the drawing done on the view port only and not the whole screen. Cleardevice is the function used to clear the whole screen with the background color.

In C++, goto is a jump statement and sometimes also referred as unconditional jump statement. It can be used to jump from goto to a labeled statement within the same function. The target label must be within the same file and context. Please note that the use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making hard to understand and modify the program.

Copy string. Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point). To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source. strcpy() is a standard library function in C/C++ and is used to copy one string to another. In C it is present in string.h header file and in C++ it is present in cstring header file. It copies the whole string to the destination string. It replaces the whole string instead of appending it. It won't change the source string.

In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.

Generate random number. Returns a pseudo-random integral number in the range between 0 and RAND_MAX. This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using function srand. RAND_MAX is a constant defined in <cstdlib>. The rand() function in C++ is used to generate random numbers; it will generate the same number every time we run the program. In order to seed the rand() function, srand(unsigned int seed) is used. The srand() function sets the initial point for generating the pseudo-random numbers. The rand() function generates numbers randomly.

rectangle() is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle. left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner.

The exit function terminates the program normally. Automatic objects are not destroyed, but static objects are. Then, all functions registered with atexit are called in the opposite order of registration. The code is returned to the operating system. An exit code of 0 or EXIT_SUCCESS means successful completion. If code is EXIT_FAILURE, an indication of program failure is returned to the operating system. Other values of code are implementation-defined. Calls all functions registered with the atexit() function, and destroys C++ objects with static storage duration, all in last-in-first-out (LIFO) order. C++ objects with static storage duration are destroyed in the reverse order of the completion of their constructor. (Automatic objects are not destroyed as a result of calling exit().)

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

The kbhit is basically the Keyboard Hit. This function is present at conio.h header file. So for using this, we have to include this header file into our code. The functionality of kbhit() is that, when a key is pressed it returns nonzero value, otherwise returns zero. kbhit() is used to determine if a key has been pressed or not. If a key has been pressed then it returns a non zero value otherwise returns zero.

All the "variables" must be declared before to use or initial statements of the block or main or function or global. Variables should specify with data type. And it binds a "data type" and