VC++ 6.0 MFC 俄罗斯方块 自动求解 代码 源程序
时间:2025-05-01
时间:2025-05-01
F1开启/关闭自动求解。
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#define tDown 1 //方块下落定时器的标识(编号)
#define tPaint 2 //重绘定时器的标识(编号)
#define tDownTime 500 //方块下落一行位置的时间间隔
#define tPaintTime 50 //窗口重绘的时间间隔
#define ROW 24 //地图的行数目(第23行不用)
#define COL 14 //地图的列数目(第0列和第13列不用)
#define MAX_CLASS 7 //方块形状数目
#define LEN 20 //每个方格大小为20×20像素
#define StartY -1 * LEN + 5 //-15,绘制俄罗斯方块地图时的边界起始位置 #define StartX -1 * LEN + 5 //-15
int iDeleteRows = 0; //总共清除的行
int iTotalNum = 0; //总得分
char WindowTxt[100] = "俄罗斯方块游戏 自动求解已关闭"; //窗口标题
char s1[] = "关闭", s2[] = "启动"; //用于启动/关闭自动求解功能时显示不同的标题
F1开启/关闭自动求解。
bool bAuto; //是否自动求解的标志
bool Pause; //是否暂停的标志
int Map[ROW][COL]; //俄罗斯方块的地图(被占据的方格为1,否则为0)
int CurrentBox[4][4]; //当前落下的方块
int CurrentY, CurrentX; //当前落下方块的当前位置(指左上角位置)
int NextBox[4][4]; //下一个将落下的方块
int Box[MAX_CLASS][4][4] = //7种方块形状
{
{
{0,0,0,0},
{1,1,1,1},
{0,0,0,0},
{0,0,0,0}
},
{
{0,0,0,0},
{0,1,0,0},
{1,1,1,0},
{0,0,0,0}
},
{
{0,0,0,0},
{1,1,0,0},
{0,1,1,0},
{0,0,0,0}
},
{
{0,0,0,0},
{0,1,1,0},
{1,1,0,0},
{0,0,0,0}
},
{
{0,1,1,0},
{0,0,1,0},
{0,0,1,0},
{0,0,0,0}
},
{
{0,1,1,0},
{0,1,0,0},
{0,1,0,0},
{0,0,0,0}
F1开启/关闭自动求解。
{
{0,0,0,0},
{0,1,1,0},
{0,1,1,0},
{0,0,0,0}
}
};
void InitMap( ); //初始化地图
int NewFall( ); //新的方块落下
void BuildNextBox( ); //产生下一个随机的方块
int Test( int y, int x, int box[4][4] ); //测试在(y,x)位置是否能放置方块box,能放置返回1,否则返回0
int Drop( ); //定时时间到,当前方块下降一行位置
void PutBox( ); //放置当前方块
int Move( int Right ); //(通过方向键)移动方块,参数right为1表示向右移动,为0表示向左移动
void Clear( ); //清除满足条件的行
int Rotate( ); //测试旋转是否可行,如果可行则旋转当前方块
int RotateTest( int Box1[4][4], int Box2[4][4] ); //旋转当前方块
int count1( int y, int x, int box[4][4] ); //新增函数
int BestStartX( ); //新增函数
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM ); //窗口处理函数声明
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow ) //入口函数
{
static TCHAR szAppName[ ] = TEXT ("Russion");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wndclass.hCursor = LoadCursor( NULL, IDC_ARROW );
wndclass.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
F1开启/关闭自动求解。
if( !RegisterClass( &wndclass ) )
{
MessageBox( NULL, TEXT ("Program requires Windows NT!" ),
szAppName, MB_ICONERROR );
return 0;
}
hwnd = CreateWindow( szAppName, WindowTxt,
WS_OVERLAPPED | WS_SYSMENU | WS_BORDER,
CW_USEDEFAULT, CW_USEDEFAULT,
(COL + 4) * LEN, //窗口宽度:(14+4)×20=360像素
ROW * LEN, //窗口高度:24×20=480像素(包括标题栏部分)
NULL, NULL, hInstance, NULL );
ShowWindow( hwnd, iCmdShow );
UpdateWindow( hwnd );
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return msg.wParam;
}
//初始化地图(将第0列和第13列、第23行设置为1(被占据),其他位置设置为0) void InitMap( )
{
int y, x;
for( y = 0; y < ROW; y++ )
{
for( x = 0; x < COL; x++ )
{
//第0列、第13列、第23行设置为1(被占据)
if( x < 1 || x > COL - 2 || y > ROW - 2 )
Map[y][x] = 1;
else Map[y][x] = 0;
}
}
}
//计算在(y,x)位置(左上角位置)上放置方块box后空出的方格数
F1开启/关闭自动求解。
int count1( int y, int x, int box[4][4] )
{
if( !Test(y,x,box) ) return 100; //不能在(y,x)位置放置box,返回∞
if( Test(y+1,x,box) ) return 100; //如果box还能下降,也返回∞
int tmpy, tmpx;
int c = 0; //空出的方格数
for( tmpx = 0; tmpx < 4; tmpx++ ) //考虑第0~3列
{
for( tmpy = 3; tmpy >= 0; tmpy-- )
{
if( box[tmpy][tmpx] ) break;
}tmpy++;
if( tmpy>0 )
{
for( ; tmpy<4; tmpy++ )
{
if( tmpy+y<0 || tmpy+y>=ROW || tmpx+x<0 || tmpx+x>=COL ) continue; i …… 此处隐藏:11379字,全部文档内容请下载后查看。喜欢就下载吧 ……