Skip to content

Commit e4b010e

Browse files
committed
add SupplementForm
1 parent 9134254 commit e4b010e

File tree

5 files changed

+100
-4
lines changed

5 files changed

+100
-4
lines changed

factory.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ FormStrategy* FormStrategyFactory::produce(int n) {
2929
switch (n) {
3030

3131
case 0:
32-
return new RemOrNotForm;
32+
return new RemOrNotForm(env);
33+
34+
case 1:
35+
return new SupplementForm(env);
36+
3337
default:
3438
return NULL;
3539

interaction.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ bool LearnOneWord::isLegal() {
9191

9292
void LearnOneWord::process() {
9393
env->pass(word, diffi, fstra->isOK());
94-
Def def(env->fsbDef, word);
95-
def.print(cout);
94+
cout << fstra->reDisplay();
9695
}
9796

9897

settings.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ FormSetting::FormSetting(Env* env) : Interaction(env) {
55

66
void FormSetting::work() {
77
cout << " 0. \"Remember or not\"" << endl;
8+
cout << " 1. \"Supplement\"" << endl;
89
cout << " ";
910

1011
int num;
@@ -13,7 +14,7 @@ void FormSetting::work() {
1314
if (str::isNumber(res)) {
1415
num = str::strToInt(res);
1516
// TODO:...
16-
if (num == 0) {
17+
if (num == 0 || num == 1) {
1718
break;
1819
}
1920
}

strategy.cpp

+72
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ string SelectStrategy::next(int& difficulty) {
1717
}
1818

1919

20+
FormStrategy::FormStrategy(Env* env) : env(env) {
21+
}
22+
2023
void FormStrategy::setWord(const string& word) {
2124
this->word = word;
2225
str::clearEnds(this->word);
@@ -72,8 +75,12 @@ int RandomSelection::chooseDifficulty() {
7275
}
7376

7477

78+
// -----------------------------
7579

7680

81+
RemOrNotForm::RemOrNotForm(Env* env) : FormStrategy(env) {
82+
}
83+
7784
string RemOrNotForm::display() {
7885
string res = "\n ";
7986
for (int i = 1; i <= word.length() + 2; ++i) {
@@ -90,6 +97,13 @@ string RemOrNotForm::display() {
9097
return res;
9198
}
9299

100+
string RemOrNotForm::reDisplay() {
101+
string res = "";
102+
Def def(env->fsbDef, word);
103+
def.print(res);
104+
return res;
105+
}
106+
93107
void RemOrNotForm::rcvResponse(const string& resp) {
94108
string s = resp;
95109
str::clearEnds(s);
@@ -106,3 +120,61 @@ void RemOrNotForm::rcvResponse(const string& resp) {
106120
legal = false;
107121
ok = false;
108122
}
123+
124+
125+
SupplementForm::SupplementForm(Env* env) : FormStrategy(env) {
126+
}
127+
128+
string SupplementForm::display() {
129+
string res = "\n ";
130+
for (int i = 1; i <= word.length() + 2; ++i) {
131+
res += "-";
132+
}
133+
res += " \n| ";
134+
res += processWord();
135+
res += " |\n ";
136+
for (int i = 1; i <= word.length() + 2; ++i) {
137+
res += "-";
138+
}
139+
res += "\n";
140+
Def def(env->fsbDef, word);
141+
def.print(res);
142+
res += " ";
143+
return res;
144+
}
145+
146+
string SupplementForm::reDisplay() {
147+
string res = " ";
148+
if (ok) {
149+
res += "";
150+
}
151+
else {
152+
res += "×";
153+
}
154+
res += " " + word + "\n\n";
155+
return res;
156+
}
157+
158+
void SupplementForm::rcvResponse(const string& resp) {
159+
string s = resp;
160+
str::clearEnds(s);
161+
legal = true;
162+
ok = (s == word);
163+
}
164+
165+
string SupplementForm::processWord() {
166+
string res = word;
167+
srand(time(0));
168+
int len = word.length();
169+
int blank = (len - 1) / 2;
170+
for (int i = 1; i <= blank; ++i) {
171+
for (;;) {
172+
int now = rand() % (len - 1) + 1;
173+
if (res[now] != '_') {
174+
res[now] = '_';
175+
break;
176+
}
177+
}
178+
}
179+
return res;
180+
}

strategy.h

+20
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@ class SelectStrategy {
2626
class FormStrategy {
2727

2828
public:
29+
FormStrategy(Env* env);
2930
virtual string display() = 0;
31+
virtual string reDisplay() = 0;
3032
virtual void rcvResponse(const string& resp) = 0;
3133

3234
void setWord(const string& word);
3335
bool isLegal();
3436
bool isOK();
3537

3638
protected:
39+
Env* env;
3740
string word;
3841
bool legal, ok;
3942

@@ -89,8 +92,25 @@ class RandomSelection : public SelectStrategy {
8992
class RemOrNotForm : public FormStrategy {
9093

9194
public:
95+
RemOrNotForm(Env* env);
96+
97+
string display();
98+
string reDisplay();
99+
void rcvResponse(const string& resp);
100+
101+
};
102+
103+
class SupplementForm : public FormStrategy {
104+
105+
public:
106+
SupplementForm(Env* env);
107+
92108
string display();
109+
string reDisplay();
93110
void rcvResponse(const string& resp);
111+
112+
private:
113+
string processWord();
94114

95115
};
96116

0 commit comments

Comments
 (0)