我用C语言开发的“推箱子”源代码【寒假版】
V6.2.0.1__boxing_20080214_0242.txt
这是第一个版本放在txt里面的:我用C语言开发的“推箱子”源代码1。记得那时在纸上写算法和代码的时候,很冷。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Menu();
int GameStart();
int Input();
void DealWhitData();
void DealWhitData_YiDong();
void DealWhitData_ZhiGuan();
int Output();
void Output_ConnectionData();
void Output_PrintBox();
int WP[8][9] = { 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 0, 0, 0, 2, 2, 2, 2, 2,
2, 0, 2, 0, 2, 0, 0, 0, 2,
2, 0, 1, 0, 0, 0, 1, 0, 2,
2, 8, 8, 2, 1, 2, 1, 2, 2,
2, 8, 0, 1, 0, 0, 9, 2, 2,
2, 8, 8, 0, 0, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2 };
int ZG[24][27];
char SR[22];
int p, q;
int i, j, k, l, n;
int main()
{
Menu();
GameStart();
printf( "\n\n <<<<<<<<<<<<<<<<<<<<<GOOD-BYE>>>>>>>>>>>>>>>>>>>>>>>>>\n\n" );
system( "pause" );
return(0);
}
void Menu()
{
printf( "**************welocome To Boxing Game*********************\n" );
printf( " " );
printf( " " );
printf( " " );
printf( "f" );
printf( " " );
printf( "f" );
system( "pause" );
}
int GameStart()
{
p = 5, q = 6;
while ( 1 )
{
DealWhitData();
if ( Output() == 8 )
{
system( "cls" );
printf( " ***********Congratulations*******************\n" );
printf( " You Win The Game !!!\n\n" );
return(0);
}
if ( Input() == '0' )
break;
}
return(0);
}
/*----------------------------Begin---------------------------------------*/
int Input()
{
n = -1;
printf( " Input the fangxiang:" );
do
{
n++;
scanf( "%c", &SR[n] );
}
while ( SR[n] != '\n' );
/* SR=getchar(); */
if ( SR[n] == '0' )
return(0);
return(1);
}
void DealWhitData()
{
DealWhitData_YiDong();
DealWhitData_ZhiGuan();
}
void DealWhitData_YiDong()
{
for ( i = 0; i < n; i++ )
{
switch ( SR[i] )
{
case 'w':
{
switch ( WP[p - 1][q] )
{
case 0:
{
WP[p][q] = 0;
p--;
WP[p][q] = 9;
break;
}
case 8:
{
WP[p][q] = 0;
p--;
WP[p][q] = 9;
break;
}
case 1:
{
if ( WP[p - 2][q] == 0 || WP[p - 2][q] == 8 )
{
WP[p - 2][q] = 1;
WP[p][q] = 0;
p--;
WP[p][q] = 9;
}else if ( WP[p - 2][q] == 2 || WP[p - 1][q] == 1 )
{
;
}
break;
}
case 2:
{
break;
}
default:
break;
}
break;
}
case 's':
{
switch ( WP[p + 1][q] )
{
case 0:
{
WP[p][q] = 0;
p++;
WP[p][q] = 9;
break;
}
case 8:
{
WP[p][q] = 0;
p++;
WP[p][q] = 9;
break;
}
case 1:
{
if ( WP[p + 2][q] == 0 || WP[p + 2][q] == 8 )
{
WP[p + 2][q] = 1;
WP[p][q] = 0;
p++;
WP[p][q] = 9;
}else if ( WP[p + 2][q] == 2 || WP[p + 1][q] == 1 )
{
;
}
break;
}
case 2:
{
break;
}
default:
break;
}
break;
}
case 'a':
{
switch ( WP[p][q - 1] )
{
case 0:
{
WP[p][q] = 0;
q--;
WP[p][q] = 9;
break;
}
case 8:
{
WP[p][q] = 0;
q--;
WP[p][q] = 9;
break;
}
case 1:
{
if ( WP[p][q - 2] == 0 || WP[p][q - 2] == 8 )
{
WP[p][q - 2] = 1;
WP[p][q] = 0;
q--;
WP[p][q] = 9;
}else if ( WP[p][q - 2] == 2 || WP[p][q - 2] == 1 )
{
;
}
break;
}
case 2:
{
break;
}
default:
break;
}
break;
}
case 'd':
{
switch ( WP[p][q + 1] )
{
case 0:
{
WP[p][q] = 0;
q++;
WP[p][q] = 9;
break;
}
case 8:
{
WP[p][q] = 0;
q++;
WP[p][q] = 9;
break;
}
case 1:
{
if ( WP[p][q + 2] == 0 || WP[p][q + 2] == 8 )
{
WP[p][q + 2] = 1;
WP[p][q] = 0;
q++;
WP[p][q] = 9;
}else if ( WP[p][q + 2] == 2 || WP[p][q + 2] == 1 )
{
;
}
break;
}
case 2:
{
break;
}
default:
break;
}
break;
}
default:
break;
}
}
}
void DealWhitData_ZhiGuan()
{
/* int x=0,y=0; */
int s = 0, t = 0;
for ( i = 0; i < 8; i++ )
{
for ( j = 0; j < 9; j++ )
{
switch ( WP[i][j] )
{
case 0:
{
for ( k = 0; k < 3; k++ )
{
for ( l = 0; l < 3; l++ )
{
ZG[s][t] = 0;
t++;
}
s++;
t = t - 3;
}
break;
}
case 1:
{
for ( k = 0; k < 3; k++ )
{
for ( l = 0; l < 3; l++ )
{
ZG[s][t] = 1;
t++;
}
s++;
t = t - 3;
}
break;
}
case 2:
{
for ( k = 0; k < 3; k++ )
{
for ( l = 0; l < 3; l++ )
{
ZG[s][t] = 2;
t++;
}
s++;
t = t - 3;
}
break;
}
case 8:
{
for ( k = 0; k < 3; k++ )
{
for ( l = 0; l < 3; l++ )
{
ZG[s][t] = 0;
t++;
}
s++;
t = t - 3;
}
break;
}
case 9:
{
for ( k = 0; k < 3; k++ )
{
for ( l = 0; l < 3; l++ )
{
if ( (k == 0 || k == 2) && (l == 0 || l == 2) )
{
ZG[s][t] = 9;
}else if ( k == 1 && l == 1 )
{
ZG[s][t] = 9;
}else {
ZG[s][t] = 0;
}
t++;
}
s++;
t = t - 3;
}
break;
}
default:
break;
} /* switch(WP[i][j]) over */
s = s - 3;
t = t + 3;
}
s = s + 3;
t = 0;
}
ZG[13][4] = 8, ZG[13][7] = 8, ZG[16][4] = 8, ZG[19][4] = 8, ZG[19][7] = 8;
}
int Output()
{
system( "cls" );
Output_ConnectionData();
Output_PrintBox();
if ( WP[4][1] == 1 && WP[4][2] == 1 && WP[5][1] == 1 && WP[6][1] == 1 && WP[6][2] == 1 )
{
return(8);
}
return(0);
}
void Output_ConnectionData()
{
int WinNumber = 0;
printf( " name:XXXX \n\n" );
if ( WP[4][1] == 1 )
WinNumber++;
if ( WP[4][2] == 1 )
WinNumber++;
if ( WP[5][1] == 1 )
WinNumber++;
if ( WP[6][1] == 1 )
WinNumber++;
if ( WP[6][2] == 1 )
WinNumber++;
printf( "WinNumber= %d \n", WinNumber );
}
void Output_PrintBox()
{
for ( i = 0; i < 24; i++ )
{
for ( j = 0; j < 27; j++ )
{
switch ( ZG[i][j] )
{
case 0:
printf( " " );
break;
case 1:
printf( "O" );
break;
case 2:
printf( "M" );
break;
case 8:
printf( "T" );
break;
case 9:
printf( "#" );
break;
default:
break;
}
}
printf( "\n" );
}
}