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

📄 owndrawbuttondlg.cpp

📁 用人工智能的αβ剪枝算法实现,界面整洁漂亮,人机各为一方,三子连成一线即赢
💻 CPP
字号:
// OwnDrawButtonDlg.cpp : implementation file
//

#include "stdafx.h"
#include "OwnDrawButton.h"
#include "OwnDrawButtonDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// COwnDrawButtonDlg dialog

COwnDrawButtonDlg::COwnDrawButtonDlg(CWnd* pParent /*=NULL*/)
	: CDialog(COwnDrawButtonDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(COwnDrawButtonDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	int i = 0, j = 0;
	for (i = 0; i < 3; ++i)
		for (j = 0; j < 3; ++j)
			table[i][j] = 'c';
		count1=0;
		count2=0;
}

void COwnDrawButtonDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COwnDrawButtonDlg)
	DDX_Control(pDX, IDC_PLUCKBUTTON5, m_PluckBut5);
	DDX_Control(pDX, IDC_PLUCKBUTTON9, m_PluckBut9);
	DDX_Control(pDX, IDC_PLUCKBUTTON8, m_PluckBut8);
	DDX_Control(pDX, IDC_PLUCKBUTTON7, m_PluckBut7);
	DDX_Control(pDX, IDC_PLUCKBUTTON6, m_PluckBut6);
	DDX_Control(pDX, IDC_PLUCKBUTTON4, m_PluckBut4);
	DDX_Control(pDX, IDC_PLUCKBUTTON3, m_PluckBut3);
	DDX_Control(pDX, IDC_PLUCKBUTTON2, m_PluckBut2);
	DDX_Control(pDX, IDC_PLUCKBUTTON1, m_PluckBut1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(COwnDrawButtonDlg, CDialog)
	//{{AFX_MSG_MAP(COwnDrawButtonDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_PLUCKBUTTON1, OnPluckbutton1)
	ON_BN_CLICKED(IDC_PLUCKBUTTON2, OnPluckbutton2)
	ON_BN_CLICKED(IDC_PLUCKBUTTON3, OnPluckbutton3)
	ON_BN_CLICKED(IDC_PLUCKBUTTON4, OnPluckbutton4)
	ON_BN_CLICKED(IDC_PLUCKBUTTON5, OnPluckbutton5)
	ON_BN_CLICKED(IDC_PLUCKBUTTON6, OnPluckbutton6)
	ON_BN_CLICKED(IDC_PLUCKBUTTON7, OnPluckbutton7)
	ON_BN_CLICKED(IDC_PLUCKBUTTON8, OnPluckbutton8)
	ON_BN_CLICKED(IDC_PLUCKBUTTON9, OnPluckbutton9)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COwnDrawButtonDlg message handlers

BOOL COwnDrawButtonDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// 初始化按钮
	b_BufFlag = true;
	m_MyButton3.SetBkColor( RGB(202,186,239) );
	m_MyButton3.SetForeColor( RGB(255,255,0) );

	m_TitleBox2.SetBkColor( RGB(248,179,253) );
	m_TitleBox2.SetForeColor( RGB(0,255,0) );
	m_TitleBox2.SetButStatus( BUT_STATUS_RIGHT );

	m_PluckBut2.SetPluckButStyle( KEY_STYLE_LEFT );
	m_PluckBut3.SetPluckButStyle( KEY_STYLE_RIGHT );
	m_PluckBut4.SetPluckButStyle( KEY_STYLE_UP );
	m_PluckBut5.SetPluckButStyle( KEY_STYLE_DOWN );
    m_PluckBut6.SetPluckButStyle( KEY_STYLE_LEFT );
	m_PluckBut7.SetPluckButStyle( KEY_STYLE_RIGHT );
	m_PluckBut8.SetPluckButStyle( KEY_STYLE_UP );
	m_PluckBut9.SetPluckButStyle( KEY_STYLE_DOWN );

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void COwnDrawButtonDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR COwnDrawButtonDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//////////////////////////////////////////////////////////////////////////////

//DEL void COwnDrawButtonDlg::OnMybutton1() 
//DEL {
//DEL 	b_BufFlag = !b_BufFlag;
//DEL 	m_MyButton2.EnableWindow( b_BufFlag );
//DEL }

//单击标题条响应函数示例:与普通按钮不同,只有单击它的文字部分才应该给予响应
//DEL void COwnDrawButtonDlg::OnTitlebox1() 
//DEL {
//DEL 	if( m_TitleBox1.ClickBut() )	//单击有效区,执行相应操作
//DEL 	{
//DEL 
//DEL 	}
//DEL }



void COwnDrawButtonDlg::OnPluckbutton1() 
{
	// TODO: Add your control notification handler code here
    if(table[0][0]=='c')
		table[0][0]='o';
	else
	{
		::AfxMessageBox("白痴,有子了,下另一格去!");
		return;
	}
	UpdateView();
	if(check(table)==2)
	{
		::AfxMessageBox("恭喜,你赢了!");

		return;
	}
	else	if(check(table)==3)
	{
		::AfxMessageBox("晕,平局!");
		return;
	}
	

	int x, y;
	m_chess.work(table, x, y);
	table[x][y]='x';
	UpdateView();
	if(check(table)==1)
	{
		::AfxMessageBox("啊噢,你输了!");
		return;
	}
	else	if(check(table)==3)
	{

		::AfxMessageBox("晕,平局!");
	    return;
	}
}

void COwnDrawButtonDlg::OnPluckbutton2() 
{
	// TODO: Add your control notification handler code here
   if( table[0][1]=='c') table[0][1]='o';
	else
	{
		::AfxMessageBox("白痴,有子了,下另一格去!");
		return;
	}
	UpdateView();
		if(check(table)==2)
		{
			::AfxMessageBox("恭喜,你赢了!");
			return;
		}
		else	if(check(table)==3)
		{
		   ::AfxMessageBox("晕,平局!");
		   return;
		}
		
	int x, y;
	m_chess.work(table, x, y);
	table[x][y]='x';
	UpdateView();	
	if(check(table)==1)
		::AfxMessageBox("啊噢,你输了!");
	else	if(check(table)==3)
		::AfxMessageBox("晕,平局!");
	UpdateView();
}

void COwnDrawButtonDlg::OnPluckbutton3() 
{
	// TODO: Add your control notification handler code here
    if( table[0][2]=='c') table[0][2]='o';
	else
	{
		::AfxMessageBox("白痴,有子了,下另一格去!");
		return;
	}
	UpdateView();
			if(check(table)==2)
		{
			::AfxMessageBox("恭喜,你赢了!");
			return;
		}
		else	if(check(table)==3)
		{
		   ::AfxMessageBox("晕,平局!");
		   return;
		}
		
	int x, y;
	m_chess.work(table, x, y);
	table[x][y]='x';
	UpdateView();	
	if(check(table)==1)
		::AfxMessageBox("啊噢,你输了!");
	else	if(check(table)==3)
		::AfxMessageBox("晕,平局!");
	UpdateView();
}

void COwnDrawButtonDlg::OnPluckbutton4() 
{
	// TODO: Add your control notification handler code here
    if( table[1][0]=='c') table[1][0]='o';
	else
	{
		::AfxMessageBox("白痴,有子了,下另一格去!");
		return;
	}
	UpdateView();
			if(check(table)==2)
		{
		  ::AfxMessageBox("恭喜,你赢了!");
			return;
		}
		else	if(check(table)==3)
		{
		   ::AfxMessageBox("晕,平局!");
		   return;
		}
		
	int x, y;
	m_chess.work(table, x, y);
	table[x][y]='x';
	UpdateView();	
	if(check(table)==1)
		::AfxMessageBox("啊噢,你输了!");
	else	if(check(table)==3)
		::AfxMessageBox("晕,平局!");
	UpdateView();
}

void COwnDrawButtonDlg::OnPluckbutton5() 
{
	// TODO: Add your control notification handler code here
   if( table[1][1]=='c') table[1][1]='o';
	else
	{
		::AfxMessageBox("白痴,有子了,下另一格去!");
		return;
	}
	UpdateView();
			if(check(table)==2)
		{
			::AfxMessageBox("恭喜,你赢了!");
			return;
		}
		else	if(check(table)==3)
		{
		   ::AfxMessageBox("晕,平局!");
		   return;
		}
		
	int x, y;
	m_chess.work(table, x, y);
	table[x][y]='x';
	UpdateView();	
	if(check(table)==1)
		::AfxMessageBox("啊噢,你输了!");
	else	if(check(table)==3)
		::AfxMessageBox("晕,平局!");
	UpdateView();
}

void COwnDrawButtonDlg::OnPluckbutton6() 
{
	// TODO: Add your control notification handler code here
    if( table[1][2]=='c') table[1][2]='o';
	else
	{
		::AfxMessageBox("白痴,有子了,下另一格去!");
		return;
	}
	UpdateView();
			if(check(table)==2)
		{
			::AfxMessageBox("恭喜,你赢了!");
			return;
		}
		else	if(check(table)==3)
		{
		   ::AfxMessageBox("晕,平局!");
		   return;
		}
		
	int x, y;
	m_chess.work(table, x, y);
	table[x][y]='x';
	UpdateView();
	if(check(table)==1)
		::AfxMessageBox("啊噢,你输了!");
	else	if(check(table)==3)
		::AfxMessageBox("晕,平局!");
	UpdateView();
}

void COwnDrawButtonDlg::OnPluckbutton7() 
{
	// TODO: Add your control notification handler code here
    if( table[2][0]=='c') table[2][0]='o';
	else
	{
		::AfxMessageBox("白痴,有子了,下另一格去!");
		return;
	}
	UpdateView();
	
			if(check(table)==2)
		{
			::AfxMessageBox("恭喜,你赢了!");
			return;
		}
		else	if(check(table)==3)
		{
		   ::AfxMessageBox("晕,平局!");
		   return;
		}
		
		int x, y;
	m_chess.work(table, x, y);
	table[x][y]='x';
	UpdateView();
	if(check(table)==1)
		::AfxMessageBox("啊噢,你输了!");
	else	if(check(table)==3)
		::AfxMessageBox("晕,平局!");
	UpdateView();
}

void COwnDrawButtonDlg::OnPluckbutton8() 
{
	// TODO: Add your control notification handler code here
   if( table[2][1]=='c') table[2][1]='o';
	else
	{
		::AfxMessageBox("白痴,有子了,下另一格去!");
		return;
	}
	UpdateView();
			if(check(table)==2)
		{
			::AfxMessageBox("恭喜,你赢了!");
			return;
		}
		else	if(check(table)==3)
		{
		   ::AfxMessageBox("晕,平局!");
		   return;
		}
		
	int x, y;
	m_chess.work(table, x, y);
	table[x][y]='x';
	UpdateView();
	if(check(table)==1)
		::AfxMessageBox("啊噢,你输了!");
	else	if(check(table)==3)
		::AfxMessageBox("晕,平局!");
	UpdateView();
}

void COwnDrawButtonDlg::OnPluckbutton9() 
{
	// TODO: Add your control notification handler code here
    if( table[2][2]=='c') table[2][2]='o';
	else
	{
		::AfxMessageBox("白痴,有子了,下另一格去!");
		return;
	}
	UpdateView();
			if(check(table)==2)
		{
			::AfxMessageBox("恭喜,你赢了!");
			return;
		}
		else	if(check(table)==3)
		{
		   ::AfxMessageBox("晕,平局!");
		   return;
		}
		
	int x, y;
	m_chess.work(table, x, y);
	table[x][y]='x';
	UpdateView();
	if(check(table)==1)
		::AfxMessageBox("啊噢,你输了!");
else	if(check(table)==3)
		

     ::AfxMessageBox("晕,平局!");
	UpdateView();
}

void COwnDrawButtonDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
    initialqipan();
}

void COwnDrawButtonDlg::UpdateView()
{
	int buttonID[] = 
	{
		IDC_PLUCKBUTTON1,
		IDC_PLUCKBUTTON2,
		IDC_PLUCKBUTTON3,
		IDC_PLUCKBUTTON4,
		IDC_PLUCKBUTTON5,
		IDC_PLUCKBUTTON6,
		IDC_PLUCKBUTTON7,
		IDC_PLUCKBUTTON8,
		IDC_PLUCKBUTTON9
	};

   
	for (int i = 0; i < 3; ++i)
		for (int j = 0; j < 3; ++j)
			if (table[i][j] == 'c')
				((CPluckButton*)(this->GetDlgItem(buttonID[i * 3 + j])))->setcolor(0);
			else if (table[i][j] == 'x')
				((CPluckButton*)(this->GetDlgItem(buttonID[i * 3 + j])))->setcolor(1);
			else
				((CPluckButton*)(this->GetDlgItem(buttonID[i * 3 + j])))->setcolor(2);

//	SendMessage(WM_CLOSE);
}

void COwnDrawButtonDlg::OnButton2() 
{
	initialqipan();// TODO: Add your control notification handler code here
	table[1][1]='x';
	UpdateView();
}

int COwnDrawButtonDlg::check(char table[][3])
{
      
   int k;
	for (k=0;k<8;k++)
	    if (che(table,k,'x')==1) return 1;//机win
		else if (che(table,k,'o')==1)return 2;//人win
		else if(che(table,k,'x')==2&&che(table,k,'o')==2) return 3;//平局
	return 0;

}
 
//判断table[i][j]为最上方的连续三个格子是否都为ch

int COwnDrawButtonDlg::che(char table[][3], int x, char ch)
{
if (x==0) 
		if (table[0][0]==ch && table[0][1]==ch && table[0][2]==ch) return 1;
	if (x==1)
		if (table[1][0]==ch && table[1][1]==ch && table[1][2]==ch) return 1;
	if (x==2)
		if (table[2][0]==ch && table[2][1]==ch && table[2][2]==ch) return 1;
	if (x==3)
		if (table[0][0]==ch && table[1][0]==ch && table[2][0]==ch) return 1;
	if (x==4)
		if (table[0][1]==ch && table[1][1]==ch && table[2][1]==ch) return 1;
	if (x==5)
		if (table[0][2]==ch && table[1][2]==ch && table[2][2]==ch) return 1;
	if (x==6)
		if (table[0][0]==ch && table[1][1]==ch && table[2][2]==ch) return 1;
	if (x==7)
		if (table[0][2]==ch && table[1][1]==ch && table[2][0]==ch) return 1;
	int i,j;
	int flag=0;
	for(i=0;i<3;i++)
		for(j=0;j<3;j++)
		{
			if(table[i][j]=='c')flag=1;
		}
		if(flag==0)return 2;
	return 0;
}


void COwnDrawButtonDlg::initialqipan()
{

	int i,j;
	for(i=0;i<3;i++)
		for(j=0;j<3;j++)
			table[i][j]='c';
		UpdateView();

}

⌨️ 快捷键说明

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