1 #include <cstdlib>
2 #include <iostream>
3 #include <ctime>
4 #include <sstream>
5 using namespace std;
6 #define random(a,b) (rand() % (b-a+1))+ a
7 const int n = 3;
8 const int m = 3;
9 int main(int argc, char *argv[])
10 {
11 srand((int)time(0));
12 string map[n][m];
13 for(int i = 0;i < n;++i) {
14 for(int j = 0;j < m;++j) {
15 map[i][j] = " + ";
16 cout << map[i][j];
17 }
18 cout << endl;
19 }
20 int hardWork;
21 int impossible[1][2];
22 cout << "請輸入努力值:" << endl;
23 cin >> hardWork;
24 cout << "請輸入未知點的坐标:" << endl;
25 cin >> impossible[0][0] >> impossible[0][1];
26 map[impossible[0][0]][impossible[0][1]] = " x ";
27 for(int i = 0;i < n;++i) {
28 for(int j = 0;j < m;++j) {
29 cout << map[i][j];
30 }
31 cout << endl;
32 }
33 int try1[n][m] = {0};
34 try1[impossible[0][0]][impossible[0][1]] = -1;
35 int x, y;
36 int tempX, tempY;
37 bool first = false;
38 bool win = true;
39 stringstream ss;
40 int count = 0;
41 string temp[n][m] = {""};
42 while(true) {
43 if(!first) {
44 x = random(0,n-1);
45 y = random(0,m-1);
46 while((x == impossible[0][0])&&(y == impossible[0][1])) {
47 x = random(0,n-1);
48 y = random(0,m-1);
49 --hardWork;
50 }
51 first = true;
52 }
53 ++try1[x][y];
54 cout << ++count << ":" << endl;
55 for(int i = 0;i < n;++i) {
56 for(int j = 0;j < m;++j) {
57 if((x == impossible[0][0])&&(y == impossible[0][1])&&(try1[i][j] == 0)) {
58 cout << map[i][j];
59 }
60 else {
61 cout << try1[i][j];
62 }
63 }
64 cout << endl;
65 }
66 tempX = random(0,2) - 1;
67 x = x + tempX;
68 tempY = random(0,2) - 1;
69 y = y + tempY;
70 while(((x == impossible[0][0])&&(y == impossible[0][1]))||(x < 0 || x >= n || y < 0 || y >= m)) {
71 if((x == impossible[0][0])&&(y == impossible[0][1])) {
72 --hardWork;
73 }
74 x = x - tempX;
75 tempX = random(0,2) - 1;
76 x = x + tempX;
77 y = y - tempY;
78 tempY = random(0,2) - 1;
79 y = y + tempY;
80 }
81 cout << "努力值剩餘:" << hardWork << endl;
82 if(hardWork <= 0) {
83 cout << "失敗了" << endl;
84 break;
85 }
86 else {
87 for(int i = 0;i < n;++i) {
88 for(int j = 0;j < m;++j) {
89 if(try1[i][j] == 0) {
90 win = false;
91 break;
92 }
93 else {
94 win = true;
95 }
96 }
97 }
98 if(win) {
99 cout << "成功了" << endl;
100 break;
101 }
102 }
103 }
104 system("PAUSE");
105 return EXIT_SUCCESS;
106 }