扫雷程序源代码
#include #include
#include
#include
#include
#include
#include
#include ???? #pragma comment( lib , "WINMM.LIB" )
int life_number ;
int dete_number ;
int time_left ;
int mine_left ;
int total_point ;
int mine[ 24 ][ 16 ] ; char name[ 15 ] ;
char str[ 10 ] ;
void initial( void ) ; void setup( void ) ;
void fail( void ) ;
void succeed( void ) ; void clean( int ) ;
void assgn( int , int , int ) ; void number_string( int , char* ) ;
int size_string( char* ) ; void detect( int , int ) ; int check_over() ;
void show( int , int ) ; void clear_safe( int , int ) ; int update_rank( int , int , const char* ) ;
void show_rank( void ) ;
int main( void )
{
printf( "PRINT YOUR NAME(Don't include "space"):\n" ) ;
scanf( "%s" , name ) ;
MOUSEMSG m ;
initial() ;
while ( 1 )
{
setup() ;
mine_left = 50 ;
life_number = 1 ;
total_point = 0 ;
dete_number = 0 ;
time_left = 512 ;
assgn( mine_left , 20 , 20 ) ;
int place = 0 ;
unsigned int length[ 5 ] ;
for ( int pos = 0 ; pos < 5 ; pos++ )
length[ pos ] = 0 ;
int start = 0 ;
int pause = 0 ;
int stop = 0 ;
int ctrl = 0 ;
int rank = 0 ;
long time_start , time_now ;
while ( life_number != 0 )
{
if ( 1 == start && 0 == pause )
{
setfillstyle( WHITE ) ;
time_now = time( 0 ) ;
time_left += time_start - time_now ;
time_start = time_now ;
number_string( time_left , str ) ;
setcolor( DARKGRAY ) ;
setbkcolor( WHITE ) ;
if ( length[ 0 ] != size_string( str ) )
bar( 10 , 300 , 120 , 319 ) ;
outtextxy( 50 , 300 , str ) ;
length[ 0 ] = size_string( str ) ;
number_string( life_number , str ) ;
if ( length[ 1 ] != size_string( str ) )
bar( 10 , 220 , 120 , 239 ) ;
outtextxy( 50 , 220 , str ) ;
length[ 1 ] = size_string( str ) ;
number_string( dete_number , str ) ;
if ( length[ 2 ] != size_string( str ) )
bar( 10 , 260 , 120 , 279 ) ;
outtextxy( 50 , 260 , str ) ;
length[ 2 ] = size_string( str ) ;
number_string( total_point , str ) ;
if ( length[ 3 ] != size_string( str ) )
bar( 10 , 340 , 120 , 359 ) ;
outtextxy( 50 , 340 , str ) ;
length[ 3 ] = size_string( str ) ;
number_string( mine_left , str ) ;
if ( length[ 4 ] != size_string( str ) )
bar( 10 , 180 , 120 , 199 ) ;
outtextxy( 50 , 180 , str ) ;
length[ 4 ] = size_string( str ) ;
if ( time_left <= 0 )
{
fail() ;
continue ;
}
}
else if ( 0 == pause && 0 == rank )
{
rank = 1 ;
show_rank() ;
}
Sleep( 5 ) ;
if ( MouseHit() )
m = GetMouseMsg() ;
switch ( m.uMsg )
{
case WM_LBUTTONDOWN :
setfillstyle( DARKGRAY ) ;
setcolor( BROWN ) ;
setbkcolor( DARKGRAY ) ;
if ( m.x >= 20 && m.x <= 110 && m.y >= 10 && m.y <= 40 )
{
bar( 21 , 11 , 109 , 39 ) ;
outtextxy( 50 , 17 , "START" ) ;
if ( 0 == start )
{
start = 1 ;
setfillstyle( WHITE ) ;
bar( 131 , 1 , 649 , 359 ) ;
setcolor( BLACK ) ;
for ( int a = 0 ; a < 16 ; a++ )
for ( int b = 0 ; b < 24 ; b++ )
rectangle( 150 + b * 20 , 20 + a * 20 , 150
+ ( b + 1 ) * 20 , 20 + ( a + 1 ) * 20 ) ;
rank = 0 ;
pause = 0 ;
time_start = time( 0 ) ;
}
else if ( pause )
{
start = 1 ;
rank = 0 ;
pause = 0 ;
time_start = time( 0 ) ;
}
}
else if ( m.x >= 20 && m.x <= 110 && m.y >= 60 && m.y <=
90 )
{
bar( 21 , 61 , 109 , 89 ) ;
outtextxy( 50 , 67 , "PAUSE" ) ;
if ( 0 == pause )
{
pause = 1 ;
}
}
else if ( m.x >= 20 && m.x <= 110 && m.y >= 110 && m.y
<= 140 )
{
bar( 21 , 111 , 109 , 139 ) ;
outtextxy( 50 , 117 , "STOP") ;
if ( 0 == stop )
{
stop = 1 ;
}
}
else if ( m.x >= 150 && m.x <= 630 && m.y >=20 && m.y <=
340 && start == 1 && pause == 0 )
{
int temp = ( m.x - 150 ) / 20 + ( m.y - 20 ) / 20 *
24 ;
int y = temp / 24 ;
int x = temp % 24 ;
if ( mine[ x ][ y ] < 12 )
{
if ( mine[ x ][ y ] != 9 )
{
clear_safe( x , y ) ;
if ( check_over() == 1 )
{
succeed() ;
continue ;
}
}
else
{
mine[ x ][ y ] += 12 ;
show( x , y ) ;
life_number-- ;
mine_left-- ;
if ( life_number == 0 )
{
fail() ;
continue ;
}
if ( check_over() == 1 )
{
succeed() ;
continue ;
}
}
PlaySound( TEXT( "1.wav" ) , NULL , SND_FILENAME
| SND_ASYNC ) ;
}
}
break ;
case WM_LBUTTONUP :
setfillstyle( WHITE ) ;
setcolor( DARKGRAY ) ;
setbkcolor( WHITE ) ;
if ( m.x >= 20 && m.x <= 110 && m.y >= 10 && m.y <= 40 )
{
bar( 21 , 11 , 109 , 39 ) ;
outtextxy( 50 , 17 , "START" ) ;
}
else if ( m.x >= 20 && m.x <= 110 && m.y >= 60 && m.y <=
90 )
{
bar( 21 , 61 , 109 , 89 ) ;
outtextxy( 50 , 67 , "PAUSE" ) ;
}
else if ( m.x >= 20 && m.x <= 110 && m.y >= 110 && m.y
<= 140 )
{
bar( 21 , 111 , 109 , 139 ) ;
outtextxy( 50 , 117 , "STOP") ;
if ( start == 1 )
{
life_number = 0 ;
continue ;
}
else
stop = 0 ;
}
break ;
case WM_RBUTTONDOWN :
if ( 0 == start | 1 == pause | 1 == ctrl )
break ;
if ( m.x >= 150 && m.x <= 630 && m.y >=20 && m.y <= 340 )
{
int temp = ( m.x - 150 ) / 20 + ( m.y - 20 ) / 20 *
24 ;
int y = temp / 24 ;
int x = temp % 24 ;
detect( x , y ) ;
if ( check_over() == 1 )
{
succeed() ;
continue ;
}
}
ctrl = 1 ;
break ;
case WM_RBUTTONUP :
ctrl = 0 ;
break ;
default:
break ;
}
}
if ( !stop )
Sleep( 1000 ) ;
}
return 0 ;
}
void initial( void ) {
int graphdrive = VGA , graphmode = VGAHI ;
initgraph( &graphdrive , &graphmode , "" ) ;
initgraph( 650 , 360 ) ;
setup() ;
}
void setup( void )
{
setfillstyle( WHITE ) ;
setbkcolor( WHITE ) ;
bar( 0 , 0 , 650 , 360 ) ;
setcolor( BLACK ) ;
rectangle( 0 , 0 , 650 , 360 ) ;
rectangle( 0 , 0 , 130 , 360 ) ;
rectangle( 0 , 0 , 130 , 150 ) ;
rectangle( 20 , 10 , 110 , 40 ) ;
rectangle( 20 , 60 , 110 , 90 ) ;
rectangle( 20 , 110 , 110 , 140 ) ;
setcolor( DARKGRAY ) ;
outtextxy( 50 , 17 , "START" ) ;
outtextxy( 50 , 67 , "PAUSE" ) ;
outtextxy( 50 , 117 , "STOP") ;
outtextxy( 20 , 160 , "MINE LEFT?º" ) ;
outtextxy( 20 , 200 , "LIFE?º" ) ;
outtextxy( 20 , 240 , "DETE?º" ) ;
outtextxy( 20 , 280 , "TIME LEFT?º" ) ;
outtextxy( 20 , 320 , "TOTAL?º" ) ; }
void assgn( int number_mine , int number_life , int number_dete )
{
srand( time( 0 ) ) ;
int temp ;
int i , j ;
for ( i = 0 ; i < 24 ; i++ )
for ( j = 0 ; j < 16 ; j++ )
mine[ i ][ j ] = 0 ;
temp = number_mine ;
while ( temp != 0 )
{
i = rand() % 24 ;
j = rand() % 16 ;
if ( mine[ i ][ j ] == 0 )
{
mine[ i ][ j ] = 9 ;
temp-- ;
}
}
temp = number_life ;
while ( temp != 0 )
{
i = rand() % 24 ;
j = rand() % 16 ;
if ( mine[ i ][ j ] == 0 )
{
mine[ i ][ j ] = 10 ;
temp-- ;
}
}
temp = number_dete ;
while ( temp != 0 )
{
i = rand() % 24 ;
j = rand() % 16 ;
if ( mine[ i ][ j ] == 0 )
{
mine[ i ][ j ] = 11 ;
temp-- ;
}
}
for ( i = 0 ; i < 24 ; i++ )
for ( j = 0 ; j < 16 ; j++ )
if ( mine[ i ][ j ] == 0 )
{
int count = 0 ;
for ( int m = max( 0 , i - 1 ) ; m <= min( 23 , i + 1 ) ;
m++ )
for ( int n = max( 0 , j - 1 ) ; n <= min( 15 , j +
1 ) ; n++ )
if ( mine[ m ][ n ] == 9 )
count++ ;
mine[ i ][ j ] = count ;
}
}
int size_string( char*b ) {
int a ;
for ( a = 0 ; b[ a ] != '\0' ; a++ ) ;
return a ;
}
void number_string( int a , char*b )
{
if ( a == 0 )
{
b[ 0 ] = '0' ;
b[ 1 ] = '\0' ;
return ;
}
int count = 0 ;
int temp = a ;
while ( temp != 0 )
{
count++ ;
temp = temp / 10 ;
}
temp = a ;
b[ count ] = '\0' ;
while ( count != 0 )
{
b[ count - 1 ] = char( 48 + temp % 10 ) ;
count-- ;
temp = temp / 10 ;
}
}
void clear_safe( int x , int y )
{
if ( x >= 24 | x < 0 | y >= 16 | y < 0 )
return ;
else if ( mine[ x ][ y ] >= 12 )
return ;
else
{
total_point++ ;
mine[ x ][ y ] += 12 ;
show( x , y ) ;
if ( mine[ x ][ y ] == 22 )
{
life_number++ ;
}
if ( mine[ x ][ y ] == 23 )
{
dete_number++ ;
}
}
return ;
}
void show( int i , int j )
{
if ( mine[ i ][ j ] < 12 )
{
setfillstyle( WHITE ) ;
bar( 151 + i * 20 , 21 + j * 20 , 149 + ( i + 1 ) * 20 , 19 +
( j + 1 ) * 20 ) ;
}
else if ( mine[ i ][ j ] == 12 )
{
setfillstyle( GREEN ) ;
bar( 151 + i * 20 , 21 + j * 20 , 149 + ( i + 1 ) * 20 , 19 +
( j + 1 ) * 20 ) ;
}
else if ( mine[ i ][ j ] <= 20 )
{
setfillstyle( GREEN ) ;
bar( 151 + i * 20 , 21 + j * 20 , 149 + ( i + 1 ) * 20 , 19 +
( j + 1 ) * 20 ) ;
setcolor( DARKGRAY ) ;
setbkcolor( GREEN ) ;
outtextxy( 157 + i * 20 , 22 + j * 20 , char( 48 + mine[ i ][ j ]
- 12 ) ) ;
}
else if ( mine[ i ][ j ] == 21 )
{
setfillstyle( GREEN ) ;
bar( 151 + i * 20 , 21 + j * 20 , 149 + ( i + 1 ) * 20 , 19 +
( j + 1 ) * 20 ) ;
setcolor( LIGHTRED ) ;
setbkcolor( GREEN ) ;
outtextxy( 157 + i * 20 , 22 + j * 20 , 'X' ) ;
}
else if ( mine[ i ][ j ] == 22 )
{
setfillstyle( GREEN ) ;
bar( 151 + i * 20 , 21 + j * 20 , 149 + ( i + 1 ) * 20 , 19 +
( j + 1 ) * 20 ) ;
int count = 0 ;
for ( int m = max( 0 , i - 1 ) ; m <= min( 23 , i + 1 ) ; m++ )
for ( int n = max( 0 , j - 1 ) ; n <= min( 15 , j + 1 ) ; n++ )
{
if ( mine[ m ][ n ] == 9 )
count++ ;
else if ( mine[ m ][ n ] == 21 )
count++ ;
else if ( mine[ m ][ n ] == 24 )
count++ ;
else
continue ;
}
setbkcolor( GREEN ) ;
setcolor( BLUE ) ;
if ( count > 0 )
{
outtextxy( 153 + i * 20 , 22 + j * 20 , '$' ) ;
setcolor( DARKGRAY ) ;
outtextxy( 161 + i * 20 , 22 + j * 20 , char( 48 + count ) ) ;
}
else
{
outtextxy( 157 + i * 20 , 22 + j * 20 , '$' ) ;
}
}
else if ( mine[ i ][ j ] == 23 )
{
setfillstyle( GREEN ) ;
bar( 151 + i * 20 , 21 + j * 20 , 149 + ( i + 1 ) * 20 , 19 +
( j + 1 ) * 20 ) ;
int count = 0 ;
for ( int m = max( 0 , i - 1 ) ; m <= min( 23 , i + 1 ) ; m++ )
for ( int n = max( 0 , j - 1 ) ; n <= min( 15 , j + 1 ) ; n++ )
{
if ( mine[ m ][ n ] == 9 )
count++ ;
else if ( mine[ m ][ n ] == 21 )
count++ ;
else if ( mine[ m ][ n ] == 24 )
count++ ;
else
continue ;
}
setcolor( BLUE ) ;
setbkcolor( GREEN ) ;
if ( count > 0 )
{
outtextxy( 153 + i * 20 , 22 + j * 20 , '#' ) ;
setcolor( DARKGRAY ) ;
outtextxy( 161 + i * 20 , 22 + j * 20 , char( 48 + count ) ) ;
}
else
{
outtextxy( 157 + i * 20 , 22 + j * 20 , '#' ) ;
}
}
else if ( mine[ i ][ j ] == 24 )
{
setfillstyle( WHITE ) ;
bar( 151 + i * 20 , 21 + j * 20 , 149 + ( i + 1 ) * 20 , 19 +
( j + 1 ) * 20 ) ;
setcolor( RED ) ;
setbkcolor( WHITE ) ;
outtextxy( 157 + i * 20 , 22 + j * 20 , '!' ) ;
}
else if ( mine[ i ][ j ] >= 25 )
{
setfillstyle( WHITE ) ;
bar( 151 + i * 20 , 21 + j * 20 , 149 + ( i + 1 ) * 20 , 19 +
( j + 1 ) * 20 ) ;
setcolor( RED ) ;
setbkcolor( WHITE ) ;
outtextxy( 157 + i * 20 , 22 + j * 20 , '!' ) ;
}
}
int check_over()
{
if ( mine_left != 0 )
{
int count = 0 ;
for ( int i = 0 ; i < 24 ; i++ )
for ( int j = 0 ; j < 16 ; j++ )
{
if ( mine[ i ][ j ] < 12 )
count++ ;
}
if ( count == mine_left )
return 1 ;
}
else
{
int count = 0 ;
for ( int i = 0 ; i < 24 ; i++ )
for ( int j = 0 ; j < 16 ; j++ )
{
if ( mine[ i ][ j ] == 25 )
count++ ;
}
if ( count == 0 )
return 1 ;
}
return 0 ;
}
void detect( int x , int y ) {
if ( mine[ x ][ y ] >= 12 )
{
if ( mine[ x ][ y ] == 24 )
{
mine_left++ ;
mine[ x ][ y ] = 9 ;
show( x , y ) ;
}
else if ( mine[ x ][ y ] >= 25 )
{
mine_left++ ;
mine[ x ][ y ] -= 25 ;
show( x , y ) ;
}
return ;
}
if ( mine_left <= 0 )
return ;
if ( dete_number <= 0 )
{
if ( mine[ x ][ y ] == 9 )
mine[ x ][ y ] = 24 ;
else
mine[ x ][ y ] += 25 ;
mine_left-- ;
show( x , y ) ;
return ;
}
else
{
for ( int m = max( 0 , x - 1 ) ; m <= min( 23 , x + 1 ) ; m++ )
for ( int n = max( 0 , y - 1 ) ; n <= min( 15 , y + 1 ) ;
n++ )
{
if ( mine[ m ][ n ] == 9 && mine_left > 0 )
{
mine[ m ][ n ] = 24 ;
mine_left-- ;
show( m , n ) ;
}
else if ( mine[ m ][ n ] < 12 )
{
total_point++ ;
mine[ m ][ n ] += 12 ;
show( m , n ) ;
if ( mine[ m ][ n ] == 22 )
life_number++ ;
if ( mine[ m ][ n ] == 23 )
dete_number++ ;
}
}
dete_number-- ;
show( x , y ) ;
return ;
}
}
int update_rank( int a , int b , const char* name )
{
int rank[ 3 ][ 2 ] ;
char user[ 3 ][ 15 ] ;
int i , j , count ;
FILE* stream = NULL ;
stream = fopen( "rank.dat" , "r" ) ;
if ( stream != NULL )
{
i = 0 ;
fseek( stream , 0L , SEEK_SET ) ;
while ( fscanf( stream , "%s%d%d\n" , user[ i ] , &rank[ i ][ 0 ] ,
&rank[ i ][ 1 ] ) != EOF )
{
i++ ;
if ( i >= 3 )
break ;
}
fclose( stream ) ;
}
else
return 0 ;
count = i ;
for ( i = 0 ; i < count ; i++ )
if ( rank[ i ][ 0 ] > a )
break ;
if ( i == count )
return 0 ;
else
{
for ( j = count - 1 ; j > i ; j-- )
{
rank[ j ][ 0 ] = rank[ j - 1 ][ 0 ] ;
rank[ j ][ 1 ] = rank[ j - 1 ][ 1 ] ;
}
rank[ i ][ 0 ] = a ;
rank[ i ][ 1 ] = b ;
for ( j = 0 ; name[ j ] != '\0' ; j++ )
user[ i ][ j ] = name[ j ] ;
user[ i ][ j ] = '\0' ;
}
stream = NULL ;
stream = fopen( "rank.dat" , "w+" ) ;
if ( stream != NULL )
{
for ( i = 0 ; i < count ; i++ )
fprintf( stream , "%s%c%d%c%d\n" , user[ i ] , ' ' ,
rank[ i ][ 0 ] , ' ' , rank[ i ][ 1 ] ) ;
fclose( stream ) ;
return 1 ;
}
else
return 0 ;
}
void fail( void )
{
PlaySound( TEXT( "3.wav" ) , NULL , SND_FILENAME | SND_ASYNC ) ;
Sleep( 1000 ) ;
setfillstyle( WHITE ) ;
bar( 131 , 0 , 650 , 360 ) ;
setcolor( DARKGRAY ) ;
setbkcolor( WHITE ) ;
outtextxy( 350 , 167 , "TRY AGAIN!") ;
life_number = 0 ;
total_point = 0 ;
}
void succeed( void )
{
PlaySound( TEXT( "2.wav" ) , NULL , SND_FILENAME | SND_ASYNC ) ;
total_point += time_left ;
number_string( total_point , str ) ;
setfillstyle( WHITE ) ;
bar( 10 , 340 , 120 , 359 ) ;
setcolor( DARKGRAY ) ;
setbkcolor( WHITE ) ;
outtextxy( 50 , 340 , str ) ;
Sleep( 1000 ) ;
bar( 131 , 1 , 649 , 359 ) ;
life_number = 0 ;
if ( update_rank( 512 - time_left , total_point , name ) == 1 )
{
outtextxy( 350 , 147 , "WIN!") ;
outtextxy( 310 , 187 , "AMAZING!") ;
}
else
outtextxy( 350 , 167 , "WIN!") ; }
void show_rank( void )
{
setfillstyle( WHITE ) ;
bar( 131 , 1 , 649 , 359 ) ;
setcolor( BLACK ) ;
setbkcolor( WHITE ) ;
outtextxy( 360 , 97 , "TOTAL RANK" ) ;
setcolor( DARKGRAY ) ;
FILE* stream = NULL ;
stream = fopen( "rank.dat" , "r" ) ;
if ( stream != NULL )
{
int line = 1 ;
int a , b ;
char user[ 15 ] ;
fseek( stream , 0L , SEEK_SET ) ;
while ( fscanf( stream , "%s%d%d\n" , user , &a , &b ) != EOF )
{
str[ 0 ] = char( line + 48 ) ;
str[ 1 ] = '.' ;
str[ 2 ] = '\0' ;
outtextxy( 260 , 97 + line * 40 , str ) ;
outtextxy( 280 , 97 + line * 40 , "NAME:" ) ;
outtextxy( 320 , 97 + line * 40 , user ) ;
number_string( a , str ) ;
outtextxy( 400 , 97 + line * 40 , "TIME:" ) ;
number_string( a , str ) ;
outtextxy( 440 , 97 + line * 40 , str ) ;
outtextxy( 480 , 97 + line * 40 , "TOTAL:" ) ;
number_string( b , str ) ;
outtextxy( 520 , 97 + line * 40 , str ) ;
line++ ;
if ( line >=4 )
break ;
}
fclose( stream ) ;
}
else
outtextxy( 320 , 167 , "ÎÞ???øÐÐÓÎÏ?" ) ; }