C++#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
int step, minnstep = 29;
char mapp[7][7] = {
{'x', 'x', 'x', 'x', 'x', 'x', 'x'},
{'x', '.', '.', '.', 's', '.', 'x'},
{'x', 's', '|', '.', '.', '.', 'x'},
{'x', '.', '|', '.', '|', '.', 'x'},
{'x', '.', '.', '.', '.', '.', 'x'},
{'x', '.', '.', '.', '|', '.', 'x'},
{'x', 'x', 'x', 'x', 'x', 'x', 'x'},
};
struct node {
int x, y;
} now, e1, e2, s1, s2;
map<char, int>dx;
map<char, int>dy;
bool vis[7][7] = {
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 1, 0, 0},
{0, 1, 0, 1, 1, 0, 0},
{0, 1, 0, 1, 0, 0, 0},
{0, 1, 1, 1, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0},
};
bool chk (int x, int y) {
if (mapp[x][y] == 'x' || mapp[x][y] == '|') return 0;
return 1;
}
bool victory() {
if (e1.x == s1.x && e1.y == s1.y && e2.x == s2.x && e2.y == s2.y) return 1;
if (e2.x == s1.x && e2.y == s1.y && e1.x == s2.x && e1.y == s2.y) return 1;
return 0;
}
void print() {
cout << "step: " << step << endl;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
if (i == now.x && j == now.y) cout << "o";
else if ( (i == e1.x && j == e1.y) || (i == e2.x && j == e2.y) ) cout << "e";
else cout << mapp[i][j];
cout << " ";
}
cout << endl << endl;
}
}
void init() {
system ("mode con lines=40 cols=150");
system ("color E1");
cout << "First let's know about the rules of the game.\n\n";
cout << " \".\"is space.\"|\"is hurdle.\"x\"is wall.\"o\"is you.\"e\"is box.\"s\"is target.\n\n";
cout << "\"wasd\" is standing for up,down,left and right.\n\n";
cout << "You target is moving the box to the \"s\".\n\n";
cout << "If you can't do it,we will tell you that you can't.\n\n";
cout << "Do you understand?\n\n";
system ("pause");
//system("mode con lines=10 cols=20");
}
void play() {
system ("cls");
now = (node) {2, 5};
e1 = (node) {3, 3};
e2 = (node) {2, 4};
s1 = (node) {2, 1};
s2 = (node) {1, 4};
print();
dx['a'] = 0;
dx['d'] = 0;
dx['w'] = -1;
dx['s'] = 1;
dy['w'] = 0;
dy['s'] = 0;
dy['a'] = -1;
dy['d'] = 1;
while (!victory() ) {
if (vis[e1.x][e1.y] == 0 || vis[e2.x][e2.y] == 0) {
char op;
system ("cls");
cout << "You lose!" << endl;
cout << "You can't get to the 's'." << endl;
cout << "Do you want to play again?" << endl;
cin >> op;
if (op == 'N') return;
else {
system ("cls");
now = (node) {2, 5};
e1 = (node) {3, 3};
e2 = (node) {2, 4};
s1 = (node) {2, 1};
s2 = (node) {1, 4};
print();
continue;
}
return;
}
char op;
int tx, ty;
while (kbhit() == 0) {
if (kbhit != 0) {
op = (char) getch();
break;
}
}
tx = now.x + dx[op];
ty = now.y + dy[op];
system ("cls");
if (op == 'r') {
now = (node) {2, 5};
e1 = (node) {3, 3};
e2 = (node) {2, 4};
s1 = (node) {2, 1};
s2 = (node) {1, 4};
print();
continue;
}
if (op == 'n') return;
if (!chk (tx, ty) ) {
print();
continue;
}
if ( (tx != e1.x || ty != e1.y) && (tx != e2.x || ty != e2.y) ) {
now = (node) {tx, ty};
step++;
print();
continue;
}
if (tx == e1.x && ty == e1.y) {
int nx = e1.x + dx[op], ny = e1.y + dy[op];
if (nx == e2.x && ny == e2.y) {
nx = e2.x + dx[op];
ny = e2.y + dy[op];
if (!chk (nx, ny) ) {
print();
continue;
}
e1.x += dx[op];
e1.y += dy[op];
e2.x += dx[op];
e2.y += dy[op];
now = (node) {tx, ty};
step++;
print();
continue;
} else if (!chk (nx, ny) ) {
print();
continue;
}
e1.x += dx[op];
e1.y += dy[op];
now = (node) {tx, ty};
step++;
print();
} else if (tx == e2.x && ty == e2.y) {
int nx = e2.x + dx[op], ny = e2.y + dy[op];
if (nx == e1.x && ny == e1.y) {
nx = e1.x + dx[op];
ny = e1.y + dy[op];
if (!chk (nx, ny) ) {
print();
continue;
}
e1.x += dx[op];
e1.y += dy[op];
e2.x += dx[op];
e2.y += dy[op];
now = (node) {tx, ty};
step++;
print();
continue;
} else if (!chk (nx, ny) ) {
print();
continue;
}
e2.x += dx[op];
e2.y += dy[op];
now = (node) {tx, ty};
step++;
print();
}
}
system ("cls");
cout << "You win!" << endl;
cout << "Your step: " << step << endl;
cout << "min step: " << minnstep << endl;
if (step == minnstep) cout << "Excellent!";
else if (step - minnstep <= 4) cout << "Good!";
else if (step - minnstep <= 8) cout << "Common.";
else if (step - minnstep <= 12) cout << "Passed.";
else cout << "Failed.";
}
int main() {
init();
play();
return 0;
}
C++#include<bits/stdc++.h>
using namespace std;
const int n = 10;
int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy[8] = {1, -1, 1, -1, 0, 1, -1, 0};
int nx[4] = {0, 0, 1, -1};
int ny[4] = {1, -1, 0, 0};
char mapp[10][10];
int num[10][10];
bool show[10][10];
bool chk (int x, int y) {
if (x < 0 || x >= n) return 0;
if (y < 0 || y >= n) return 0;
return 1;
}
bool victory() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (mapp[i][j] == '*' && num[i][j] != -1) return 0;
if (mapp[i][j] != '.' && num[i][j] == -1) return 0;
if (mapp[i][j] != '*' && !show[i][j]) return 0;
}
}
return 1;
}
void shengcheng() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int t = rand() % 4;
if (t == 0) num[i][j] = -1;
mapp[i][j] = '.';
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int cnt = 0;
if (num[i][j] == -1) continue;
for (int k = 0; k < 8; k++) {
int tx = i + dx[k], ty = j + dy[k];
if (!chk (tx, ty) ) continue;
if (num[tx][ty] == -1) cnt++;
}
num[i][j] = cnt;
}
}
return;
}
void print() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (show[i][j]) cout << num[i][j] << " ";
else cout << mapp[i][j] << " ";
}
cout << endl;
}
return;
}
void flood (int x, int y) {
show[x][y] = true;
mapp[x][y] = '.';
for (int i = 0; i < 4; i++) {
int tx = x + nx[i];
int ty = y + ny[i];
if (!chk (tx, ty) ) continue;
if (num[tx][ty] == -1) return;
}
for (int i = 0; i < 4; i++) {
int tx = x + nx[i];
int ty = y + ny[i];
if (!chk (tx, ty) ) continue;
if (num[tx][ty] == -1) return;
if (show[tx][ty]) continue;
flood (tx, ty);
}
return;
}
void play() {
while (1) {
int x, y, op;
cin >> x >> y >> op;
x--;
y--;
system ("cls");
if (!chk (x, y) ) {
print();
continue;
}
if (op == 1) {
flood (x, y);
if (num[x][y] == -1) {
cout << "You lose!";
break;
}
} else if (op == 2) mapp[x][y] = '*';
else if (op == 3) mapp[x][y] = '.';
else if (op == 4) {
for (int i = 0; i < 8; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if (!chk (tx, ty) ) continue;
if (show[tx][ty]) continue;
if (mapp[tx][ty] == '*') continue;
if (num[x][y] == -1) {
cout << "You lose!";
break;
}
show[tx][ty] = true;
flood (tx, ty);
}
}
if (victory() ) {
cout << "You win!";
break;
}
print();
}
}
int main() {
srand ( (int) time (0) );
shengcheng();
print();
play();
return 0;
}