C扫雷游戏

 
  
1 #include < stdio.h >
2 #include < stdlib.h >
3
4 #define CHEAKED 100
5 #define UNEXPLORE 1
6 #define EXPLORE -1
7 #define WIN 1
8 #define NOTWIN -1
9 #define TEST 1
10
11 int map[ 100 ][ 100 ];
12 int test_flag = 0 ;
13
14 void test( int n)
15 {
16 int i, j;
17 for (i = 1 ;i <= n;i ++ ) {
18 for (j = 1 ;j <= n;j ++ ) {
19 printf( " %4d " , map[i][j]);
20 }
21 printf( " \n " );
22 }
23 }
24
25 void set_map( int k, int n)
26 {
27 srand(time(NULL));
28 int i, t, j, q;
29 for (i = 0 ;i < k;){
30 t = rand() % (n * n) + 1 ;
31 if (map[(t + n - 1 ) / n][t % n + 1 ] == 0 ) {
32 map[(t + n - 1 ) / n][t % n + 1 ] = - 10 ;
33 i ++ ;
34 }
35 }
36
37 for (i = 1 ;i <= n;i ++ ) {
38 for (j = 1 ;j <= n;j ++ ) {
39 if (map[i][j] == - 10 ) {
40 for (q = i - 1 ;q <= i + 1 ;q ++ )
41 for (t = j - 1 ;t <= j + 1 ;t ++ ) {
42 if (map[q][t] != - 10 ) {
43 map[q][t] ++ ;
44 }
45 }
46 }
47 }
48 }
49 }
50
51 void reset_map( int n)
52 {
53 int i, j;
54 for (i = 0 ;i <= n + 1 ;i ++ ) {
55 for (j = 0 ;j <= n + 1 ;j ++ ) {
56 map[i][j] = 0 ;
57 }
58 }
59 }
60
61 int set_cheaked( int x, int y, int n)
62 {
63 int q, t;
64 map[x][y] += 100 ;
65 for (q = x - 1 ;q <= x + 1 ;q ++ )
66 for (t = y - 1 ;t <= y + 1 ;t ++ ) {
67 if (map[q][t] != - 10 && map[q][t] != 0 ) {
68 map[q][t] += 100 ;
69 } else if (map[q][t] == 0
70 && (q >= 1 && q <= n)
71 && (t >= 1 && t <= n)
72 && ( ! (q == x && t == y))) {
73 set_cheaked(q, t, n);
74 }
75 }
76 return 0 ;
77 }
78
79 int cheak_sweep( int x, int y, int n)
80 {
81 if (map[x][y] == - 10 ) {
82 return - 1 ;
83 } else {
84 if (map[x][y] == 0 )
85 set_cheaked(x, y, n);
86 else
87 map[x][y] += 100 ;
88 return map[x][y];
89 }
90 }
91
92 void show_map( int flag, int n)
93 {
94 int i, j;
95 for (i = 1 ;i <= n;i ++ ) {
96 for (j = 1 ;j <= n;j ++ ) {
97 if (map[i][j] >= 100 ) {
98 if ((map[i][j] % 100 ) == 0 )
99 printf( " " );
100 else
101 printf( " %d " ,map[i][j] % 100 );
102 } else if (flag == UNEXPLORE) {
103 printf( " X " );
104 } else if (flag == EXPLORE) {
105 if (map[i][j] == - 10 ) {
106 printf( " * " );
107 } else if (map[i][j] % 100 == 0 ){
108 printf( " " );
109 } else {
110 printf( " %d " ,map[i][j]);
111 }
112 }
113 }
114 printf( " \n " );
115 }
116 }
117
118 int is_win( int m, int n)
119 {
120 int i, j;
121 int cnt = 0 ;
122 for (i = 1 ;i <= n;i ++ )
123 for (j = 1 ;j <= n;j ++ ) {
124 if (map[i][j] >= 100 ) {
125 cnt ++ ;
126 }
127 }
128 if (cnt == n * n - m)
129 return WIN;
130 else
131 return NOTWIN;
132 }
133
134 void play( int k, int n)
135 {
136 int x, y;
137 reset_map(n);
138 set_map(k,n);
139 if (test_flag)
140 test(n);
141 else
142 show_map(UNEXPLORE,n);
143 while ( 1 ){
144 printf( " Please enter x,y: " );
145 scanf( " %d %d " , & x, & y);
146 if (x > n || x <= 0 || y > n || y <= 0 ){
147 printf( " Error coord, " );
148 } else {
149 if (cheak_sweep(x, y, n) == - 1 ) {
150 printf( " Oops! Boom!!!\n " );
151 show_map(EXPLORE, n);
152 break ;
153 } else if (is_win(k,n) == WIN) {
154 printf( " Congratulation!You win!\n " );
155 show_map(EXPLORE, n);
156 break ;
157 } else {
158 show_map(UNEXPLORE, n);
159 }
160 }
161 }
162 printf( " Game over!\n " );
163 }
164
165 void clear()
166 {
167 while (getchar() != ' \n ' );
168 }
169
170 int main( int args, char * argv[])
171 {
172 int n,k;
173 int select;
174 if (args > 1 && argv[ 1 ][ 0 ] == ' T ' )
175 test_flag = TEST;
176 while ( 1 ){
177 printf( " Set map size [1 100] : " );
178 scanf( " %d " , & n);
179 printf( " Please enter a number [1 - %d] : " ,n * n);
180 scanf( " %d " , & k);
181 if ((k > n || k <= 0 ) && (n < 0 || n > 100 )){
182 continue ;
183 }
184 play(k, n);
185 printf( " Do you want play again?[Y/n]\n " );
186 while ( 1 ) {
187 clear();
188 select = getchar();
189 if ((select == ' n ' ) || (select == ' N ' )){
190 printf( " Exiting Game...\n " );
192 return 0 ;
193 } else if (select == ' y ' || select == ' Y ' ) {
194 break ;
195 } else {
196 printf( " Error input,try again!\n " );
197 }
198 }
199 }
200 printf( " Exiting Game...\n " );
201 return 0 ;
202 }

转载于:https://www.cnblogs.com/moupeng/archive/2011/03/30/1999912.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值