本程序是基于 API 技术,用 VC++ 6.0 MFC 开发工具编写的,实现了数独小游戏。
以下为全部源程序代码。
//written by luweikang// shudu.cpp : Defines the entry point for the application.
//finished 2010-06-20
#include
#include "stdafx.h"
#include
#include
#define tPaint
#define tPaintTime
#define ROW
#define COL
#define LEN
#define StartY
#define StartX
int CurrentX=0;
int CurrentY=0;
2 //重绘定时器的标识(编号)
50 //窗口重绘的时间间隔
//方格的行数与列数
//方格的行数与列数
//每个方格大小为 40×40 像素
9
9
40
3 //
3 //绘制方格地图时的边界起始位置
//当前操作位置
void
InitMap_show( );
//初始化地图
value[ROW][COL];
Initvalue( );
judge();
random();
change[9][9];
Initchange();
IsSuccess();
answer[9][9][9];
Initanswer();
bauto(int n);
showhelp();
int
void
int
void
int
void
int
int
void
void
void
bool help=0;
bool
int
f;
tmp=20;
//记录每个方格中的数字
//初始化方格中的数字为 0
//判断当前位置是否能输入用户想要输入的数字
//实现随机生成整张地图上的数字
//用于记录各个位置的数字是否可改变,0 不可变,1 可变
//初始化 change 数组中的值
//判断是否完成
//用于保存每个方格可以填入的数字
//初始化 answer[81][9][9]全部为 0
//自动求解
//求辅助数据
//是否显示辅助数据
//成功标志
//记录当前地图是哪副
int easy[21][9][9]={
//用于存储初级地图
{
},
{
},
{
{0,0,0,0,3,0,2,0,8},
{0,2,8,0,4,0,1,3,0},
{0,0,9,5,8,0,0,4,7},
{0,3,1,0,2,0,5,9,0},
{0,0,5,3,9,1,7,0,0},
{0,9,0,4,7,5,8,1,3},
{0,0,0,0,6,0,4,0,2},
{0,0,0,0,0,3,0,6,0},
{0,7,6,2,5,4,0,8,0}
{0,0,0,0,0,4,0,5,2},
{0,0,5,0,0,7,0,1,4},
{0,0,1,0,0,8,3,0,6},
{0,1,2,0,0,6,4,3,9},
{0,0,9,0,4,1,6,8,5},
{0,0,6,0,3,5,2,7,1},
{0,6,7,0,1,0,0,0,0},
{1,5,0,0,0,3,0,2,8},
{0,2,0,0,5,9,0,6,0}
{0,7,8,4,0,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,0,0,0},
{6,4,0,8,0,1,7,0,2},
{7,0,3,9,4,6,0,8,1},
{0,1,8,0,0,0,0,3,0}
//第 20 种
{0,3,5,0,0,9,0,2,7},
{0,2,9,3,5,1,0,6,8},
{0,0,0,0,0,4,0,0,9},
{0,5,0,5,6,0,0,0,3},
{8,7,0,5,0,3,0,0,2},
{0,4,0,0,0,2,0,0,5},
{6,0,4,0,3,7,2,0,0},
{0,1,3,9,2,0,8,7,4},
{0,0,0,0,0,5,0,9,6}
//测试数据
{7,1,2,3,6,9,4,5,8},
{3,4,5,2,1,8,6,7,9},
{9,6,8,4,5,7,3,1,2},
{4,2,1,7,9,5,8,3,6},
{5,7,3,8,4,6,2,9,1},
{6,8,9,1,3,2,5,4,7},
{8,5,7,9,2,4,1,6,3},
{1,9,4,6,8,3,7,2,5},
{2,3,6,5,7,1,0,8,4}
}
};
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
//窗口处理函数
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR
int
iCmdShow)
lpCmdLine,
{
// written by luweikang
// TODO: Place code here.
static TCHAR szAppName[ ] = TEXT ("Russion");
HWND
MSG
WNDCLASS
hwnd;
msg;
wndclass;
= CS_HREDRAW | CS_VREDRAW;
= WndProc;
= 0;
= 0;
wndclass.style
wndclass.lpfnWndProc
wndclass.cbClsExtra
wndclass.cbWndExtra
wndclass.hInstance
wndclass.hIcon
wndclass.hCursor
wndclass.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
= hInstance;
= LoadIcon( NULL, IDI_APPLICATION );
= LoadCursor( NULL, IDC_ARROW );
if( !RegisterClass( &wndclass ) )
{
MessageBox( NULL, TEXT ("Program requires Windows NT!" ),
szAppName, MB_ICONERROR );
return 0;
}
hwnd = CreateWindow( szAppName, TEXT ("数独游戏"),
WS_OVERLAPPED | WS_SYSMENU | WS_BORDER,
CW_USEDEFAULT, CW_USEDEFAULT,
(COL + 4) * LEN+2,
ROW * LEN + 40,
NULL, NULL, hInstance, NULL );
//窗口宽度
//窗口高度(包括标题栏部分 29pix)
ShowWindow( hwnd, iCmdShow );
UpdateWindow( hwnd );
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return msg.wParam;
}
void Initvalue( )
{
//初始化地图上的每一个值,使其为零
int y, x;
for( y = 0; y < ROW; y++ )
{
for( x = 0; x < COL; x++ )
{
value[y][x] = 0;
}
}