本文實(shí)例為大家分享了C語(yǔ)言實(shí)現(xiàn)拼圖游戲的具體代碼,供大家參考,具體內(nèi)容如下
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
|
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<conio.h> int step= 0 ; void map() //游戲菜單函數(shù)。 { printf( "▇▇▇▇▇▇▇▇▇▇▇▇▇▇\n" ); printf( "█ 拼 圖 游 戲 █\n" ); printf( "█ 1. 3×3 █\n" ); printf( "█ 2. 4×4 █\n" ); printf( "█ 3. 5×5 █\n" ); printf( "█ 4. 退出 █\n" ); printf( "▇▇▇▇▇▇▇▇▇▇▇▇▇▇\n" ); } void fun( int *a, int t, int m, int r) { int i; int g; for (i= 0 ;i<m;i++) { if (i%r==(r- 1 )&&(i+t)%r== 0 ) //讓數(shù)組不能跨行轉(zhuǎn)換。 continue ; if (i%r== 0 &&(i+t)%r==(r- 1 )) continue ; if (a[i+t]== 0 && (i+t)>= 0 && (i+t)<m) { g=a[i]; a[i]=a[i+t]; a[i+t]=g; step++; return ; } } } void move( int *a, int m, int r) { //上下左右移動(dòng)函數(shù)。 char ch; int i,j; while (ch=getch()) { system( "cls" ); if (ch== 'w' ) { i=-r; fun(a,i,m,r); } else if (ch== 's' ) { i=r; fun(a,i,m,r); } else if (ch== 'a' ) { i=- 1 ; fun(a,i,m,r); } else if (ch== 'd' ) { i= 1 ; fun(a,i,m,r); } else if (ch== '4' ) { printf( "您已退出游戲,請(qǐng)下次再玩!\n" ); exit( 0 ); } for (i= 0 ;i<m;i++) { if (a[i]== 0 ) printf( " " ); else printf( "%2d " ,a[i]); if ((i+ 1 )%r== 0 ) putchar( 10 ); } printf( "共走:%d步!\n" ,step); } } void map_x( int m, int r) { int i= 1 ,j,a[ 25 ],n; srand(time(NULL)); printf( "拼圖游戲開(kāi)始了:\n" ); while (i<m) { n= 1 +rand()%(m- 1 ); for (j= 1 ;j<i;j++) { if (n==a[j]) break ; } if (j==i) { a[i]=n; i++; } } a[ 0 ]= 0 ; for (i= 0 ;i<m;i++) { if (a[i]== 0 ) printf( " " ); else printf( "%2d " ,a[i]); if ((i+ 1 )%r== 0 ) putchar( 10 ); } printf( "共走:%d步!\n" ,step); move(a,m,r); } int main() { int i,n; char ch; map(); //顯示菜單。 printf( "請(qǐng)選擇所玩游戲的級(jí)別:\n" ); ch=getch(); while (ch!= '1' && ch!= '2' && ch!= '3' &&ch!= '4' ) { printf( "輸入有誤,請(qǐng)重新輸入:\n" ); ch=getch(); } switch (ch) { case '1' : map_x( 9 , 3 ); break ; case '2' : map_x( 16 , 4 ); break ; case '3' : map_x( 25 , 5 ); break ; case '4' : printf( "您已退出游戲,請(qǐng)下次再玩!\n" ); exit( 0 ); break ; } return 0 ; } //對(duì)于確定的循環(huán)一般用for;而不確定的循環(huán)則用while. |
小編收藏的另一段拼圖游戲代碼,分享給大家
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
|
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<time.h> int a[25],sum=0,flag=0,i=0,j; void Menu() //菜單 { puts ( "********************" ); puts ( "* 1 3x3 *" ); puts ( "* 2 4x4 *" ); puts ( "* 3 5x5 *" ); puts ( "* 4 退出 *" ); puts ( "********************" ); } int Size() //圖大小 { int b,h; puts ( "選擇:" ); scanf ( "%d" ,&b); system ( "cls" ); if (b==1) h=3; else if (b==2) h=4; else if (b==3) h=5; else if (b==4) exit (1); return h; //h:圖長(zhǎng)和寬 } void Show( int a[], int n, int sum, int h) //輸出 { system ( "cls" ); for (i=0;i<n;i++) { if (a[i]==0) printf ( "%3s" , "_" ); else printf ( "%3d" ,a[i]); if ((i+1)%h==0) puts ( "" ); } printf ( "\n\n共移動(dòng)了%d次!\n" ,sum); } int suiji( int h) //隨機(jī)分布 { int n,k; n=h*h; srand ( time (NULL)); while (i<n-1) //出圖 { a[n-1]=0; k= rand ()%(n-1)+1; for (j=0;j<i;j++) if (a[j]==k) break ; if (j==i) { a[j]=k; i++; } } Show(a,n,sum,h); //輸出 return n; } int Move( int n, int h) //移動(dòng) { int y,temp; char f; y=n-1; while (1) { f=getch(); if (f== 'w' ||f== 'W' ) { for (i=0;i<h;i++) if (y==i) flag=1; if (flag!=1) { temp=a[y-h]; a[y-h]=a[y]; a[y]=temp; y=y-h; sum++; } } else if (f== 's' ||f== 'S' ) { for (i=0;i<h;i++) if (y==h*(h-1)+i) flag=2; if (flag!=2) { temp=a[y+h]; a[y+h]=a[y]; a[y]=temp; y=y+h; sum++; } } else if (f== 'a' ||f== 'A' ) { for (i=0;i<h;i++) if (y==h*i) flag=3; if (flag!=3) { temp=a[y-1]; a[y-1]=a[y]; a[y]=temp; y=y-1; sum++; } } else if (f== 'd' ||f== 'D' ) { for (i=0;i<h;i++) if (y==h*i+(h-1)) flag=4; if (flag!=4) { temp=a[y+1]; a[y+1]=a[y]; a[y]=temp; y=y+1; sum++; } } Show(a,n,sum,h); //輸出 /*for(i=1,j=0;i<n;i++) { if(a[j]==i) j++; } if(j==n-1) { puts("You win!"); exit(1); }*/ } //移動(dòng) } void Exit( int a[], int n) { for (i=1,j=0;i<n;i++) { if (a[j]==i) j++; } if (j==n-1) { puts ( "You win!" ); exit (1); } } int main() { int n,h,sum=0,*a; Menu(); //菜單 while (1) { h=Size(); //圖大小 n=suiji(h); //隨機(jī)分布 Move(n,h); //移動(dòng) Exit(a,n); } return 0; } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/u011131296/article/details/17286191