-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.cpp
150 lines (150 loc) · 2.91 KB
/
1.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
#include<iostream>
#include<stdlib.h>
#include<cstdlib>
#include<ctime>
#include<conio.h>
using namespace std;
void begin() , change( int s ) , gm();
bool go() , gyj( int x , int y , char z );
int a[4][4];
int main(){
srand( (int) time (0) );
int time;
cout << "按任意键开始";
getch();
begin();
time = clock();
gm();
system("cls");
cout << "你赢了!\n用时" << ( clock() - time ) / 60000 << "分" << (( clock() - time ) / 1000 ) % 60 << "秒" << endl;;
system("pause");
}
void begin(){ //将15个数字打乱
char z;
int erb[15] , k , l = 0;
for( int i = 0 ; i < 14 ; i++ ){
erb[i] = 99;
}
for( int i = 0 ; i < 4 ; i++ ){
for( int j = 0 ; j < 4 ; j++ ){
if( i == 3 && j == 3 ){
break;
}
aaa:;
k = rand() % 16;
for( int x = 0 ; x < 16 ; x++ ){
if( k == erb[x] || k == 0 ){
goto aaa;
}
}
a[i][j] = k;
erb[l] = k;
l++;
}
}
}
void change( int s ){ //将数字工整地输出
char x;
switch( s ){
case 1 : cout << "1 ";break;
case 2 : cout << "2 ";break;
case 3 : cout << "3 ";break;
case 4 : cout << "4 ";break;
case 5 : cout << "5 ";break;
case 6 : cout << "6 ";break;
case 7 : cout << "7 ";break;
case 8 : cout << "8 ";break;
case 9 : cout << "9 ";break;
case 10 : cout << "10";break;
case 11 : cout << "11";break;
case 12 : cout << "12";break;
case 13 : cout << "13";break;
case 14 : cout << "14";break;
case 15 : cout << "15";break;
default: cout << " ";break;
}
}
void gm(){ //游戏主体部分
char z;
int k_i = 3 , k_j = 3 , t;
for( ; ; ){
if( go() == 0 ){
break;
}
system("cls");
for( int i = 0 ; i < 4 ; i++ ){
for( int j = 0 ; j < 4 ; j++ ){
change( a[i][j] );
cout << ' ';
}
cout << endl;
}
z = getch();
if( gyj( k_i , k_j , z ) == 1 ){}
else if( z == 'w' || z == 'W' ){
t = a[k_i - 1][k_j];
a[k_i - 1][k_j] = 0;
a[k_i][k_j] = t;
k_i--;
}
else if( z == 's' || z == 'S' ){
t = a[k_i + 1][k_j];
a[k_i + 1][k_j] = 0;
a[k_i][k_j] = t;
k_i++;
}
else if( z == 'a' || z == 'Z' ){
t = a[k_i][k_j - 1];
a[k_i][k_j - 1] = 0;
a[k_i][k_j] = t;
k_j--;
}
else if( z == 'd' || z == 'D' ){
t = a[k_i][k_j + 1];
a[k_i][k_j + 1] = 0;
a[k_i][k_j] = t;
k_j++;
}
}
}
bool go(){ //检查有没有赢,15个数字有没有归位
int z = 0 , k = 0;
for( int i = 0 ; i < 4 ; i++ ){
for( int j = 0 ; j < 4 ; j++ ){
if( i == 3 && j == 3){
break;
}
z++;
if( a[i][j] != z ){
k = 1;
goto bbb;
}
}
}
bbb:;
return k;
}
bool gyj( int x , int y , char z ){ //检测有没有数组越界
int t = 0;
if( x == 0 ){
if( z == 'w' || z == 'W' ){
t = 1;
}
}
if( x == 3 ){
if( z == 's'|| z == 'S' ){
t = 1;
}
}
if( y == 0 ){
if( z == 'a'|| z == 'A' ){
t = 1;
}
}
if( y == 3 ){
if( z == 'd'|| z == 'D' ){
t = 1;
}
}
return t;
}