代码如下:
C++# include <bits/stdc++.h>
# include <windows.h>
# include <conio.h>
using namespace std ;
int brd [5] [5] ;
void SlowSay ( string s )
{
for ( int i = 0 ; i < s . size ( ) ; i ++ )
{
cout << s [i] ;
if ( s [i] != ' ' )
{
Sleep ( 250 ) ;
}
}
cout << endl ;
Sleep ( 250 ) ;
return ;
}
void WordSay ( string s )
{
for ( int i = 0 ; i < s . size ( ) ; i ++ )
{
cout << s [i] ;
if ( s [i] == ' ' )
{
Sleep ( 250 ) ;
}
}
cout << endl ;
Sleep ( 250 ) ;
return ;
}
void LineSay ( string s )
{
cout << s << endl ;
Sleep ( 250 ) ;
return ;
}
void prt ( )
{
cout << "+----+----+----+----+" << endl ;
for ( int i = 1 ; i <= 4 ; i ++ )
{
cout << '|' ;
for ( int j = 1 ; j <= 4 ; j ++ )
{
if ( brd [i] [j] != 0 )
{
cout << ' ' << left << setw ( 3 ) << brd [i] [j] << '|' ;
}
else
{
cout << " |" ;
}
}
if ( i != 4 )
{
cout << endl << "|----+----+----+----|" << endl ;
}
else
{
cout << endl << "+----+----+----+----+" << endl ;
}
}
return ;
}
void up ( )
{
for ( int i = 2 ; i <= 4 ; i ++ )
{
for ( int j = 1 ; j <= 4 ; j ++ )
{
if ( brd [i - 1] [j] == 0 )
{
swap ( brd [i - 1] [j] , brd [i] [j] ) ;
return ;
}
}
}
return ;
}
void lft ( )
{
for ( int i = 1 ; i <= 4 ; i ++ )
{
for ( int j = 2 ; j <= 4 ; j ++ )
{
if ( brd [i] [j - 1] == 0 )
{
swap ( brd [i] [j - 1] , brd [i] [j] ) ;
return ;
}
}
}
return ;
}
void dwn ( )
{
for ( int i = 3 ; i >= 1 ; i -- )
{
for ( int j = 1 ; j <= 4 ; j ++ )
{
if ( brd [i + 1] [j] == 0 )
{
swap ( brd [i + 1] [j] , brd [i] [j] ) ;
return ;
}
}
}
return ;
}
void rgt ( )
{
for ( int i = 1 ; i <= 4 ; i ++ )
{
for ( int j = 3 ; j >= 1 ; j -- )
{
if ( brd [i] [j + 1] == 0 )
{
swap ( brd [i] [j + 1] , brd [i] [j] ) ;
return ;
}
}
}
return ;
}
void init ( )
{
int cnt = 0 , stp = rand ( ) % 20 + 20 ;
for ( int i = 1 ; i <= 4 ; i ++ )
{
for ( int j = 1 ; j <= 4 ; j ++ )
{
brd [i] [j] = cnt ;
cnt ++ ;
}
}
for ( int i = 1 ; i <= stp ; i ++ )
{
int c = rand ( ) % 4 ;
switch ( c )
{
case 1 :
{
up ( ) ;
}
case 2 :
{
lft ( ) ;
}
case 3 :
{
dwn ( ) ;
}
case 0 :
{
rgt ( ) ;
}
}
}
system ( "cls" ) ;
prt ( ) ;
return ;
}
void read ( )
{
ifstream fin ( "map.txt" ) ;
for ( int i = 1 ; i <= 4 ; i ++ )
{
for ( int j = 1 ; j <= 4 ; j ++ )
{
fin >> brd [i] [j] ;
}
}
return ;
}
void save ( )
{
ofstream fout ( "map.txt" ) ;
for ( int i = 1 ; i <= 4 ; i ++ )
{
for ( int j = 1 ; j <= 4 ; j ++ )
{
fout << brd [i] [j] << ' ' ;
}
fout << endl ;
}
return ;
}
void Srand ( )
{
SYSTEMTIME sys ;
GetLocalTime ( & sys ) ;
srand ( sys . wSecond * 1000 + sys . wMilliseconds ) ;
return ;
}
bool isw ( )
{
int cnt = 1 ;
for ( int i = 1 ; i <= 4 ; i ++ )
{
for ( int j = 1 ; j <= 4 ; j ++ )
{
if ( brd [i] [j] != cnt && cnt != 16 )
{
return false ;
}
cnt ++ ;
}
}
return true ;
}
void game ( )
{
bool flg = false ;
while ( true )
{
if ( isw ( ) )
{
system ( "cls" ) ;
SlowSay ( "You Win !!!" ) ;
break ;
}
char a = getch ( ) ;
while ( a != 'W' && a != 'A' && a != 'S' && a != 'D' && a != 'Q' )
{
a = getch ( ) ;
}
system ( "cls" ) ;
switch ( a )
{
case 'W' :
{
up ( ) ;
Sleep ( 125 ) ;
prt ( ) ;
break ;
}
case 'A' :
{
lft ( ) ;
Sleep ( 125 ) ;
prt ( ) ;
break ;
}
case 'S' :
{
dwn ( ) ;
Sleep ( 125 ) ;
prt ( ) ;
break ;
}
case 'D' :
{
rgt ( ) ;
Sleep ( 125 ) ;
prt ( ) ;
break ;
}
case 'Q' :
{
flg = true ;
}
}
save ( ) ;
if ( flg )
{
break ;
}
}
cout << endl ;
return ;
}
void select ( )
{
Srand ( ) ;
while ( true )
{
system ( "cls" ) ;
cout << "请选择功能" << endl ;
cout << "A 新地图" << endl ;
cout << "B 存档地图" << endl ;
char a ;
while ( a != 'A' && a != 'B' )
{
a = getch ( ) ;
}
if ( a == 'B' )
{
read ( ) ;
}
else
{
init ( ) ;
}
game ( ) ;
system ( "pause" ) ;
system ( "cls" ) ;
cout << " 继续玩?\nY 是 N 否" << endl ;
char c ;
while ( c != 'Y' && c != 'N' )
{
c = getch ( ) ;
}
if ( c == 'N' )
{
break ;
}
}
system ( "cls" ) ;
cout << "谢谢游玩" << endl ;
return ;
}
int main ( )
{
system ( "title 数字华容道 V1.0" ) ;
cout << "数字华容道 V1.0" << endl ;
cout << "By Michael0727" << endl ;
system ( "pause" ) ;
select ( ) ;
return 0 ;
}
问题: