// Charlie Tonneslan // a3.cpp // Period 1 // AP Java #include #include // string function #include #include // time function #include // _getch function using namespace std; // *************************** fuctions declaration ****************************** void displayInstructions (string name); // Displays the instruction of the game void pauseFunction (); // Pauses the game until the player presses any key int main() { // ********************* variable declaration statements ********************* int num[50]; // computer numbers string colors[7]; // computer strings/colors int userImput[50]; // user's inputed numbers string userImputedStrings[50]; // user's inputed strings int j = 0; // loop variable int i = 0; // loop variable int k = 0; // loop variable int f = 0; // variable used for win detection string name; // user's name string color; // color // ******************************** Game Body ******************************** cout << " ooooooooo" << endl; cout << " ooooooooooooooooooooooo" << endl; cout << " ooooooooooooooooooooooooooooooo" << endl; cout << " ooooooooooooooooooooooooooooooooooooo" << endl; cout << " oooooooooooooooooooooooooooooooooooooooooo" << endl; cout << " ooooooooooooooooooooooooooooooooooooooooooooo" << endl; cout << " oooooooooooo* *oooooooooooooo* *ooooooooooooo" << endl; cout << " oooooooooooo oooooooooooo ooooooooooooo" << endl; cout << " oooooooooooooo oooooooooooooooo ooooooooooooooo" << endl; cout << "ooooooooooooooooooooooooooooooooooooooooooooooooooo" << endl; cout << " ooooo ooooooooooooooooooooooooooooooo ooooo" << endl; cout << " ooooooo oooooooooooooooooooooooooooooooooo ooooooo" << endl; cout << " *ooooo ooooooooooooooooooooooooooooooooo ooooo*" << endl; cout << " *oooooo *ooooooooooooooooooooooooooooo* oooooo*" << endl; cout << " *oooooo *ooooooooooooooooooooooooooo* oooooo*" << endl; cout << " *ooooooo *ooooooooooooooooooooooo* ooooooo*" << endl; cout << " *oooooooo *ooooooooooooooooo* oooooooo*" << endl; cout << " *ooooooooo *ooooooooooo* ooooooooo*" << endl; cout << " *ooooooooo ooooooooo*" << endl; cout << " *ooooooooooooooooooooo*" << endl; cout << " ooooooooooooo " << endl; cout << " " << endl; cout << " " << endl; cout << " " << endl; cout << " " << endl; cout << " " << endl; cout << " " << endl; cout << " " << endl; cout << " Welcome to Charlie's Memory Game !" << endl; cout << " What is your name ? " << endl; cin >> name; cout << " "; cout << " "; displayInstructions (name); cout << " " << endl; cout << " " << endl; cout << " " << endl; pauseFunction (); system("cls"); srand(time(NULL)); for (i = 0; i < 50; i++) { num[i] = rand() % 100 + 1; cout << endl; cout << num[i] << endl; cout << endl; int currentNum = rand() % 7 + 1; if (currentNum == 1) { color = "Red"; cout << endl; cout << color << endl; cout << endl; colors[i] = color; } else if (currentNum == 2) { color = "Orange"; cout << endl; cout << color << endl; cout << endl; colors[i] = color; } else if (currentNum == 3) { color = "Yellow"; cout << endl; cout << color << endl; cout << endl; colors[i] = color; } else if (currentNum == 4) { color = "Green"; cout << endl; cout << color << endl; cout << endl; colors[i] = color; } else if (currentNum == 5) { color = "Blue"; cout << endl; cout << color << endl; cout << endl; colors[i] = color; } else if (currentNum == 6) { color = "Violet"; cout << endl; cout << color << endl; cout << endl; colors[i] = color; } else if (currentNum == 7) { color = "Purple"; cout << endl; cout << color << endl; cout << endl; colors[i] = color; } pauseFunction (); system("cls"); cout << " Enter the numbers and colors you have memorized in the correct order " << endl; for (j = 0; j <= i; j++) { cin >> userImput[i]; cin >> userImputedStrings[i]; } system ("cls"); // clearing the screen f = 0; // reset f each time the user gets to an additional number and string for (k = 0; k <= i; k++) { if (userImput[k] == num[k] && userImputedStrings[k] == colors[k]) { f++; } } if (f == k) { cout << endl << " You have " << i + 1 << " points" << endl << endl; } else { cout << endl << "Game over, you have " << i << " points" << endl << endl; cout << "Thank you for playing !" << endl; break; } }// end of for loop system ("pause"); return 0; }// end of main // ********************************* Functions ************************************ void displayInstructions (string name) { cout << " Hello " << name << "," << endl; cout << " The rules of this game are simple" << endl; cout << " The computer is going to display a random number and color" << endl; cout << " Once you have memorized the number and color combination, you can press any key and the computer will hide the number and color." << endl; cout << " You will then be prompted to enter the number and color (must have color capitalized) you have memorized" << endl; cout << " The computer will then display another number and color combination" << endl; cout << " When you are ready, press any key and the new number and color will be hidden" << endl; cout << " You will then be prompted to enter both sets of numbers and colors" << endl; cout << " You will get a point for each time you enter the correct number and color" << endl; cout << " The game is over when you make a mistake" << endl; cout << " Good Luck" << endl; } // end of displayInstructions void pauseFunction () { char anyKey = ' '; // user presses any key to continue cout << "Press any key if you are ready to start..."; anyKey = _getch(); } //end of pauseFunction