⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bahelp.cpp

📁 8皇后国际象棋问题练习,实现基本算法
💻 CPP
字号:
// bahelp.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
#include "bahelp.h"
#include "MATH.H"
#include "stdlib.h"

#define MAX_LOADSTRING 100

// Global Variables:
HWND hWnd;
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];								// The title bar text
static TCHAR szAppName[] = TEXT ("HelloWin") ;
int ChessArry[7];
int column=0;
bool show=false;


// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
bool FindNextCol();
bool CheckChess(int chessA[],int ChessX,int Chessy);
bool ExecLet();

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_BAHELP, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_BAHELP);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{

	WNDCLASS	wndclass ;

 	wndclass.style		  = CS_HREDRAW | CS_VREDRAW ;
 	wndclass.lpfnWndProc  = (WNDPROC)WndProc;
	wndclass.cbClsExtra	  = 0 ;
	wndclass.cbWndExtra	  = 0 ;
	wndclass.hInstance	  = hInstance ;
	wndclass.hIcon		  = LoadIcon(hInstance, (LPCTSTR)IDI_BAHELP);
  	wndclass.hCursor	  = LoadCursor (NULL, IDC_ARROW) ;
 	wndclass.hbrBackground	= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  	wndclass.lpszMenuName	= (LPCSTR)IDC_BAHELP;
	wndclass.lpszClassName	= szAppName ;

	return RegisterClass(&wndclass);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   

   hInst = hInstance; // Store instance handle in our global variable
   RECT rect1,rect2;
   int  width,height;
   hWnd = CreateWindow(szAppName, szTitle, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ::GetWindowRect(hWnd,&rect1);
   ::GetClientRect(hWnd,&rect2);
   width=rect1.right-rect1.left-rect2.right+rect2.left;
   height=rect1.bottom-rect1.top-rect2.bottom+rect2.top;
 //  ::SetWindowPos(hWnd,NULL,100,100,240+width,240+height,SWP_NOMOVE|SWP_NOZORDER);
   ::MoveWindow(hWnd,300,100,401+width,401+height,true);

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	TCHAR szHello[MAX_LOADSTRING];
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
	int i,j;
	RECT rt;			

	switch (message) 
	{
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				case IDM_ABOUT:
				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);	 
				   break;
                case IDM_START:
				     show=ExecLet();
					 GetClientRect(hWnd, &rt);
					 ::InvalidateRect(hWnd,&rt,false);
					 
				   break;
				case IDM_EXIT:
				   DestroyWindow(hWnd);
				   break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			// TODO: Add any drawing code here...
			SelectObject(hdc, CreatePen (PS_SOLID, 0, RGB (255, 0, 0)));
            for(i=0;i<10;i++)
			{
				::MoveToEx(hdc,i*50,0,NULL);
				::LineTo(hdc,i*50,401);
				for(j=0;j<10;j++)
				{
					::MoveToEx(hdc,0,j*50,NULL);
				    ::LineTo(hdc,401,j*50);
				}

			}
			if(show)
			{
				for(i=0;i<10;i++)
				{

				    Ellipse (hdc, 10+ChessArry[i]*50, 10+i*50, 40+ChessArry[i]*50, 40+i*50); 

				}
			}


			EndPaint(hWnd, &ps);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_INITDIALOG:
				return TRUE;

		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
			{
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
			break;
	}
    return FALSE;
}

bool FindNextCol()
{
	int i;
	bool FindVal=false;
	if(column==8)
		{
			FindVal=true;
		}
	else
	{
 	    for(i=0;i<8;i++)
		{
	    	if(CheckChess(ChessArry,column,i))
			{
				ChessArry[column]=i;
				++column;
			   if(column<9) FindVal=FindNextCol();
			   if(FindVal)
					break;
				else
					--column;
			}
			else
				FindVal=false;
		}
	}
	return FindVal;
}

bool CheckChess(int chessA[],int ChessX,int Chessy)
{
	int i;
	bool Value=true;
	for(i=0;i<column;i++)
	{
		if(abs(ChessX-i)==abs(Chessy-chessA[i])||chessA[i]==Chessy)
			Value=false;
		
	}
	return Value;
}

bool ExecLet()
{
	int i;
	bool LetVal=false;
	column=1;
	for(i=7;i<8;i++)
	{
		ChessArry[0]=i;
		LetVal=FindNextCol();
		if(LetVal) break;
		
	}
	return LetVal;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -