guessingGame

rtf

School

Tacoma Community College *

*We aren’t endorsed by this school

Course

142

Subject

Computer Science

Date

Nov 24, 2024

Type

rtf

Pages

10

Uploaded by HeavyA

Report
#include <iostream> #include <math.h> #include <stdlib.h> #include <time.h> #include <limits.h> #include <string.h> #define NUM_LEADERS 5 /*---------------------------------------------------- Struct to store player data ----------------------------------------------------*/ typedef struct { char name[30]; int num_guesses; } Player; /*---------------------------------------------------- GetGuess prompts the user for a guess between 10 and 100 and checks for valid input ----------------------------------------------------*/ int GetGuess() { int guess; printf("Guess a value between 10 and 100: "); int num_values = scanf("%d", &guess); // Use anytime reading input from user while (getchar() != '\n'); // clear buffer of newline // re-prompt if user enters invalid guess while (num_values != 1 || guess < 10 || guess > 100) { printf("Make sure your guess is between 10 and 100: "); num_values = scanf("%d", &guess); while (getchar() != '\n'); } return guess; } /*---------------------------------------------------- PlayGuessingGame generates the number to be guessed and then prompts the user for a guess; guesses are indicated to be 'too high' or 'too low' until the user guesses correctly; returns the number of guesses made ----------------------------------------------------*/ int PlayGuessingGame() { // Generate a random number between 10 and 100 and find its square root
srand(( unsigned int )time( NULL )); // Seed generator with the current time int numberToGuess = rand() % 100 + 1; double squareRoot = sqrt(numberToGuess); // cout << squareRoot << " is the square root of what number?" << endl; printf("%.8f is the square root of what number? (%d) ", squareRoot, numberToGuess); //printf("%.8f is the square root of what number?", squareRoot); int num_guesses = 0; char done = 0; while (!done) { int guess = GetGuess(); num_guesses++; if (guess < numberToGuess) printf("Too low, guess again: "); else if (guess > numberToGuess) printf("Too high, guess again: "); else done = 1; } printf("You got it baby!\n"); return num_guesses; } /*---------------------------------------------------- InsertPlayer inserts a player into the leaders array such that the order is maintained ----------------------------------------------------*/ void InsertPlayer(Player leaders[], Player player) { // find insertion index int insertion_idx = -1; for ( int i = NUM_LEADERS-1; i >= 0; i--) if (player.num_guesses < leaders[i].num_guesses) insertion_idx = i; if (insertion_idx != -1) { // right shift players if necessary int i = NUM_LEADERS-1; while (i > insertion_idx) { leaders[i] = leaders[i-1]; i--;
} // put new player at good index leaders[insertion_idx] = player; } } /*---------------------------------------------------- Printleaders displays the current state of the leaders array ----------------------------------------------------*/ void Printleaders(Player leaders[]) { printf("Here are the current leaders:\n"); for ( int i = 0; i < NUM_LEADERS; i++) { if (leaders[i].num_guesses != INT_MAX) printf("%s made %d guesses\n", leaders[i].name, leaders[i].num_guesses); } } /*---------------------------------------------------- Writeleaders writes the leaders array to a text file ----------------------------------------------------*/ void Writeleaders( char filename[], Player leaders[]) { FILE* fp = fopen(filename, "w"); // Check file before opening if (fp == NULL ) printf("Error opening file\n"); for ( int i = 0; i < NUM_LEADERS; i++) if (leaders[i].num_guesses != INT_MAX) fprintf( fp, "%s %d\n", leaders[i].name, leaders[i].num_guesses); fclose(fp); } /*---------------------------------------------------- Readleaders reads data from the file into the leaders array ----------------------------------------------------*/ void Readleaders( char filename[], Player leaders[]) { FILE* fp = fopen(filename, "r"); if (fp == NULL ) return ; for ( int i = 0; i < NUM_LEADERS; i++) fscanf( fp, "%s %d\n", leaders[i].name, &(leaders[i].num_guesses)); fclose(fp); } /*---------------------------------------------------- main prompts the user to play or quit and then enters a loop to allow the user to play as many times as they like
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
----------------------------------------------------*/ int main() { Player leaders[NUM_LEADERS]; for ( int i = 0; i < NUM_LEADERS; i++) leaders[i].num_guesses = INT_MAX; Readleaders("/Users/kylemabrey/CSE240SrcFiles/leaderboard.txt", leaders); printf("Welcome! Press 'q' to quit or any other key to continue:\n"); char c, game_over = 0; while (!game_over) { scanf(" %c", &c); if (c == 'q') { game_over = 1; printf("Bye Bye!\n"); } else { Player current_player; printf("Please enter your name to start:"); scanf("%s", current_player.name); current_player.num_guesses = PlayGuessingGame(); printf("You made %d guesses.\n", current_player.num_guesses); InsertPlayer(leaders, current_player); Printleaders(leaders); printf("Press 'q' to quit or any other key to continue:\n"); } } Writeleaders("/Users/kylemabrey/CSE240SrcFiles/leaderboard.txt", leaders); return 0; }
The program for A5 must be converted to C++. This includes using iostream instead of stdio. For file I/O you can continue to use fprintf etc. by including cstdio. The main function and the PlayGuessingGame functions can remain as standalone functions but you will need to create a class for the leaderboard and for the player
#include <iostream> #include <cmath> #include <cstdlib> #include <ctime> #include <limits> #include <fstream> #include <cstring> #define NUM_LEADERS 5 using namespace std; /*---------------------------------------------------- Struct to store player data ----------------------------------------------------*/ struct Player { char name[30]; int num_guesses; }; /*---------------------------------------------------- GetGuess prompts the user for a guess between 10 and 100 and checks for valid input ----------------------------------------------------*/ int GetGuess() { int guess; cout << "Guess a value between 10 and 100: "; while (!(cin >> guess) || guess < 10 || guess > 100) { cout << "Make sure your guess is between 10 and 100: "; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); } cin.ignore(numeric_limits<streamsize>::max(), '\n'); return guess; } /*---------------------------------------------------- PlayGuessingGame generates the number to be guessed and then prompts the user for a guess; guesses are indicated to be 'too high' or 'too low' until the user guesses correctly; returns the number of guesses made
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
----------------------------------------------------*/ int PlayGuessingGame() { srand(static_cast<unsigned int>(time(NULL))); int numberToGuess = rand() % 100 + 1; double squareRoot = sqrt(numberToGuess); cout << squareRoot << " is the square root of what number? (" << numberToGuess << ") "; int num_guesses = 0; bool done = false; while (!done) { int guess = GetGuess(); num_guesses++; if (guess < numberToGuess) cout << "Too low, guess again: "; else if (guess > numberToGuess) cout << "Too high, guess again: "; else done = true; } cout << "You got it!\n"; return num_guesses; } /*---------------------------------------------------- InsertPlayer inserts a player into the leaders array such that the order is maintained ----------------------------------------------------*/ void InsertPlayer(Player leaders[], Player player) { int insertion_idx = -1; for (int i = NUM_LEADERS - 1; i >= 0; i--) { if (player.num_guesses < leaders[i].num_guesses) { insertion_idx = i; } } if (insertion_idx != -1) { for (int i = NUM_LEADERS - 1; i > insertion_idx; i--)
{ leaders[i] = leaders[i - 1]; } leaders[insertion_idx] = player; } } /*---------------------------------------------------- Printleaders displays the current state of the leaders array ----------------------------------------------------*/ void Printleaders(Player leaders[]) { cout << "Here are the current leaders:\n"; for (int i = 0; i < NUM_LEADERS; i++) { if (leaders[i].num_guesses != numeric_limits<int>::max()) { cout << leaders[i].name << " made " << leaders[i].num_guesses << " guesses" << endl; } } } /*---------------------------------------------------- Writeleaders writes the leaders array to a text file ----------------------------------------------------*/ void Writeleaders(const char filename[], Player leaders[]) { ofstream file(filename); if (file.is_open()) { for (int i = 0; i < NUM_LEADERS; i++) { if (leaders[i].num_guesses != numeric_limits<int>::max()) { file << leaders[i].name << " " << leaders[i].num_guesses << endl; } } file.close(); } else { cout << "Error opening file: " << filename << endl; } } /*----------------------------------------------------
Readleaders reads data from the file into the leaders array ----------------------------------------------------*/ void Readleaders(const char filename[], Player leaders[]) { ifstream file(filename); if (file.is_open()) { for (int i = 0; i < NUM_LEADERS; i++) { file >> leaders[i].name >> leaders[i].num_guesses; } file.close(); } } /*---------------------------------------------------- main prompts the user to play or quit and then enters a loop to allow the user to play as many times as they like ----------------------------------------------------*/ int main() { Player leaders[NUM_LEADERS]; for (int i = 0; i < NUM_LEADERS; i++) { leaders[i].num_guesses = numeric_limits<int>::max(); } Readleaders("/Users/kylemabrey/CSE240SrcFiles/leaderboard.txt", leaders); cout << "Welcome! Press 'q' to quit or any other key to continue:\n"; char c; bool game_over = false; while (!game_over) { cin >> c; if (c == 'q') { game_over = true; cout << "Bye Bye!\n"; } else { Player current_player;
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
cout << "Please enter your name to start: "; cin >> current_player.name; current_player.num_guesses = PlayGuessingGame(); cout << "You made " << current_player.num_guesses << " guesses.\n"; InsertPlayer(leaders, current_player); Printleaders(leaders); cout << "Press 'q' to quit or any other key to continue:\n"; } } Writeleaders("/Users/kylemabrey/CSE240SrcFiles/leaderboard.txt", leaders); return 0; }