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

📄 ddz2.cpp

📁 EVC 下的斗地主源码,有AI机器人,运行环境是模拟器5
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// ddz2.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "ddz2.h"
#include <commctrl.h>
#include  <windows.h> 
#include  <string.h> 
#include  <time.h> 
#include  <math.h> 
#include <tchar.h>
#include <stdlib.h>
#include <Afxwin.h>

#define MAX_LOADSTRING 100

#define ADD 20
#define WIDTH 800//实际上窗口
#define HEIGHT 480
HINSTANCE g_hInstance  =  NULL ; 
int IsGameOver();

void AddToMemDC(HWND hWnd , int PicName , int x , int y ,int Width,int Height);
void DelDC(HDC hdc) ; 
void LoadCard(HWND hWnd , int n , int *x , int *y  , int A ,int B);
void LoadMem(HWND hWnd , HDC hDC) ; 
void SendOutCard() ; 
void IfStart(int x,int y);
void clear(HWND hWnd);

struct PLAYER
{
	int card[20] ; 
	int statics[20] ; //5表正常,1表选出 , 2表打出
} ; 
struct CARDTYPE
{
	int card;
	char type[10] ; 
	int num ; 
	int min ; 
} ; 
CARDTYPE CurrentOutCard;
PLAYER player1 ; 
PLAYER player2 ;//= {{15 , 10 , 11 , 11 , 12 , 12 , 12 , 13 , 13 , 13 , 14 , 14 , 14 , 9 , 9 , 8 , 7}} ; 
PLAYER player3 ; 
PLAYER player4 ; 
void SaveOutCard(int *temp , PLAYER player) ; 
void ShowDesktop(HWND hWnd ,PLAYER player) ; 
void sort(PLAYER *player) ; 
void InitStatic() ; 
void DelCard(PLAYER *player) ; 
void SelectCard(int x , int y) ; 
void GetLord(PLAYER *player);
void NotGetLord(HWND hWnd);
int GameMain(HWND hWnd);
int CheckCard(PLAYER player) ; 
int CardNum(PLAYER player , int n) ; 

int CardX , CardY , g_nCardX1 , g_nCardY1 ; 
int xPos , yPos ; //单击到的位置
int g_nScreenX  =  0  ,  g_nScreenY  =  0 ;  // 屏幕宽度 , 屏幕高度
int g_nCardWidth  =  71  ,  g_nCardHeight  =  0 ; //牌的宽,高
int g_nTurnGetLord = 2;//刚开始时,到谁要牌
int g_nTurnOut;//到谁出牌
int g_nLastWhoOut;//上次是谁出的
int g_nWhoIsLord = 0;//谁是地主,1表左,2表我,3表右为地主
int g_nIfBegin = 1;//是不是刚开始,1 为是,
int g_nNotGetLord;
int g_nOutFlag;//右键改变其状态为1时出牌
int g_nPlayer1NotGet , g_nPlayer3NotGet ;//不叫
int g_nPlayer1NotOut , g_nPlayer3NotOut ;//不出
int g_nLast;//连子的最后一个
int g_nLen;//连子的长度
int g_nPosition;//位置
int g_nShowStart = 1;//控制开始按钮
int g_nShowNotOut;//控制不出按钮
int g_nShow3f;//控制叫按钮
int g_nShowNotGet;//控制不叫按钮
int g_nCanNotOut;//控制提示
int CurrentCard1[20] = {0},CurrentCard2[20] = {0},CurrentCard3[20] = {0};
int OutTemp[200][20];
int analyse[15][2] = {0};
char *Name = "ddz" ; 
HDC g_hMemDC  =  NULL ;  // 缓冲DC

LRESULT CALLBACK WndProc(HWND hWnd ,  UINT message ,  WPARAM wParam ,  LPARAM lParam) ; 


// Global Variables:
HINSTANCE			hInst;			// The current instance
HWND				hwndCB;			// The command bar handle

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass	(HINSTANCE, LPTSTR);
BOOL				InitInstance	(HINSTANCE, int);
LRESULT CALLBACK	WndProc			(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About			(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(	HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					LPTSTR    lpCmdLine,
					int       nCmdShow)
{
	g_hInstance  =  hInstance ; 

	// 第一步:注册窗口类
	WNDCLASS wndcls ; 
	wndcls.cbClsExtra  =  0 ; 
//	wndcls.cbSize  =  sizeof(WNDCLASSEX) ; 
	wndcls.cbWndExtra  =  0 ; 
	wndcls.hbrBackground  =  (HBRUSH)(COLOR_BACKGROUND) ; 
//	wndcls.hCursor  =  ::LoadCursor(hInstance ,  IDC_ARROW) ; 
	wndcls.hIcon  =  0 ; 
//	wndcls.hIconSm  =  0 ; 
	wndcls.hInstance  =  hInstance ; 
	wndcls.lpfnWndProc  =  WndProc ; 	// 重要!
	wndcls.lpszClassName  =  (unsigned short*)Name ;  // 重要!
	wndcls.lpszMenuName  =  0 ; 
	wndcls.style  =  CS_HREDRAW | CS_VREDRAW ; 
	::RegisterClass(&wndcls) ; 

	// 第二步:创建窗口
	HWND hWnd  =  ::CreateWindow(
					(unsigned short*)Name ,  
					_T("徐超的斗地主" ),   
					WS_VISIBLE ,  /* 普通窗口 */
					//WS_OVERLAPPED | WS_SYSMENU | WS_VISIBLE ,  /* 对话框 */
					0 ,  0 ,  WIDTH ,  HEIGHT ,  
					NULL ,  
					NULL ,  
					hInstance ,  
					0) ; 

	// 第三步:显示窗口
	::ShowWindow(hWnd ,  SW_SHOW) ; 
	::UpdateWindow(hWnd) ; 


	g_nTurnGetLord = rand() % 3 + 1;

	// 第四步:消息循环
	MSG msg ; 
	while (GetMessage(&msg ,  NULL ,  0 ,  0)) 
	{
		GameMain(hWnd);
		//InvalidateRect (hWnd ,  NULL ,  0)  ; 
		DispatchMessage(&msg) ;

		
	}
		
	return msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    It is important to call this function so that the application 
//    will get 'well formed' small icons associated with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
	WNDCLASS	wc;

    wc.style			= CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc		= (WNDPROC) WndProc;
    wc.cbClsExtra		= 0;
    wc.cbWndExtra		= 0;
    wc.hInstance		= hInstance;
    wc.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_DDZ2));
    wc.hCursor			= 0;
    wc.hbrBackground	= (HBRUSH) GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName		= 0;
    wc.lpszClassName	= szWindowClass;

	return RegisterClass(&wc);
}

//
//  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)
{
	HWND	hWnd;
	TCHAR	szTitle[MAX_LOADSTRING];			// The title bar text
	TCHAR	szWindowClass[MAX_LOADSTRING];		// The window class name

	hInst = hInstance;		// Store instance handle in our global variable
	// Initialize global strings
	LoadString(hInstance, IDC_DDZ2, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance, szWindowClass);

	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

	if (!hWnd)
	{	
		return FALSE;
	}

	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);
	if (hwndCB)
		CommandBar_Show(hwndCB, TRUE);

	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)
{
	switch (message)
	{
	case WM_CREATE:
		{
			// 创建缓冲DC
			HDC hWndDC  =  ::GetDC(hWnd) ; 
			HBITMAP hBitmap  =  NULL ; 
			g_hMemDC  =  ::CreateCompatibleDC(hWndDC) ; 
			g_nScreenX  =  ::GetSystemMetrics(SM_CXSCREEN) ; 
			g_nScreenY  =  ::GetSystemMetrics(SM_CYSCREEN) ; 
			hBitmap  =  ::CreateCompatibleBitmap(hWndDC ,  g_nScreenX ,  g_nScreenY) ; 
			::SelectObject (g_hMemDC ,  hBitmap) ; 
			::DeleteObject(hBitmap) ;  // 可以释放了!
			::ReleaseDC(hWnd ,  hWndDC) ;  // 与GetDC()配套!			
			// 填充缓冲DC为白色
			//::BitBlt(g_hMemDC ,  0 ,  0 ,  g_nScreenX ,  g_nScreenY ,  g_hMemDC ,  0 ,  0 ,  WHITENESS) ; 
			
			SendOutCard() ; //发牌
			sort(&player2) ; //排序
			//::SetTimer(hWnd, 1, 1000, NULL); // 创建定时器
			::SetTimer(hWnd,1,500,NULL);
			//
			clear(hWnd);
			AddToMemDC(hWnd,IDB_BMP_QUIT,150,250,g_nScreenX,g_nScreenY);
		}
		break ; 

	case WM_TIMER:
		{
			
		}
        break;

	case WM_LBUTTONDOWN:
		{
			if(xPos>150 && xPos<250  && yPos>150 && yPos<180)
			{
				MessageBox(NULL, _T("BTN EXIT DOWN"), _T("SYS EXIT"), MB_OK);
				PostQuitMessage(0) ; 
				break;
			}
			xPos  =  LOWORD (lParam) ; 
			yPos  =  HIWORD (lParam) ; 
			if(g_nTurnOut == 2)
				SelectCard(xPos , yPos) ; //选出要打的牌
			IfStart(xPos , yPos);
			//不出
			if(xPos>380 && xPos<441  && yPos<226 && yPos>200 && g_nShowNotOut == 1)
			{
				g_nTurnOut++;
				g_nTurnOut %= 3 ;
				if(g_nTurnOut == 0) g_nTurnOut = 3 ;
				g_nShowNotOut = 0;
			}
			//叫
			if(xPos>340 && xPos<400  && yPos<226 && yPos>200 && g_nShow3f == 1)
			{
				GetLord(&player2);//叫
				g_nLastWhoOut = 2;
				g_nWhoIsLord = 2;
				g_nTurnOut = 2;
				g_nPlayer3NotGet = 0;
				g_nPlayer1NotGet = 0;
				g_nShow3f = 0;
				g_nShowNotGet = 0;
			}
			//不叫
			if(xPos>410 && xPos<470  && yPos<226 && yPos>200 && g_nShowNotGet == 1)
			{
				NotGetLord(hWnd);//不叫
				if(g_nNotGetLord >=3)
				{
					g_nNotGetLord = 0;
					g_nTurnGetLord = rand() % 3 + 1;
					g_nIfBegin = 1;
					MessageBox(hWnd , _T("都不叫,重新发牌!") , _T("提示信息") , MB_OK);
					SendOutCard();
					g_nShowNotGet = 0;
					g_nShow3f = 0;
				}
				g_nShowNotGet = 0;
				g_nShow3f = 0;
			}
			//添加的出牌方式
			if(xPos >300 && xPos < 360 && yPos>200 && yPos< 220)
			{
				g_nOutFlag = 1;//出牌标志
			}
		}
		break ; 
	
	case WM_RBUTTONDOWN:
		{
			//g_nOutFlag = 1;//出牌标志
		}	
		break ; 

	case WM_MOUSEMOVE:
		{
			//DelCard(&player1) ; 
			//InvalidateRect (hWnd ,  NULL ,  0)  ; 
		}
		break ; 

	case WM_PAINT:
		{			
			PAINTSTRUCT paint ; 
			HDC hDC  =  ::BeginPaint(hWnd ,  &paint) ; 
			ShowDesktop(hWnd , player1) ; //显示手中的牌

			RECT rcClient ; 
			::GetClientRect(hWnd ,  &rcClient) ;  // 取得客户区大小
			::BitBlt(hDC ,  // 目的DC
				     0 ,  0 , 
					 rcClient.right ,  rcClient.bottom ,  
					 g_hMemDC ,  // 源DC
					 0 , 0 , 
					 SRCCOPY) ;  // 复制缓冲DC到屏幕		
			::EndPaint(hWnd ,  &paint) ; 
		}
		break ; 

	case WM_COMMAND:
		{
			int wNotifyCode = HIWORD(wParam);
			if(wNotifyCode == BN_CLICKED)
			{
				switch(LOWORD(wParam))
				{
				case 5://不出
					break;
				}
			}
		}
		break;

	case WM_DESTROY:
	    DeleteDC(g_hMemDC) ; 
		PostQuitMessage(0) ; 
		break ; 

	default:
		return DefWindowProc(hWnd ,  message ,  wParam ,  lParam) ; 
	}

	return 0 ; 
	/*
	HDC hdc;
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	TCHAR szHello[MAX_LOADSTRING];

	switch (message) 
	{
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				case IDM_HELP_ABOUT:
				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				   break;
				case IDM_FILE_EXIT:
				   DestroyWindow(hWnd);
				   break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_CREATE:
			hwndCB = CommandBar_Create(hInst, hWnd, 1);			
			CommandBar_InsertMenubar(hwndCB, hInst, IDM_MENU, 0);
			CommandBar_AddAdornments(hwndCB, 0, 0);
			break;
		case WM_PAINT:
			RECT rt;
			hdc = BeginPaint(hWnd, &ps);
			GetClientRect(hWnd, &rt);
			LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
			DrawText(hdc, szHello, _tcslen(szHello), &rt, 
				DT_SINGLELINE | DT_VCENTER | DT_CENTER);
			EndPaint(hWnd, &ps);
			break;
		case WM_DESTROY:
			CommandBar_Destroy(hwndCB);
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
   */
}

// Mesage handler for the About box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	RECT rt, rt1;
	int DlgWidth, DlgHeight;	// dialog width and height in pixel units
	int NewPosX, NewPosY;

	switch (message)
	{
		case WM_INITDIALOG:
			// trying to center the About dialog
			if (GetWindowRect(hDlg, &rt1)) {
				GetClientRect(GetParent(hDlg), &rt);
				DlgWidth	= rt1.right - rt1.left;
				DlgHeight	= rt1.bottom - rt1.top ;
				NewPosX		= (rt.right - rt.left - DlgWidth)/2;
				NewPosY		= (rt.bottom - rt.top - DlgHeight)/2;
				
				// if the About box is larger than the physical screen 
				if (NewPosX < 0) NewPosX = 0;
				if (NewPosY < 0) NewPosY = 0;
				SetWindowPos(hDlg, 0, NewPosX, NewPosY,
					0, 0, SWP_NOZORDER | SWP_NOSIZE);
			}
			return TRUE;

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

////////////////////////////////////////////////////////////////////////////////
// 时间控制
////////////////////////////////////////////////////////////////////////////////
void delay(DWORD dwTime)
{
	DWORD dwStart = GetTickCount();
	while (GetTickCount() - dwStart < dwTime)
	{
		MSG msg ; 
		if (GetMessage(&msg,  NULL,  0,  0))
			DispatchMessage(&msg); 
	}
}

////////////////////////////////////////////////////////////////////////////////
// 开不开始
////////////////////////////////////////////////////////////////////////////////
void IfStart(int x,int y)
{
	if(x>650 && x<710  && y<277 && y>250 && g_nShowStart == 1)
	{
		InitStatic();
		g_nShowStart = 0;
	}
}
////////////////////////////////////////////////////////////////////////////////
// 叫不叫
////////////////////////////////////////////////////////////////////////////////
int IfGetLord(PLAYER player)
{
	int num = 0;
	for(int i = 0;i < 20;i++)
	{
		if(player.card [i] >14)
			num++;
	}
	if(num >= 2)
		return 1;
	return 0;
}
////////////////////////////////////////////////////////////////////////////////
// 叫
////////////////////////////////////////////////////////////////////////////////
void GetLord(PLAYER *player)
{
	for(int i = 17;i < 20;i++)//加入另三张
	{
		player->card [i] = player4.card[i-17];
		player->statics [i] = 5;
		player->card [i] = player4.card[i-17];
		player->statics [i] = 5;
		player->card [i] = player4.card[i-17];
		player->statics [i] = 5;
	}
	sort(player);
	//g_nLastWhoOut = g_nTurnOut = g_nWhoIsLord = 1;
	g_nIfBegin = 0;
	g_nNotGetLord = 0;
	g_nTurnGetLord = rand() % 3 + 1;
}
////////////////////////////////////////////////////////////////////////////////
// 不叫
////////////////////////////////////////////////////////////////////////////////
void NotGetLord(HWND hWnd)
{
	g_nNotGetLord ++;
	g_nTurnGetLord ++;

⌨️ 快捷键说明

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