+1 (315) 557-6473 

Game Tracker Program using C++ Assignment Solution


Game-Tracker Assignment

For this version of the Game-tracker program, you will declare 10 objects of your game tracking class. Then create a loop that will allow the user to continue write your C++ assignment program to add new games and to see and adjust their scores until they indicate they are done. (Hint: this is indeterministic). You should present a menu to the user that lets them pick the action they want to do inside the loop at the current time and use a switch to react to the choice. Something like:

Please select a choice:

  1. Enter the new game
  2. Update times played or high score
  3. See all game stats
  4. Quit.

You will need to search for the game needed (in another loop inside the main loop) for option 2 above. Inside of the main loop, when the user wants to enter a new game, which they must do before seeing any scores, give the user the ability to enter up to 10 games and their initial scores (potentially deterministic). After the first game is entered, the other menu options become valid and new games up to 10 may be entered at any time. Your output should show the user entering incorrect values and the error checking you are doing, as in the last program as well as the entering of 3 or more games (maximum 10) and after games are entered, the checking and updating of score information and the number of times played.

Solution:

#include < iostream> #include < string> using namespace std; #define MAX_GAME 20 // maximum game created class Game { private: // Attributes string GameName; int CurrentHighScore; int NumberOfTimesPlayed; int NumberOfPlayers; public: Game() { GameName = "Default"; CurrentHighScore = 0; NumberOfTimesPlayed = 0; NumberOfPlayers = 0; } // Default Constructor // Full Constructor Game(string game_name, int current_high_score, int number_of_times_played, int number_of_players) { bool is_error = false; // initialy, there is no error if(current_high_score < 0) // error because of negative high score { cout << "Failed to create a game.\nError: Negative High Score" << endl; is_error=true; // change the error to true } if(number_of_times_played < 0) // error because of negative number_of_times_played { cout << "Failed to create a game.\nError: Negative Number of Times Played" << endl; is_error=true; // change the error to true } if(number_of_players < 0) // error because of negative number_of_players { cout << "Failed to create a game.\nError: Negative Number of Player" << endl; is_error=true; // change the error to true } if(!is_error) // if not error, assign the attributes as the user input { GameName = game_name; CurrentHighScore = current_high_score; NumberOfTimesPlayed = number_of_times_played; NumberOfPlayers = number_of_players; } else // if error, assign the attributes as the default values { GameName = "Default"; CurrentHighScore = 0; NumberOfTimesPlayed = 0; NumberOfPlayers = 0; } } // Partial Constructor for Game Name Game(string gamedisplay1) { GameName = gamedisplay1; CurrentHighScore = 0; NumberOfTimesPlayed = 0; NumberOfPlayers = 0; } // Copy Constructor Game(const Game& otherGame) { GameName = otherGame.GameName; CurrentHighScore = otherGame.CurrentHighScore; NumberOfTimesPlayed = otherGame.NumberOfTimesPlayed; NumberOfPlayers = otherGame.NumberOfPlayers; } // get and set methods and subroutine void setNumberOfPlayers(int aNumberOfPlayers) { if(aNumberOfPlayers < 0) { cout << "Error: Negative Number of Player" << endl; return; // end this attribute set here } NumberOfPlayers = aNumberOfPlayers; } void setNumberOfTimesPlayed(int aNumberOfTimesPlayed) { if(aNumberOfTimesPlayed < 0) { cout << "Error: Negative Number of Times Played" << endl; return; // end this attribute set here } NumberOfTimesPlayed = aNumberOfTimesPlayed; } void setHighscoreNumber(int aCurrentHighScore) { if(aCurrentHighScore < 0) { cout << "Error: Negative High Score" << endl; return; // end this attribute set here } CurrentHighScore = aCurrentHighScore; } void setgameName(string aGameName) { GameName = aGameName; } int getNumberOfPlayers() { return NumberOfPlayers; } int getNumberOfTimesPlayed() { return NumberOfTimesPlayed; } int getHighscoreNumber() { return CurrentHighScore; } string getgameName() { return GameName; } }; // print a helper for user choice void printChoice(){ cout << "Please select a choice:\n" "\t1. Enter new game\n" "\t2. Update high score\n" "\t3. Update number of times played\n" "\t4. Update number of players\n" "\t5. See all game stats\n" "\t6. Quit\n"; cout << "Enter you choice here: "; } void printWrongChoice() { cout << "You choose wrong choice. Try again.\n"; } void printQuit() { cout << "Program quit. Thanks.\n"; } void cannotCreateANewGame() { cout << "Game full. You cannot create a new game.\n"; } // get game name from user string getInputGameName() { string name; cout << "Input new game name: "; // get the user input according to the game name with some white space std::getline(std::cin>>std::ws,name); return name; } // get current high score from user int getCurrentHighScore() { int hs; cout << "Input current high score: "; cin >> hs; return hs; } // get number of times played from user int getNumberOfTimesPlayed() { int ntp; cout << "Input number of times played: "; cin >> ntp; return ntp; } // get number of players from user int getNumberOfPlayers() { int np; cout << "Input number of players: "; cin >> np; return np; } int selectGameIndex(Game game[MAX_GAME], int num_of_game) { if(num_of_game == 0)// no game in database { cout << "There is no game in database\n"; } else { for(int i=0;i Game " << game[i].getgameName() << "\n"; } int idx; cout << "Select game index: "; // user select the game based on index above cin >> idx; if(idx < num_of_game) { return idx; } else { cout << "Wrong index input\n"; } } return -1; // no game in database or wrong index input } void showAllGameStat(Game game[MAX_GAME], int num_of_game) { if(num_of_game == 0) // no game in database { cout << "There is no game in database\n"; } for(int i=0;i> choice; if((choice != '1') && (choice != '2') && (choice != '3') && (choice != '4')&& (choice != '5') && (choice != '6')) { printWrongChoice(); continue; // continue to "print the helper" (start from initial loop) } switch(choice){ case '1': // user select 1 (create a new game) if(num_of_game >= MAX_GAME) { // the game is full (already MAX_GAME) cannotCreateANewGame(); } else { // get user input according to game name, current high score, number of times played and number of players string name=getInputGameName(); int hs=getCurrentHighScore(); int ntp=getNumberOfTimesPlayed(); int np=getNumberOfPlayers(); game[num_of_game]=Game(name,hs,ntp,np); // assign a new game object into game[last_index] if(game[num_of_game].getgameName() != "Default") // this indicates that the constructor is successfully created { num_of_game++; // increment the number of game gameCreated(); // show the user that the game is successfully created } } break; case '2': // user select 2 (edit current highscore of an existing game) idx=selectGameIndex(game, num_of_game); if(idx != -1){ int hs=getCurrentHighScore(); // user enter the new highscore game[idx].setHighscoreNumber(hs); // update the highscore } break; case '3': // user select 3 (edit number of times played of an existing game) idx=selectGameIndex(game, num_of_game); if(idx != -1){ int ntp=getNumberOfTimesPlayed(); // user enter the new number of times played game[idx].setNumberOfTimesPlayed(ntp);// update the number of times played } break; case '4': // user select 4 (edit number of players of an existing game) idx=selectGameIndex(game, num_of_game); if(idx != -1){ int np=getNumberOfPlayers();// user enter the new number of players game[idx].setNumberOfPlayers(np);// update the number of players } break; case '5': // user select 4 (show the statistics) showAllGameStat(game, num_of_game); break; default: break; } } printQuit(); return 0; }