Sie sind auf Seite 1von 5

Project 4 Test Data

For the Game class, there were 24 test cases, worth 2 points each. For basic correctness testing of Player, there
were 5 test cases worth 3 points each on a small word list; the test reports the number of turns needed for
Player::generateProbeWord to return a probe that was the secret word; you got the 3 points if the number was not
unreasonably large.

For the remaining 37 points, we used the 60386-word list (without the word "I") and determined the number of
turns it took for Player::generateProbeWord to return a probe that was the secret word for each of the following
13 words (using a new instance of Player for each word): neat nymph parse spare inane jockeys success
shoplifter grandchild unpredictably extraterrestrials electroencephalogram uncharacteristically. We compared
your program's performance to those of other students to determine a score. Those who needed comparatively
few probes to determine the secret word got the full 37 points; people who needed more probes got fewer points.
Points were also deducted for programs that crashed or needed more than 100 probes.

Of the student submisions for Project 4, here are the minimum and the median number of probes that Player
generated to determine the following secret words:

Min Median
8 21 neat
7 16 nymph
6 23 parse
7 26 spare
7 16 inane
7 16 jockeys
5 16 success
7 19 shoplifter
4 12 grandchild
5 16 unpredictably
4 13 extraterrestrials
1 10 electroencephalogram
3 12 uncharacteristically

The people who achieved a low number of probes all used a strategy like the following: Each time you learn
from the result of a probe, you eliminate from the word list all those words that are inconsistent with the results
of all probes made so far. Your next probe will be one of the remaining possible words. Simply picking one of
the remaining possible words at random works remarkably well.
#include "Game.h"
#include "Player.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <cstdlib>
#include <cassert>
using namespace std;

const char* wordsfile = "wordlist.txt";


vector<string> getfile()
{
ifstream wf(wordsfile);
if (!wf)
{
cerr << "Cannot open " << wordsfile << endl;
throw 1;
}
vector<string> words;
string s;
while (wf >> s)
words.push_back(s);
return words;
}

void testprobe(string secret, string probe, int bulls, int cows)


{
vector<string> words({ probe, secret, string("pika") });
Game g(words);
g.setSecretWord(words[1]);
int b = 0, c = 0;
g.probe(words[0], b, c);
assert(b == bulls && c == cows);
}

void countMatches(const string& s1, const string& s2, int& nInRightPlace, int& nInWrongPlace)
{
const char BULL_BLOT = '#'; // any nonword character will do
const char COW_BLOT = '@'; // any nonword character will do

string s1copy(s1);

// First, count and blot out the bulls so they won't be matched later.
// Check every position for a bull, stopping at the end of the
// shorter string

int shorterLen = min(s1.size(), s2.size());

nInRightPlace = 0;
for (int k = 0; k != shorterLen; k++)
{
if (s1copy[k] == s2[k])
{
nInRightPlace++;
s1copy[k] = BULL_BLOT;
}
}

// Now count the cows. For every character in s2

nInWrongPlace = 0;
for (int k2 = 0; k2 != s2.size(); k2++)
{
// Don't count bulls

if (k2 < shorterLen && s1copy[k2] == BULL_BLOT)


continue;

// For every character in the copy of s1

auto p = find(s1copy.begin(), s1copy.end(), s2[k2]);


if (p != s1copy.end())
{
nInWrongPlace++;
*p = COW_BLOT;
}
}
}
}

int play(const vector<string>& words, string secret)


{
Player p(words);
int nTurns;
for (nTurns = 1; nTurns <= 100; nTurns++)
{
string probe = p.generateProbeWord();
int nInRightPlace, nInWrongPlace;
countMatches(secret, probe, nInRightPlace, nInWrongPlace);
if (nInRightPlace == secret.size())
break;
p.learn(probe, nInRightPlace, nInWrongPlace);
}
return nTurns;
}

void testone(int n)
{
switch (n)
{
case 1: {
Game g({ "tiger", "heron", "pika" });
assert(!g.setSecretWord("egret"));
assert(g.setSecretWord("heron"));
} break; case 2: {
Game g({ "tiger", "heron", "pika" });
assert(g.setSecretWord("pika") && g.secretWordLength() == 4);
} break; case 3: {
Game g({ "hyrax", "bison", "pika" });
g.setSecretWord("bison");
int b = 999, c = 999;
g.probe("hyrax", b, c);
assert(b >= 0 && b <= 5 && c >= 0 && c <= 5);
} break; case 4: {
testprobe("bison", "hyrax", 0, 0);
} break; case 5: {
testprobe("bison", "tiger", 1, 0);
} break; case 6: {
testprobe("mouse", "louse", 4, 0);
} break; case 7: {
testprobe("mouse", "tiger", 0, 1);
} break; case 8: {
testprobe("minor", "rhino", 0, 4);
} break; case 9: {
testprobe("shrew", "horse", 1, 3);
} break; case 10: {
testprobe("aster", "stare", 0, 5);
} break; case 11: { // more in secret than probe
testprobe("geese", "sheep", 1, 2);
} break; case 12: { // more in probe than secret
testprobe("sheep", "geese", 1, 2);
} break; case 13: { // multiple letters
testprobe("redeem", "emerge", 0, 5);
} break; case 14: { // secret 5 probe 4
testprobe("hyena", "myna", 1, 2);
} break; case 15: { // secret 4 probe 5
testprobe("myna", "hyena", 1, 2);
} break; case 16: { // secret 6 probe 4
testprobe("badger", "bear", 1, 3);
} break; case 17: { // secret 4 probe 6
testprobe("bear", "badger", 1, 3);
} break; case 18: { // secret 6 probe 5
} break; case 18: { // secret 6 probe 5
testprobe("tigers", "tiger", 5, 0);
} break; case 19: { // secret 5 probe 6
testprobe("tiger", "tigers", 5, 0);
} break; case 20: { // secret 6 probe 4, repeats
testprobe("vervet", "veer", 2, 2);
} break; case 21: { // secret 4 probe 6, repeats
testprobe("veer", "vervet", 2, 2);
} break; case 22: { // checking doesn't corrupt word
Game g({ "llama", "halal", "pika" });
g.setSecretWord("halal");
int b = 0, c = 0;
g.probe("llama", b, c);
int b2, c2;
b2 = 0; c2 = 0; g.probe("llama", b2, c2);
assert(b == b2 && c == c2);
b2 = 0; c2 = 0; g.probe("llama", b2, c2);
assert(b == b2 && c == c2);
} break; case 23: {
vector<string> words = getfile();
Game g(words);
g.setSecretWord("electroencephalograms");
int b = 0, c = 0;
g.probe("playgrounds", b, c);
assert(b == 4 && c == 4);
} break; case 24: {
vector<string> words = getfile();
Game g(words);
g.setSecretWord("playgrounds");
int b = 0, c = 0;
g.probe("electroencephalograms", b, c);
assert(b == 4 && c == 4);
} break; case 25: {
vector<string> words { "hyrax", "tiger", "hyena", "horse",
"bison", "louse", "mouse", "heron", "rhino" };
int nTurns = play(words, "mouse");
cout << nTurns << endl;
} break; case 26: {
vector<string> words { "hyrax", "tiger", "hyena", "horse",
"bison", "louse", "mouse", "heron", "rhino" };
sort(words.begin(), words.end());
int nTurns = play(words, "rhino");
cout << nTurns << endl;
} break; case 27: {
vector<string> words { "hyrax", "tiger", "hyena", "horse",
"bison", "louse", "llama", "mouse", "heron", "rhino" };
sort(words.begin(), words.end());
int nTurns = play(words, "llama");
cout << nTurns << endl;
} break; case 28: {
vector<string> words { "hyrax", "tiger", "hyena", "horse",
"bison", "louse", "llama", "mouse", "heron", "rhino",
"badger", "vole" };
sort(words.begin(), words.end());
int nTurns = play(words, "badger");
cout << nTurns << endl;
} break; case 29: {
vector<string> words { "hyrax", "tiger", "hyena", "horse",
"bison", "louse", "llama", "mouse", "heron", "rhino",
"badger", "vole" };
sort(words.begin(), words.end());
int nTurns = play(words, "vole");
cout << nTurns << endl;
} break;
}
}
}

int main(int argc, char* argv[])


{
cout << "Enter test number: ";
int n;
cin >> n;
testone(n);
cout << "Passed" << endl;
}

Das könnte Ihnen auch gefallen