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

The Mind Reader 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 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
/* The Mind Reader Game */ #include #include #include #include void First_Card(void);void Second_Card(void); void Third_Card(void);void Fourth_Card(void); void Fifth_Card(void);void Sixth_Card(void); void Analyze(void); Check(void); void main(void) { int ans=0,in=0; clrscr(); _setcursortype(_NOCURSOR); First_Card(); in=Check(); if (in==0) ans+=1; Second_Card(); in=Check(); if (in==0) ans+=2; Third_Card(); in=Check(); if (in==0) ans+=4; Fourth_Card(); in=Check(); if (in==0) ans+=8; Fifth_Card(); in=Check(); if (in==0) ans+=16; Sixth_Card(); in=Check(); if (in==0) ans+=32; textcolor(WHITE);textbackground(BLACK); clrscr(); Analyze(); clrscr(); if(ans>60) {gotoxy(20,12);printf("You Entered Incorrect Information.");} else {gotoxy(28,13);printf("Your No Is : "%d"",ans);} gotoxy(20,14);printf("Thanks For Using This Program... "); } void First_Card(void) { gotoxy(25,6); printf("ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»"); gotoxy(25,7); printf("º C O M P U T E R G A M E º"); gotoxy(25,8); printf("ÌÍÍÍÍËÍÍÍÍËÍÍÍÍËÍÍÍÍËÍÍÍÍËÍÍÍ͹"); gotoxy(25,9); printf("º 1 º 3 º 5 º 7 º 9 º 11 º"); gotoxy(25,10); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,11); printf("º 13 º 15 º 17 º 19 º 21 º 23 º"); gotoxy(25,12); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,13); printf("º 25 º 27 º 29 º 31 º 33 º 35 º"); gotoxy(25,14); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,15); printf("º 37 º 39 º 41 º 43 º 45 º 47 º"); gotoxy(25,16); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,17); printf("º 49 º 51 º 53 º 55 º 57 º 59 º"); gotoxy(25,18); printf("ÈÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍͼ"); } void Second_Card(void) { gotoxy(25,6); printf("ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»"); gotoxy(25,7); printf("º C O M P U T E R G A M E º"); gotoxy(25,8); printf("ÌÍÍÍÍËÍÍÍÍËÍÍÍÍËÍÍÍÍËÍÍÍÍËÍÍÍ͹"); gotoxy(25,9); printf("º 2 º 3 º 6 º 7 º 10 º 11 º"); gotoxy(25,10); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,11); printf("º 14 º 15 º 18 º 19 º 22 º 23 º"); gotoxy(25,12); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,13); printf("º 25 º 27 º 30 º 31 º 34 º 35 º"); gotoxy(25,14); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,15); printf("º 38 º 39 º 42 º 43 º 46 º 47 º"); gotoxy(25,16); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,17); printf("º 50 º 51 º 54 º 55 º 58 º 59 º"); gotoxy(25,18); printf("ÈÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍͼ"); } void Third_Card(void) { gotoxy(25,6); printf("ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»"); gotoxy(25,7); printf("º C O M P U T E R G A M E º"); gotoxy(25,8); printf("ÌÍÍÍÍËÍÍÍÍËÍÍÍÍËÍÍÍÍËÍÍÍÍËÍÍÍ͹"); gotoxy(25,9); printf("º 4 º 5 º 6 º 7 º 12 º 13 º"); gotoxy(25,10); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,11); printf("º 14 º 15 º 20 º 21 º 22 º 23 º"); gotoxy(25,12); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,13); printf("º 28 º 29 º 30 º 31 º 36 º 37 º"); gotoxy(25,14); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,15); printf("º 38 º 39 º 44 º 45 º 46 º 47 º"); gotoxy(25,16); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,17); printf("º 52 º 53 º 54 º 55 º 60 º 60 º"); gotoxy(25,18); printf("ÈÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍͼ"); } void Fourth_Card(void) { gotoxy(25,6); printf("ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»"); gotoxy(25,7); printf("º C O M P U T E R G A M E º"); gotoxy(25,8); printf("ÌÍÍÍÍËÍÍÍÍËÍÍÍÍËÍÍÍÍËÍÍÍÍËÍÍÍ͹"); gotoxy(25,9); printf("º 8 º 9 º 10 º 11 º 12 º 13 º"); gotoxy(25,10); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,11); printf("º 14 º 15 º 24 º 25 º 26 º 27 º"); gotoxy(25,12); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,13); printf("º 28 º 29 º 30 º 31 º 40 º 41 º"); gotoxy(25,14); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,15); printf("º 42 º 43 º 44 º 45 º 46 º 47 º"); gotoxy(25,16); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,17); printf("º 56 º 57 º 58 º 59 º 60 º 60 º"); gotoxy(25,18); printf("ÈÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍͼ"); } void Fifth_Card(void) { gotoxy(25,6); printf("ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»"); gotoxy(25,7); printf("º C O M P U T E R G A M E º"); gotoxy(25,8); printf("ÌÍÍÍÍËÍÍÍÍËÍÍÍÍËÍÍÍÍËÍÍÍÍËÍÍÍ͹"); gotoxy(25,9); printf("º 16 º 17 º 18 º 19 º 20 º 21 º"); gotoxy(25,10); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,11); printf("º 22 º 23 º 24 º 25 º 26 º 27 º"); gotoxy(25,12); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,13); printf("º 28 º 29 º 30 º 31 º 48 º 49 º"); gotoxy(25,14); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,15); printf("º 50 º 51 º 52 º 53 º 54 º 55 º"); gotoxy(25,16); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,17); printf("º 56 º 57 º 58 º 59 º 60 º 60 º"); gotoxy(25,18); printf("ÈÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍͼ"); } void Sixth_Card(void) { gotoxy(25,6); printf("ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» 52 º 53 º 54 º 55 º"); gotoxy(25,16); printf("ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍ͹"); gotoxy(25,17); printf("º 56 º 57 º 58 º 59 º 60 º 60 º"); gotoxy(25,18); printf("ÈÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍͼ"); } Check(void) { int loop,key,index=0; char *menu[]={" Yes "," No "}; textcolor(WHITE); textbackground(CYAN); while(1) { for (loop=0;loop<2;loop++) { if (loop==index) textbackground(YELLOW); else textbackground(CYAN); gotoxy(33+loop*10,20);cprintf("%s",menu[loop]); } key=getch(); switch(key) { case 75: index--;if (index<0) index=1;break; case 77: index++;if (index>1) index=0;break; case 13: return index; } } } void Analyze(void) { gotoxy(30,10); printf("Analyzing Result..."); gotoxy(25,11); printf("ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»"); gotoxy(25,12); printf("º °°°°°°°°°°°°°°°°°°°°°°°°°°° º"); gotoxy(25,13); printf("ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ"); for(int a=27;a<=53;a++) { delay(100); gotoxy(a,12);printf("%c",219); } }

Positions cursor in text window. The gotoxy() function places the cursor at the desired location on the screen. This means it is possible to change the cursor location on the screen using the gotoxy() function. It is basically used to print text wherever the cursor is moved. If the coordinates are in any way invalid the call to gotoxy is ignored. Neither argument to gotoxy can be zero.

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.

#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.

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.

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.

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.

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.

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.

As the name already suggests, these operators help in assigning values to variables. These operators help us in allocating a particular value to the operands. The main simple assignment operator is '='. We have to be sure that both the left and right sides of the operator must have the same data type. We have different levels of operators. Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language:

Use the textcolor function to define what color you want to use for text. You can use this function to vary the text colors of your output. Colors must be written in all caps, or expressed as a numeral. Now, if you want your text to blink then while calling the textcolor() function pass the color and also say BLINK. This will like this: textcolor(BLUE+BLINK).

Function textbackground is used to change current background color in text mode. To use the textbackground() function all you need to do is before printing any text call this function with a parameter defining the color in capital letters. That will be enough to change the background color of the text.

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.

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.

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




See in this case the output is Woof, which is what we expect. What happens in this case? Since we marked the function animalSound() as virtual, the call to the function is resolved