Question

Game in C++. Need to make use of object-oriented techniques, i.e. implementation of classes and objects....

Game in C++. Need to make use of object-oriented techniques, i.e. implementation of classes and objects. Given a file containing a list of 4-letter words, the program needs to make the user guess. Each word difference is a minus one point. Each match is plus 4 points. String in capital or lower case should not pose a problem. The nearest match condition is the least number of difference between the two words.

For example:

(file.txt)

JAZZ BUZZ FUZZ FIZZ QUIZ

(Program)

Enter a four-letter word which ends in 'z': jazz

Points +4

Total: 4

Enter a four-letter word which ends in 'z': bizz

Nearest match: BUZZ FIZZ (1 change needed)

Points -1

Total: 3

Enter a four-letter word which ends in 'z': jazz

You entered that already! Try a new one...

Enter a four-letter word which ends in 'z': just

Word does not end in 'z'. Try a new one...

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
class A
{
   public:
   vector<string> tokenise(string fullpath, char chr)//returns tokens from a sentence by splitting words seperated by space
   {
       string strdup = "";//inner string which stores each word
       int i = 0; vector<string>tokens;
       for (i = 0; fullpath[i]; i++)//read entire string
       {
           if (fullpath[i] != chr)//if it is not a space add it to strdup
           {
               strdup += fullpath[i];
           }
           else
           {
               tokens.push_back(strdup);//otherwise add the string obtained tokens
               strdup = "";//initialise for next word
           }
       }
       tokens.push_back(strdup);
       return tokens;//return
   }
   string upp(string a)//converts entire string to uppercase
   {  
       transform(a.begin(), a.end(), a.begin(), ::toupper);;
       return a;
   }
   string low(string a)//converts entire string to lowercase
   {
       transform(a.begin(), a.end(), a.begin(), ::tolower);;
       return a;
   }
   int comp(string a, string b)//gives no. of non-matching characters
   {
       int ans = 0;a=upp(a);b=upp(b);
       for(int i=0;i<a.size();i++)
       {
           if(a[i]!=b[i]) ans++;
       }
       return ans;
   }
   vector<string> read_file()//returns words from a file
   {
       ifstream file ("file.txt");string line;
       vector<string>res, res2;
       while( getline ( file, line) )//read file line by line
       {
           res2 = tokenise(line,' ');//tokenise
           for(int i = 0; i < res2.size(); i++)//push tokens into res
           {
               res.push_back(res2[i]);
           }
       }
       return res;
   }
   void operate(vector<string> res, map<string,int> m)//objective function
   {
       int total = 0, cnt = 0;
       while(1)
       {
           cout << "Enter a four-letter word which ends in 'z': ";
           string inp;cin>>inp;
           if(m.find(low(inp)) == m.end() && m.find(upp(inp)) == m.end())//if string is not present in required words
           {
               if(inp[inp.size()-1] != 'z' && inp[inp.size()-1] != 'Z')//if last letter isn't z
               {
                   cout<<"Word does not end in 'z'. Try a new one..."<<endl;
                   continue;
               }
               int ans = 5, num;vector<string> loc;//ans has no of non-matching characters
               for(map<string,int>::iterator iter = m.begin(); iter != m.end(); ++iter)
               {
                   num=comp(iter->first, inp);
                   if(num<ans)//if there is a string with less no. of non-matching characters
                   {
                       ans = num;loc.clear();loc.push_back(iter->first);
                   }
                   else if(num==ans)//if the string has same no. of non matching characters as that of a previously obtained one
                   {
                       loc.push_back(iter->first);
                   }
               }
               cout<<"Nearest match: ";
               for(int k=0;k<loc.size();k++) cout<<loc[k]<<" ";//print all near matching strings
               cout<<"("<<ans<<" change needed)"<<endl;
               cout<<"Points -"<<ans<<endl;
               total-=ans;
               cout<<"Total: "<<total<<endl;
           }
           else if(m[upp(inp)] == 1)//if inp has already been guessed
           {
               cout<<"You entered that already! Try a new one..."<<endl;
           }
           else//if inp hadn't been guessed yet
           {
               cout<<"Points +4"<<endl;
               total += 4;
               cout<<"Total: "<<total<<endl;
               m[upp(inp)]=1;
               cnt++;//no of correct guesses
           }
           if(cnt == m.size()) break;//no of correct guesses is equal to available words, stop asking
       }
   }
};  
int main()
{
   A obj;
   vector<string>res=obj.read_file();
   map<string, int>m;//map stores 0 if the word hadn't been guessed, 1 otherwise
   for(int i = 0; i < res.size(); i++)
   {
       m.insert(make_pair(res[i], 0));
   }
   obj.operate(res, m);
   return 0;
}

Output:

Add a comment
Know the answer?
Add Answer to:
Game in C++. Need to make use of object-oriented techniques, i.e. implementation of classes and objects....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create the game hangman using c code: Program description In the game of hangman, one player...

    Create the game hangman using c code: Program description In the game of hangman, one player picks a word, and the other player has to try to guess what the word is by selecting one letter at a time. If they select a correct letter, all occurrences of the letter are shown. If no letter shows up, they use up one of their turns. The player is allowed to use no more than 10 incorrect turns to guess what the...

  • I am trying to make a word guessing game on java, where there are four letters...

    I am trying to make a word guessing game on java, where there are four letters and you have 10 guesses to try and guess the letter combination. When I try to play, it says every combination of letters I enter is correct. For example: "You have 10 guesses left. Enter your guess: wxyz There are 4 correct letter placements. Congrats!" I emailed my professor and she said one of my methods is wrong because I'm only checking for specific...

  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

  • Question 2: Finding the best Scrabble word with Recursion using java Scrabble is a game in...

    Question 2: Finding the best Scrabble word with Recursion using java Scrabble is a game in which players construct words from random letters, building on words already played. Each letter has an associated point value and the aim is to collect more points than your opponent. Please see https: //en.wikipedia.org/wiki/Scrabble for an overview if you are unfamiliar with the game. You will write a program that allows a user to enter 7 letters (representing the letter tiles they hold), plus...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • Word Guessing Game Assignment Help Needed. This C++ (I'm using Visual Studio 2015) assignment is actually...

    Word Guessing Game Assignment Help Needed. This C++ (I'm using Visual Studio 2015) assignment is actually kind of confusing to me. I keep getting lost in all of the words even though these are supposed to instruct me as to how to do this. Can I please get some help as to where to start? Word guessing game: Overview: Create a game in which the user has a set number of tries to correctly guess a word. I highly recommend...

  • Using C programming REQUIREMENTS: This program is a letter guessing game where a simple Al opponent...

    Using C programming REQUIREMENTS: This program is a letter guessing game where a simple Al opponent generates a secret letter for the user to guess. The user must be able to take turns guessing the secret letter, with the Al responding to the user's guesses. After successfully guessing, the user must be allowed to play again. The Al must count how many turns the user has taken. Here is an example of a game in progress where the human user...

  • Original question IN JAVA Show the user a number of blank spaces equal to the number...

    Original question IN JAVA Show the user a number of blank spaces equal to the number of letters in the word For cat, you would show _ _ _. Prompt a user to guess a letter until they have run out of guesses or guessed the full word By default, the user should be able to make 7 failed guesses before they lose the game. If the user guesses a letter that is in the target word, it does not...

  • For this week's lab, you will use two of the classes in the Java Collection Framework:...

    For this week's lab, you will use two of the classes in the Java Collection Framework: HashSet and TreeSet. You will use these classes to implement a spell checker. Set Methods For this lab, you will need to use some of the methods that are defined in the Set interface. Recall that if set is a Set, then the following methods are defined: set.size() -- Returns the number of items in the set. set.add(item) -- Adds the item to the...

  • For this week's lab, you will use two of the classes in the Java Collection Framework:...

    For this week's lab, you will use two of the classes in the Java Collection Framework: HashSet and TreeSet. You will use these classes to implement a spell checker. Set Methods For this lab, you will need to use some of the methods that are defined in the Set interface. Recall that if set is a Set, then the following methods are defined: set.size() -- Returns the number of items in the set. set.add(item) -- Adds the item to the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT