Implement a bulls and cows solver that given a secret word

Assignment Help Computer Engineering
Reference no: EM131168787

Programming Assignment Bulls and Cows

This project is about a modified version of Bulls and Cows. Instead of playing the game by using 4-letter words, the number of letters in a word in this modified version for the player to guess ranges from 1 to 21 letters. The modified version of the game plays as follows:

• A player (Host) comes up with a valid secret word from the word list.

• Another player (Guesser) tries to come up with a probe word.

• The Host responds by giving the Guesser the number of bulls and cows for the probe word. "Bulls" gives information about how many letters in the probe word match the letters in the secret word in the right position while "Cows" gives information about how many letters in the probe word match the letters in the secret word in the wrong position. The number of letters counted in "Bulls" cannot be double counted in "Cows" though.

• If the Guesser correctly guessed the secret word, then the game is over. Otherwise,the Guesser needs to come up with another probe word and obtains feedback from the Host until the Guesser correctly guesses the secret word.

For example, if the secret word is apocalypse and the probe word is abstract, then the Host should respond 1 "Bulls" and 3 "Cows". If the secret word is apocalypse and the probe word is a, then the Host should respond 1 "Bulls" and 0 "Cows". If the secret word is abstract and the probe word is abstain, then the Host should respond 4 "Bulls" and 1 "Cows".

Your task for this project will be to implement a Bulls and Cows solver that, given a secret word in the test program, will produce as few number of probe words as possible that lead the Guesser to correctly guess the secret word. Because we may give you a large word list, you will want to come up with an efficient way of generating as few number of probe words as possible that correctly guess the secret word. It is possible that your algorithm might correctly guess the secret word in 1 step, but the probability is 1/60000 given a word list with 60,000 words.

Here is the interface for a Game class (Game.h) that lets you initialize a word list we provide in the constructor, set up a a secret word, determine the length of the secret word, and determine the "Bulls" (nInRightPlace) and "Cows" (nInWrongPlace) from the probe word provided by the Player:

class Game
{
public:
Game(const vector<string> &words); bool setSecretWord(const string &sw); int secretWordLength() const;
void probe(const string &probeWord, int &nInRightPlace, int &nInWrongPlace);
~Game(); private:
// Prevent people from copying Game objects. Game(const Game&);
Game& operator=(const Game&);
// Define your own data structure here
// ...
string secretWord;
};

• The constructor stores the given word list in a data structure of your choice as private data member. setSecretWord() takes in a secret word and checks against the word list to determine if the given secret word is in the word list. If it is, return true, otherwise return false.

• secretWordLength() returns the length of the secret word.

• probe() takes in a probe word provided by the Player, determines "Bulls" and "Cows", stores the correct value of "Bulls" in nInRightPlace, and stores the correct value of "Cows" in nInWrongPlace

• The destructor does whatever is necessary to clean up.

Here is the interface for a Player class (Player.h).

class Player
{
public:
Player(const vector<string> &words); string generateProbeWord();
void learn(string probe, int nInRightPlace, int nInWrongPlace);
~Player(); private:
// Prevent people from copying Player objects. Player(const Player&);
Player& operator=(const Player&);
// Define your own data structure here
};

• The constructor stores the given word list in a data structure of your choice as private data member. You may also need to build a data structure inside the constructor that allows you to efficiently search for the next probe word to return when generateProbeWord() is called.

• generateProbeWord() returns a probe word chosen by the Player.

• learn() tells the Player how many bulls and cows are in the probe word; that information presumably comes from a previous call to Game::probe(). The Player object does what is necessary to update its data structures in light of that information.

• The destructor does whatever is necessary to clean up your data structures.

None of these member functions may read from cin or write to cout. (They can write debugging information to cerr, which we will ignore.)

Here is a very simple working solution for a Player class that does not meet the requirement for this project as it performs a linear search through the word list.

class Player
{
public:
Player(const vector<string> &words) { wordlist = words;
n = 0;
}
string generateProbeWord() { return wordlist[n++];
}
void learn(string probe, int nInRightPlace, int nInWrongPlace) { }
~Player() { } private:
// Prevent people from copying Player objects.
Player(const Player&);
Player& operator=(const Player&);
// Define your own data structure here vector<string> wordlist;
int n;
};

When we test your code, we will use a large word list file of about 60,000 words that we will provide you here. (The file came from an open-source wordlist project that is hosted at wordlist.sourceforge.net. If you're interested in building a freeware or open-source application that requires a large list of English words, this is a great place to get a wordlist for it.) Here's a small example of some tests:

#include <iostream>
#include <fstream>
...
using namespace std;

const char* filename = " ";
// A Windows user might have the path be "C:/CS32/P4/wordlist.txt"
// A Mac user might have it be "/Users/fred/CS32/P4/wordlist.txt"

void play(Game& g, Player &p)
{
int secretLength = g.secretWordLength(); int nTurns = 0;
for(;;)
{
int nInRightPlace, nInWrongPlace; string probe = p.generateProbeWord();
g.probe(probe, nInRightPlace, nInWrongPlace);
// repeat probes until word is guessed nTurns++;
if (nInRightPlace == secretLength) break;
p.learn(probe, nInRightPlace, nInWrongPlace);
}

cerr << "The Player probed " << nTurns << " times!" << endl;
}

int main()
{
ifstream wordlistfile(filename); if (!wordlistfile)
{
cerr << "Cannot open " << filename << "!" << endl; return 1;
}
vector<string> words;

string w;
while (wordlistfile >> w)
words.push_back(w);

Player player(words); Game g(words);

if (!g.setSecretWord("apocalypse")) // Supply the secret word
{
cerr << "Secret word is not in word list!" << endl;

return 1;
}

play(g, player);
}

Here are some requirements your program must meet:

• You must not add any public members to Game or Player. You are allowed to add private data members and private member functions if you wish.

• Your program should be efficient enough to generate as few number of probe words as possible that lead the Player (Guesser) to correctly guess the secret word. A linear search solution provided above will guarantee to fail the performance requirement for this project. In order to satisfy this performance requirement, your Player constructor is allowed to run up to 25 seconds on the SEASnet Linux server, and your generateProbeWord and learn functions are allowed to run up to 5 seconds each on the SEASnet Linux server.

• You may design interesting data structures of your own, but the only containers from the C++ standard library you may use are vector, list, stack, and queue (and string). If you want anything fancier, implement it yourself. (It'll be a good exercise for you.) If you decide to use a hash table, it must not have more than 100000 buckets. (Using a hash table is a good way to get reasonable speed with a large word list.) Although we're limiting your use of containers from the library, you are free to use library algorithms (e.g., sort).

• During execution, your program must not perform any undefined actions, such as dereferencing a null or uninitialized pointer.

Attachment:- Wordlist.rar

Reference no: EM131168787

Questions Cloud

Responsibility for establishing and implementing health : Describe the importance of taking responsibility for establishing and implementing health maintenance for individuals of all ages by keeping the three areas of health in balance
Use edge finding conditions to check for valid precedences : Use the edge-finding conditions (3.112) to check for valid precedences, and update the bounds accordingly. Does edge finding identify all valid precedences?
Describe the methods for establishing component priorities : Describe the methods for establishing component priorities, including Business functions and processes b. BIA scenarios and components c. Financial and service impact of components not being available d. Recovery time frameworks.
Does edge finding identify all valid precedences : Use the edge-finding conditions (3.112) to check for valid precedences, and update the bounds accordingly.-  Does edge finding identify all valid precedences?
Implement a bulls and cows solver that given a secret word : Your task for this project will be to implement a Bulls and Cows solver that, given a secret word in the test program, will produce as few number of probe words as possible that lead the Guesser to correctly guess the secret word.
What can you do to try to minimize the stress : Explain how and why these conditions create an optimum environment for stress. What can you do to try to minimize the stress in these situations? Include things like environment, time management and others
Use edge finding conditions to check for valid precedences : Use the edge-finding conditions (3.112) to check for valid precedences, and update the bounds accordingly. Does edge finding identify all valid precedences?
How does this apply to government-created interest groups : What is the relationship between interest groups and government? How does this apply to government-created interest groups? In addition, what are the effects of bureaucrats as interest groups? Do you believe this crossover between bureaucrats and ..
Compute census data in various situations : Assignment: Complete and Analyze a Census. Compute census data in various situations, Calculate length of stay in various situations and Consider how census values affect the management decisions in the day-to-day operations and PI

Reviews

Write a Review

Computer Engineering Questions & Answers

  Questiondesign and apply a java program use switch

questiondesign and apply a java program use switch statement that find a traffic violation number and output traffic

  Define how much more secure is this double encryption

find two substitution ciphers. One adds a value of i to the ASCII code of the plaintext character. The other adds a value of j to the plaintext character. All additions are modulo 256.

  How arrangement of subnet masks to make this possible

Suggest what the company may do if department D grows to 34 hosts.

  What repetition control structure is used in java

How may easy to read or highly documented code become a security risk?

  Give us your insights on why packet switched networks are

give us your insights on why packet switched networks are the future of telecommunications? what applicationsuses will

  Developing countermeasures against dos attacks

Explain whether the administrators of server systems still have to be concerned about, and take the further countermeasures against DoS attacks, if so, what kinds of attacks may still happen, and what measures can be taken in order to decrease the..

  Design an eight-input priority encoder

Design an eight-input priority encoder with inputs A7:0 and outputs Y2:0 and NONE should be 0. Give a simplified Boolean equation for each output.

  Decimal representation of all the numbers

Read a positive integer input n, and count the number of occurrences of the digit '9' in the decimal representation of all the numbers between 1 and n inclusive. Print that number.

  Process in java that uses the bubble-sort algorithm

process in Java that uses the bubble-sort algorithm. The bubble-sort algorithm makes many passes through the array. On each pass, successive neighboring pairs are compared.

  External gates to realize the function

Use a 4-to-1 multiplexer and a minimum number of external gates to realize the function - The inputs are only available uncomplemented.

  Describe measures which can be taken to increasing employee

reflection paper on your iss680 readings exercises and discussion posts.the paper should includea brief summary of your

  Assess the impact of the internet on newspaper and book

q1. evaluate the impact of the internet on newspaper and book publishers using the value chain and competitive forces

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd