-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.cpp
377 lines (353 loc) · 10 KB
/
core.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <cmath>
class Level {
public:
int input_num, input_index;
int inputs[100];
int space_num;//表示关卡中空格数
int space[8];
bool avliable_space[8];//表示有积木的空格
bool avliable_command[8];//8位数组 表示每位对应的指令集是否可用
int output_index;
int outputs[100];
int std_outputs[100];
bool pass();
};
bool Level::pass() {
for (int i = 0; i < 100; ++i) if (outputs[i] != std_outputs[i]) return false;
return true;
}
class Command {
private:
int counter,counter_p;//表示玩家一共输入了多少行指令集
public:
Command() { this->counter = this->counter_p = 0; for (int i = 0; i < 100; ++i) collection[i][0] = collection[i][1] = 0; };
int collection[100][2];//第一位表示指令 第二位表示可能的指令参数,没有则用-1代替
bool add_command(std::string);
bool setCounter(int);
int getCounter() { return this->counter; };
};
bool Command::add_command(std::string a) {
if ("" == a) return true;
int i = 0;
for (i = 0; i < a.length(); ++i)
if (' ' == a[i]) break;
std::string c(a, 0, i);
int arg = -1;
if (i < a.length() - 1) arg = a[i + 1] - 48;//目前认为参数为一位数字
if ("inbox" == c) {
this->collection[counter_p][0] = 0;
this->collection[counter_p][1] = arg;
++counter_p;
return true;
}
else if ("outbox" == c) {
this->collection[counter_p][0] = 1;
this->collection[counter_p][1] = arg;
++counter_p;
return true;
}
else if ("add" == c) {
this->collection[counter_p][0] = 2;
this->collection[counter_p][1] = arg;
++counter_p;
return true;
}
else if ("sub" == c) {
this->collection[counter_p][0] = 3;
this->collection[counter_p][1] = arg;
++counter_p;
return true;
}
else if ("copyto" == c) {
this->collection[counter_p][0] = 4;
this->collection[counter_p][1] = arg;
++counter_p;
return true;
}
else if ("copyfrom" == c) {
this->collection[counter_p][0] = 5;
this->collection[counter_p][1] = arg;
++counter_p;
return true;
}
else if ("jump" == c) {
this->collection[counter_p][0] = 6;
this->collection[counter_p][1] = arg;
++counter_p;
return true;
}
else if ("jumpifzero" == c) {
this->collection[counter_p][0] = 7;
this->collection[counter_p][1] = arg;
++counter_p;
return true;
}
else return false;
}
bool Command::setCounter(int a) {
if (a >= 1) this->counter = a;
else return false;
return true;
}
class Robot {
private:
int boxInHand;
bool hasBoxInHand;
public:
Robot() { boxInHand = 0; hasBoxInHand = false; };
int inbox(Level*);
bool outbox(Level*);
bool add(Level, int);
bool sub(Level, int);
bool copyto(Level*, int);
bool copyfrom(Level, int);
int jump(Command, int);//返回目标的行数 error为-1
int jumpifzero(Command, int);//返回目标的行数 error为-1 不操作为-2
int executeCommmands(Level*, Command);
};
int Robot::inbox(Level* level) {
if (level->input_index >= level->input_num) return -1;
if (level->input_index >= level->input_num) return 0;
boxInHand = level->inputs[level->input_index];
++level->input_index;
hasBoxInHand = true;
return 1;
}
bool Robot::outbox(Level* level) {
if (!hasBoxInHand) return false;
else {
level->outputs[level->output_index] = boxInHand;
++level->output_index;
hasBoxInHand = false;
return true;
}
}
bool Robot::add(Level level, int spaceIndex) {
if (!hasBoxInHand) return false;
else if (!level.avliable_space[spaceIndex]) return false;
else if (spaceIndex >= level.space_num) return false;
else {
boxInHand += level.space[spaceIndex];
return true;
}
}
bool Robot::sub(Level level, int spaceIndex) {
if (!hasBoxInHand) return false;
else if (!level.avliable_space[spaceIndex]) return false;
else if (spaceIndex >= level.space_num) return false;
else {
boxInHand -= level.space[spaceIndex];
return true;
}
}
bool Robot::copyto(Level* level, int spaceIndex) {
if (!hasBoxInHand) return false;
else if (spaceIndex >= level->space_num) return false;
else {
level->avliable_space[spaceIndex] = true;
level->space[spaceIndex] = boxInHand;
return true;
//hasBoxInHand = false;因为是将手中积木复制到目标空地,所以不用将hasBoxInHand设置为false
}
}
bool Robot::copyfrom(Level level, int spaceIndex) {
if (!level.avliable_space[spaceIndex]) return false;
else if (spaceIndex >= level.space_num) return false;
else {
hasBoxInHand = true;
boxInHand = level.space[spaceIndex];
return true;
}
}
int Robot::jump(Command command, int targetLine) {
if (targetLine <= command.getCounter() && targetLine >= 1) {
std::cout << "jump to Line " << targetLine << std::endl;
return targetLine - 1;
}
else return -1;
}
int Robot::jumpifzero(Command command, int targetLine) {
if (targetLine <= command.getCounter() && targetLine >= 1 && hasBoxInHand) {
if (0 == boxInHand) return targetLine - 1;
else return -2;
}
else return - 1;
}
int Robot::executeCommmands(Level* level, Command command){//完成对指令集在本关是否可用的校验 并执行
int i = 0;
bool flag = true;
while (i < command.getCounter() && flag) {
std::cout << "i=" << i << std::endl;
switch (command.collection[i][0])
{
case 0: {
int temp = inbox(level);
if (!level->avliable_command[0] || 0 == temp) return i;
else if (-1 == temp) flag = false;
std::cout << "inbox" << std::endl;
break;
}
case 1:
if (!level->avliable_command[1] || !outbox(level)) return i;
std::cout << "outbox" << std::endl;
break;
case 2:
if (!level->avliable_command[2] || !add(*level, command.collection[i][1])) return i;
std::cout << "add" << std::endl;
break;
case 3:
if (!level->avliable_command[3] || !sub(*level, command.collection[i][1])) return i;
std::cout << "sub" << std::endl;
break;
case 4:
if (!level->avliable_command[4] || !copyto(level, command.collection[i][1])) return i;
std::cout << "copyto" << std::endl;
break;
case 5:
if (!level->avliable_command[5] || !copyfrom(*level, command.collection[i][1])) return i;
std::cout << "copyfrom" << std::endl;
break;
case 6: {
int temp = jump(command, command.collection[i][1]);
if (-1 == temp || !level->avliable_command[6]) return i;
else i = temp - 1;
std::cout << "jump" << std::endl;
break;
}
case 7: {
int temp = jumpifzero(command, command.collection[i][1]);
if (-1 == temp || !level->avliable_command[7]) return i;
else if (-2 != temp) i = temp - 1;
std::cout << "jumpifzero" << std::endl;
break;
}
default:
break;
}
++i;
}
return -1;
}
int main() {
int level_seq = 0;
std::cin >> level_seq;
Level level;
for (int i = 0; i < 100; ++i) level.inputs[i] = level.outputs[i] = level.std_outputs[i] = 0;
for (int i = 0; i < 8; ++i) level.avliable_command[i] = level.space[i] = level.avliable_space[i] = 0;
level.input_index = level.output_index = 0;
if (1 == level_seq) {//第1关
std::cout << "level 1!" << std::endl;
level.input_num = 2;
level.inputs[0] = 1;
level.inputs[1] = 2;
level.space_num = 0;
level.std_outputs[0] = 1;
level.std_outputs[1] = 2;
level.avliable_command[0] = level.avliable_command[1] = true;
}
else if (2 == level_seq) {//第2关
std::cout << "level 2!" << std::endl;
level.input_num = 8;
level.inputs[0] = 3;
level.inputs[1] = 9;
level.inputs[2] = 5;
level.inputs[3] = 1;
level.inputs[4] = -2;
level.inputs[5] = -2;
level.inputs[6] = 9;
level.inputs[7] = -9;
level.space_num = 3;
level.std_outputs[0] = -6;
level.std_outputs[1] = 6;
level.std_outputs[2] = 4;
level.std_outputs[3] = -4;
level.std_outputs[4] = 0;
level.std_outputs[5] = 0;
level.std_outputs[6] = 18;
level.std_outputs[7] = -18;
for (int i = 0; i < 8; ++i) level.avliable_command[i] = true;
}
else if (3 == level_seq) {//第3关
std::cout << "level 3!" << std::endl;
level.input_num = 8;
level.inputs[0] = 6;
level.inputs[1] = 2;
level.inputs[2] = 7;
level.inputs[3] = 7;
level.inputs[4] = -9;
level.inputs[5] = 3;
level.inputs[6] = -3;
level.inputs[7] = -3;
level.space_num = 3;
level.std_outputs[0] = 7;
level.std_outputs[1] = -3;
for (int i = 0; i < 8; ++i) level.avliable_command[i] = true;
}
else if (4 == level_seq) {//自定义关卡
//
}
else {//处理非法输入
std::cout << "error level_seq" << std::endl;
return 1;
}
std::string filePath = "C:\\Users\\li_ha\\Desktop\\command.txt";
//std::cin >> filePath;
char temp[256] = { 0 };
strcpy_s(temp, filePath.c_str());
std::ifstream commands(temp);
char buffer[256] = { 0 };
Command* command = new Command();
if (commands.is_open()) {
int time = 0;
while (!commands.eof()) {//逐行对指令集文件进行读取并处理
commands.getline(buffer, 100);
//std::cout << buffer << std::endl;
if (time != 0) {
bool f = command->add_command(buffer);
if (!f) {
std::cout << "“Error on instruction " << time + 1 << std::endl;
return 3;
}
}
else {
int length = 0;
while (buffer[length] != 0) ++length;
std::cout << "length=" << length << std::endl;
int c = 0;
for (int i = length - 1; i >= 0; i--) {
c += (int(buffer[i]) - 48) * pow(10, length - 1 - i);
}
std::cout << "c=" << c << std::endl;
command->setCounter(c);
}
time++;
}
}
else {//错误处理
std::cout << "noooooooo" << std::endl;
return 2;
}
commands.close();
std::cout << "...................................." << std::endl << command->getCounter() << std::endl;
for (int i = 0; i < command->getCounter(); ++i) {
std::cout << command->collection[i][0] << ' ' << command->collection[i][1] << std::endl;
}
std::cout << "...................................." << std::endl;
Robot* robot = new Robot();
int result = robot->executeCommmands(&level, *command);
if (-1 != result) {
std::cout << "Error on instruction " << result + 1 << std::endl;
return 3;
}
std::cout << "inbox outbox standard answer" << std::endl;
for (int i = 0; i < level.output_index; ++i) {
std::cout << level.inputs[i] << ' ' << level.outputs[i] << ' ' << level.std_outputs[i] << std::endl;
}
if (level.pass()) std::cout << "Success" << std::endl;
else std::cout << "Fail" << std::endl;
return 0;
}